Skip to content

Commit 7486952

Browse files
authored
fix: make data mounting option reactive (#540)
Fixes #538
1 parent 706e5ab commit 7486952

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

src/dataMixin.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { getCurrentInstance } from 'vue'
1+
import { getCurrentInstance, reactive } from 'vue'
22

33
export 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
}

src/mount.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import { attachEmitListener } from './emit'
3535
import { createDataMixin } from './dataMixin'
3636
import { MOUNT_COMPONENT_REF, MOUNT_PARENT_NAME } from './constants'
3737
import { createStub, stubComponents } from './stubs'
38-
import { hyphenate } from './utils/vueShared'
3938

4039
// NOTE this should come from `vue`
4140
type PublicProps = VNodeProps & AllowedComponentProps & ComponentCustomProps

tests/setData.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff 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
})

0 commit comments

Comments
 (0)