Skip to content

Commit f73278c

Browse files
UrsMetzlmiller1990
andauthored
fix: reactive props for stubs (#453)
* Add tests to reproduce bug * Add created stub to stubs mapping * chore: lint Co-authored-by: Lachlan Miller <[email protected]>
1 parent 3a06c17 commit f73278c

File tree

2 files changed

+77
-7
lines changed

2 files changed

+77
-7
lines changed

src/stubs.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,9 @@ export function stubComponents(
169169
// case 1: default stub
170170
if (stub === true || shallow) {
171171
const propsDeclaration = type?.props || {}
172-
return [
173-
createStub({ name, propsDeclaration, props }),
174-
props,
175-
children,
176-
patchFlag,
177-
dynamicProps
178-
]
172+
const newStub = createStub({ name, propsDeclaration, props })
173+
stubs[name] = newStub
174+
return [newStub, props, children, patchFlag, dynamicProps]
179175
}
180176
}
181177

tests/props.spec.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,78 @@ describe('props', () => {
111111
await wrapper.trigger('click')
112112
expect(wrapper.props('modelValue')).toBe(2)
113113
})
114+
115+
it('returns reactive props on a stubbed component', async () => {
116+
const Foo = {
117+
name: 'Foo',
118+
template: 'Foo',
119+
props: {
120+
foo: String
121+
}
122+
}
123+
const Component = defineComponent({
124+
data: () => ({ foo: 'old value' }),
125+
template:
126+
'<div><foo :foo="foo" /><button @click="buttonClick">Click me</button></div>',
127+
methods: {
128+
buttonClick() {
129+
this.foo = 'new value'
130+
}
131+
},
132+
components: { Foo }
133+
})
134+
135+
const wrapper = mount(Component, {
136+
global: {
137+
stubs: ['Foo']
138+
}
139+
})
140+
let fooCmp = wrapper.findComponent({ name: 'Foo' })
141+
142+
expect(fooCmp.props()).toEqual({
143+
foo: 'old value'
144+
})
145+
146+
await wrapper.find('button').trigger('click')
147+
148+
expect(fooCmp.props()).toEqual({
149+
foo: 'new value'
150+
})
151+
})
152+
153+
it('returns reactive props on a stubbed component shallow case', async () => {
154+
const Foo = {
155+
name: 'Foo',
156+
template: 'Foo',
157+
props: {
158+
foo: String
159+
}
160+
}
161+
const Component = defineComponent({
162+
data: () => ({ foo: 'old value' }),
163+
template:
164+
'<div><foo :foo="foo" /><button @click="buttonClick">Click me</button></div>',
165+
methods: {
166+
buttonClick() {
167+
this.foo = 'new value'
168+
}
169+
},
170+
components: { Foo }
171+
})
172+
173+
const wrapper = mount(Component, {
174+
shallow: true
175+
})
176+
let fooCmp = wrapper.findComponent({ name: 'Foo' })
177+
178+
expect(fooCmp.props()).toEqual({
179+
foo: 'old value'
180+
})
181+
182+
await wrapper.find('button').trigger('click')
183+
184+
expect(fooCmp.props()).toEqual({
185+
foo: 'new value'
186+
})
187+
})
114188
})

0 commit comments

Comments
 (0)