Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix non-reactive stub props #453

Merged
merged 3 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,9 @@ export function stubComponents(
// case 1: default stub
if (stub === true || shallow) {
const propsDeclaration = type?.props || {}
return [
createStub({ name, propsDeclaration, props }),
props,
children,
patchFlag,
dynamicProps
]
const newStub = createStub({ name, propsDeclaration, props })
stubs[name] = newStub
return [newStub, props, children, patchFlag, dynamicProps]
}
}

Expand Down
74 changes: 74 additions & 0 deletions tests/props.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,78 @@ describe('props', () => {
await wrapper.trigger('click')
expect(wrapper.props('modelValue')).toBe(2)
})

it('returns reactive props on a stubbed component', async () => {
const Foo = {
name: 'Foo',
template: 'Foo',
props: {
foo: String
}
}
const Component = defineComponent({
data: () => ({ foo: 'old value' }),
template:
'<div><foo :foo="foo" /><button @click="buttonClick">Click me</button></div>',
methods: {
buttonClick() {
this.foo = 'new value'
}
},
components: { Foo }
})

const wrapper = mount(Component, {
global: {
stubs: ['Foo']
}
})
let fooCmp = wrapper.findComponent({ name: 'Foo' })

expect(fooCmp.props()).toEqual({
foo: 'old value'
})

await wrapper.find('button').trigger('click')

expect(fooCmp.props()).toEqual({
foo: 'new value'
})
})

it('returns reactive props on a stubbed component shallow case', async () => {
const Foo = {
name: 'Foo',
template: 'Foo',
props: {
foo: String
}
}
const Component = defineComponent({
data: () => ({ foo: 'old value' }),
template:
'<div><foo :foo="foo" /><button @click="buttonClick">Click me</button></div>',
methods: {
buttonClick() {
this.foo = 'new value'
}
},
components: { Foo }
})

const wrapper = mount(Component, {
shallow: true
})
let fooCmp = wrapper.findComponent({ name: 'Foo' })

expect(fooCmp.props()).toEqual({
foo: 'old value'
})

await wrapper.find('button').trigger('click')

expect(fooCmp.props()).toEqual({
foo: 'new value'
})
})
})