Skip to content

Commit ca5f32c

Browse files
authored
fix(rerender): Do not mount component again on rerender (#199)
* Do not mount component again on rerender * Remove leftover
1 parent 94f57f8 commit ca5f32c

File tree

2 files changed

+25
-37
lines changed

2 files changed

+25
-37
lines changed

src/__tests__/rerender.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,24 @@ import NumberDisplay from './components/NumberDisplay'
77
// to ensure that the rerendered component is being updated correctly.
88
// That said, if you'd prefer to, for example, update the props of a rendered
99
// component, this function can be used to do so.
10-
test('calling rerender remounts the component and updates the props', () => {
10+
test('rerender re-renders the element', async () => {
1111
const {rerender, getByTestId} = render(NumberDisplay, {
1212
props: {number: 1},
1313
})
1414

1515
expect(getByTestId('number-display')).toHaveTextContent('1')
1616

17-
rerender({props: {number: 3}})
17+
await rerender({number: 3})
1818
expect(getByTestId('number-display')).toHaveTextContent('3')
1919

20-
rerender({props: {number: 5}})
20+
await rerender({number: 5})
2121
expect(getByTestId('number-display')).toHaveTextContent('5')
2222

23-
// Assert that, after rerendering and updating props, the component has been remounted,
24-
// meaning we are testing a different component instance than we rendered initially.
25-
expect(getByTestId('instance-id')).toHaveTextContent('3')
23+
// Notice we don't remount a different instance
24+
expect(getByTestId('instance-id')).toHaveTextContent('1')
2625
})
2726

28-
test('rerender works with composition API', () => {
27+
test('rerender works with composition API', async () => {
2928
const Component = defineComponent({
3029
props: {
3130
foo: {type: String, default: 'foo'},
@@ -43,11 +42,11 @@ test('rerender works with composition API', () => {
4342

4443
const {rerender, getByTestId} = render(Component)
4544

46-
const originalNode = getByTestId('node')
47-
expect(originalNode).toHaveTextContent('Foo is: foo. Foobar is: foo-bar')
45+
const getContent = () => getByTestId('node')
4846

49-
rerender({props: {foo: 'qux'}})
47+
expect(getContent()).toHaveTextContent('Foo is: foo. Foobar is: foo-bar')
5048

51-
const newNode = getByTestId('node')
52-
expect(newNode).toHaveTextContent('Foo is: qux. Foobar is: qux-bar')
49+
await rerender({foo: 'qux'})
50+
51+
expect(getContent()).toHaveTextContent('Foo is: qux. Foobar is: qux-bar')
5352
})

src/index.js

+14-25
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
const mountedWrappers = new Set()
1313

1414
function render(
15-
TestComponent,
15+
Component,
1616
{
1717
store = null,
1818
routes = null,
@@ -41,26 +41,20 @@ function render(
4141
plugins.push(routerPlugin)
4242
}
4343

44-
const mountComponent = (Component, newProps) => {
45-
const wrapper = mount(
46-
Component,
47-
merge({
48-
attachTo: container,
49-
global: {plugins},
50-
...mountOptions,
51-
props: newProps || mountOptions.props,
52-
}),
53-
)
54-
55-
// this removes the additional "data-v-app" div node from VTU:
56-
// https://github.com/vuejs/vue-test-utils-next/blob/master/src/mount.ts#L196-L213
57-
unwrapNode(wrapper.parentElement)
44+
const wrapper = mount(
45+
Component,
46+
merge({
47+
attachTo: container,
48+
global: {plugins},
49+
...mountOptions,
50+
}),
51+
)
5852

59-
mountedWrappers.add(wrapper)
60-
return wrapper
61-
}
53+
// this removes the additional "data-v-app" div node from VTU:
54+
// https://github.com/vuejs/vue-test-utils-next/blob/master/src/mount.ts#L196-L213
55+
unwrapNode(wrapper.parentElement)
6256

63-
let wrapper = mountComponent(TestComponent)
57+
mountedWrappers.add(wrapper)
6458

6559
return {
6660
container,
@@ -72,12 +66,7 @@ function render(
7266
unmount: () => wrapper.unmount(),
7367
html: () => wrapper.html(),
7468
emitted: () => wrapper.emitted(),
75-
rerender: ({props}) => {
76-
wrapper.unmount()
77-
mountedWrappers.delete(wrapper)
78-
79-
wrapper = mountComponent(TestComponent, props)
80-
},
69+
rerender: props => wrapper.setProps(props),
8170
...getQueriesForElement(baseElement),
8271
}
8372
}

0 commit comments

Comments
 (0)