Skip to content

Commit e6104e4

Browse files
committed
Fix initial array data in setup() and script setup
Using setData or providing a data() function for an array property would not work correctly.
1 parent ad9d21a commit e6104e4

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

src/createInstance.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
ComponentOptions,
1010
ConcreteComponent,
1111
DefineComponent,
12-
transformVNodeArgs
12+
transformVNodeArgs,
13+
proxyRefs
1314
} from 'vue'
1415

1516
import { MountingOptions, Slot } from './types'
@@ -20,6 +21,7 @@ import {
2021
isObject,
2122
isObjectComponent,
2223
isScriptSetup,
24+
mergeDeep,
2325
mergeGlobalProperties
2426
} from './utils'
2527
import { processSlot } from './utils/compileSlots'
@@ -163,7 +165,7 @@ export function createInstance(
163165
const originalSetupFn = objectComponent.setup
164166
objectComponent.setup = function (a, b) {
165167
const data = originalSetupFn(a, b)
166-
Object.assign(data, providedData)
168+
mergeDeep(proxyRefs(data), providedData)
167169
return data
168170
}
169171
} else {

src/vueWrapper.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { nextTick, App, ComponentPublicInstance, VNode } from 'vue'
2-
import * as reactivity from '@vue/reactivity'
1+
import { nextTick, App, ComponentPublicInstance, VNode, proxyRefs } from 'vue'
32

43
import { config } from './config'
54
import domEvents from './constants/dom-events'
@@ -266,7 +265,7 @@ export class VueWrapper<
266265
} else if (!Object.isFrozen(this.componentVM.$.setupState)) {
267266
// data from setup() function when using the object api
268267
// @ts-ignore
269-
mergeDeep(reactivity.proxyRefs(this.componentVM.$.setupState), data)
268+
mergeDeep(proxyRefs(this.componentVM.$.setupState), data)
270269
} else {
271270
// data when using data: {...} in the object api
272271
mergeDeep(this.componentVM.$data, data)

tests/setData.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,30 @@ describe('setData', () => {
308308

309309
expect(wrapper.find('#show').exists()).toBe(true)
310310
})
311+
312+
it('correctly sets initial array refs data when using <script setup>', async () => {
313+
const Component = {
314+
template: `<div><div v-for="item in items" :key="item">{{ item }}</div></div>`,
315+
setup() {
316+
const items = ref([])
317+
return { items }
318+
}
319+
}
320+
321+
const wrapper = shallowMount(Component, {
322+
data() {
323+
return {
324+
items: ['item1', 'item2']
325+
}
326+
}
327+
})
328+
329+
expect(wrapper.html()).toContain('item1')
330+
expect(wrapper.html()).toContain('item2')
331+
332+
await wrapper.setData({ items: ['item3', 'item4'] })
333+
334+
expect(wrapper.html()).toContain('item3')
335+
expect(wrapper.html()).toContain('item4')
336+
})
311337
})

0 commit comments

Comments
 (0)