File tree Expand file tree Collapse file tree 3 files changed +47
-3
lines changed
Expand file tree Collapse file tree 3 files changed +47
-3
lines changed Original file line number Diff line number Diff line change 1- import { getCurrentInstance } from 'vue'
1+ import { getCurrentInstance , reactive } from 'vue'
22
33export const createDataMixin = ( data : Record < string , unknown > ) => {
44 return {
55 created ( ) {
66 for ( const [ k , v ] of Object . entries ( data ) ) {
7- getCurrentInstance ( ) ! . data = { ...getCurrentInstance ( ) ! . data , [ k ] : v }
7+ getCurrentInstance ( ) ! . data = reactive ( {
8+ ...getCurrentInstance ( ) ! . data ,
9+ [ k ] : v
10+ } )
811 }
912 }
1013 }
Original file line number Diff line number Diff line change @@ -35,7 +35,6 @@ import { attachEmitListener } from './emit'
3535import { createDataMixin } from './dataMixin'
3636import { MOUNT_COMPONENT_REF , MOUNT_PARENT_NAME } from './constants'
3737import { createStub , stubComponents } from './stubs'
38- import { hyphenate } from './utils/vueShared'
3938
4039// NOTE this should come from `vue`
4140type PublicProps = VNodeProps & AllowedComponentProps & ComponentCustomProps
Original file line number Diff line number Diff line change @@ -123,4 +123,46 @@ describe('setData', () => {
123123 'Cannot add property count'
124124 )
125125 } )
126+
127+ // https://github.com/vuejs/vue-test-utils-next/issues/538
128+ it ( 'updates data set via data mounting option using setData' , async ( ) => {
129+ const Comp = defineComponent <
130+ { } ,
131+ { } ,
132+ { field : number | null } ,
133+ { isFieldNull : any }
134+ > ( {
135+ template : `
136+ <div>{{ isFieldNull ? 'It is null' : 'It is not null' }}</div>
137+ ` ,
138+ data ( ) {
139+ return {
140+ field : null
141+ }
142+ } ,
143+ computed : {
144+ isFieldNull ( ) {
145+ return this . field === null
146+ }
147+ }
148+ } )
149+
150+ const wrapper = mount ( Comp , {
151+ data ( ) {
152+ return {
153+ field : null
154+ }
155+ }
156+ } )
157+
158+ expect ( wrapper . vm . isFieldNull ) . toBe ( true )
159+ expect ( wrapper . html ( ) ) . toContain ( 'It is null' )
160+
161+ await wrapper . setData ( { field : 10 } )
162+ await wrapper . vm . $nextTick ( )
163+
164+ expect ( wrapper . html ( ) ) . toContain ( 'It is not null' )
165+ expect ( wrapper . vm . field ) . toEqual ( 10 )
166+ expect ( wrapper . vm . isFieldNull ) . toBe ( false )
167+ } )
126168} )
You can’t perform that action at this time.
0 commit comments