Skip to content

Commit 9779bcc

Browse files
committed
Replace setProps with rerender Function
* Removed `setProps` function (and tests) to confirm with Testing Library standards. * Added `rerender` function (and tests) to make the API more familiar to the Testing Library family.
1 parent 9e20198 commit 9779bcc

File tree

3 files changed

+71
-53
lines changed

3 files changed

+71
-53
lines changed

src/__tests__/rerender.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import '@testing-library/jest-dom'
2+
import {defineComponent, h, computed} from 'vue'
3+
import {render} from '@testing-library/vue'
4+
import NumberDisplay from './components/NumberDisplay'
5+
6+
// It'd probably be better if you test the component that's doing the rerendering
7+
// to ensure that the rerendered component is being updated correctly.
8+
// That said, if you'd prefer to, for example, update the props of a rendered
9+
// component, this function can be used to do so.
10+
test('calling rerender remounts the component and updates the props', () => {
11+
const {rerender, getByTestId} = render(NumberDisplay, {
12+
props: {number: 1},
13+
})
14+
15+
expect(getByTestId('number-display')).toHaveTextContent('1')
16+
17+
rerender({props: {number: 3}})
18+
expect(getByTestId('number-display')).toHaveTextContent('3')
19+
20+
// Assert that, after rerendering and updating props, the component has been remounted,
21+
// meaning we are testing a different component instance than we rendered initially.
22+
expect(getByTestId('instance-id')).toHaveTextContent('2')
23+
})
24+
25+
test('rerender works with composition API', () => {
26+
const Component = defineComponent({
27+
props: {
28+
foo: {type: String, default: 'foo'},
29+
},
30+
setup(props) {
31+
const foobar = computed(() => `${props.foo}-bar`)
32+
return () =>
33+
h(
34+
'div',
35+
{'data-testid': 'node'},
36+
`Foo is: ${props.foo}. Foobar is: ${foobar.value}`,
37+
)
38+
},
39+
})
40+
41+
const {rerender, getByTestId} = render(Component)
42+
43+
const originalNode = getByTestId('node')
44+
expect(originalNode).toHaveTextContent('Foo is: foo. Foobar is: foo-bar')
45+
46+
rerender({props: {foo: 'qux'}})
47+
48+
const newNode = getByTestId('node')
49+
expect(newNode).toHaveTextContent('Foo is: qux. Foobar is: qux-bar')
50+
})

src/__tests__/set-props.js

-51
This file was deleted.

src/vue-testing-library.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function render(
4747
// additionalOptions = configurationCb(router)
4848
// }
4949

50-
const wrapper = mount(
50+
let wrapper = mount(
5151
TestComponent,
5252
merge({
5353
attachTo: container,
@@ -73,7 +73,26 @@ function render(
7373
unmount: () => wrapper.unmount(),
7474
html: () => wrapper.html(),
7575
emitted: () => wrapper.emitted(),
76-
setProps: props => wrapper.setProps(props),
76+
rerender: ({props}) => {
77+
wrapper.unmount()
78+
mountedWrappers.delete(wrapper)
79+
80+
wrapper = mount(
81+
TestComponent,
82+
merge({
83+
attachTo: container,
84+
global: {
85+
plugins,
86+
},
87+
...mountOptions,
88+
props,
89+
// ...additionalOptions,
90+
}),
91+
)
92+
93+
unwrapNode(wrapper.parentElement)
94+
mountedWrappers.add(wrapper)
95+
},
7796
...getQueriesForElement(baseElement),
7897
}
7998
}

0 commit comments

Comments
 (0)