|
1 | 1 | // @flow
|
2 | 2 |
|
3 |
| -import Vue from 'vue' |
4 | 3 | import pretty from 'pretty'
|
5 | 4 | import getSelector from './get-selector'
|
6 | 5 | import {
|
|
9 | 8 | VUE_VERSION,
|
10 | 9 | DOM_SELECTOR
|
11 | 10 | } from 'shared/consts'
|
12 |
| -import config from './config' |
13 | 11 | import WrapperArray from './wrapper-array'
|
14 | 12 | import ErrorWrapper from './error-wrapper'
|
15 | 13 | import {
|
@@ -721,71 +719,49 @@ export default class Wrapper implements BaseWrapper {
|
721 | 719 | if (!this.vm) {
|
722 | 720 | throwError(`wrapper.setProps() can only be called on a Vue instance`)
|
723 | 721 | }
|
724 |
| - this.__warnIfDestroyed() |
725 | 722 |
|
726 |
| - // Save the original "silent" config so that we can directly mutate props |
727 |
| - const originalConfig = Vue.config.silent |
728 |
| - Vue.config.silent = config.silent |
729 |
| - |
730 |
| - try { |
731 |
| - Object.keys(data).forEach(key => { |
732 |
| - // Don't let people set entire objects, because reactivity won't work |
733 |
| - if ( |
734 |
| - typeof data[key] === 'object' && |
735 |
| - data[key] !== null && |
736 |
| - // $FlowIgnore : Problem with possibly null this.vm |
737 |
| - data[key] === this.vm[key] |
738 |
| - ) { |
739 |
| - throwError( |
740 |
| - `wrapper.setProps() called with the same object of the existing ` + |
741 |
| - `${key} property. You must call wrapper.setProps() with a new ` + |
742 |
| - `object to trigger reactivity` |
743 |
| - ) |
744 |
| - } |
| 723 | + // $FlowIgnore : Problem with possibly null this.vm |
| 724 | + if (!this.vm.$parent.$options.$_isWrapperParent) { |
| 725 | + throwError( |
| 726 | + `wrapper.setProps() can only be called for top-level component` |
| 727 | + ) |
| 728 | + } |
745 | 729 |
|
746 |
| - if ( |
747 |
| - !this.vm || |
748 |
| - !this.vm.$options._propKeys || |
749 |
| - !this.vm.$options._propKeys.some(prop => prop === key) |
750 |
| - ) { |
751 |
| - if (VUE_VERSION > 2.3) { |
752 |
| - // $FlowIgnore : Problem with possibly null this.vm |
753 |
| - this.vm.$attrs[key] = data[key] |
754 |
| - return nextTick() |
755 |
| - } |
756 |
| - throwError( |
757 |
| - `wrapper.setProps() called with ${key} property which ` + |
758 |
| - `is not defined on the component` |
759 |
| - ) |
760 |
| - } |
| 730 | + this.__warnIfDestroyed() |
761 | 731 |
|
762 |
| - // Actually set the prop |
| 732 | + Object.keys(data).forEach(key => { |
| 733 | + // Don't let people set entire objects, because reactivity won't work |
| 734 | + if ( |
| 735 | + typeof data[key] === 'object' && |
| 736 | + data[key] !== null && |
763 | 737 | // $FlowIgnore : Problem with possibly null this.vm
|
764 |
| - this.vm[key] = data[key] |
765 |
| - }) |
| 738 | + data[key] === this.vm[key] |
| 739 | + ) { |
| 740 | + throwError( |
| 741 | + `wrapper.setProps() called with the same object of the existing ` + |
| 742 | + `${key} property. You must call wrapper.setProps() with a new ` + |
| 743 | + `object to trigger reactivity` |
| 744 | + ) |
| 745 | + } |
| 746 | + |
| 747 | + if ( |
| 748 | + VUE_VERSION <= 2.3 && |
| 749 | + (!this.vm || |
| 750 | + !this.vm.$options._propKeys || |
| 751 | + !this.vm.$options._propKeys.some(prop => prop === key)) |
| 752 | + ) { |
| 753 | + throwError( |
| 754 | + `wrapper.setProps() called with ${key} property which ` + |
| 755 | + `is not defined on the component` |
| 756 | + ) |
| 757 | + } |
766 | 758 |
|
767 | 759 | // $FlowIgnore : Problem with possibly null this.vm
|
768 |
| - this.vm.$forceUpdate() |
769 |
| - return new Promise(resolve => { |
770 |
| - nextTick().then(() => { |
771 |
| - const isUpdated = Object.keys(data).some(key => { |
772 |
| - return ( |
773 |
| - // $FlowIgnore : Problem with possibly null this.vm |
774 |
| - this.vm[key] === data[key] || |
775 |
| - // $FlowIgnore : Problem with possibly null this.vm |
776 |
| - (this.vm.$attrs && this.vm.$attrs[key] === data[key]) |
777 |
| - ) |
778 |
| - }) |
779 |
| - return !isUpdated ? this.setProps(data).then(resolve()) : resolve() |
780 |
| - }) |
781 |
| - }) |
782 |
| - } catch (err) { |
783 |
| - throw err |
784 |
| - } finally { |
785 |
| - // Ensure you teardown the modifications you made to the user's config |
786 |
| - // After all the props are set, then reset the state |
787 |
| - Vue.config.silent = originalConfig |
788 |
| - } |
| 760 | + const parent = this.vm.$parent |
| 761 | + parent.$set(parent.vueTestUtils_childProps, key, data[key]) |
| 762 | + }) |
| 763 | + |
| 764 | + return nextTick() |
789 | 765 | }
|
790 | 766 |
|
791 | 767 | /**
|
|
0 commit comments