Skip to content

Replace setProps with rerender Function #173

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

Merged
merged 1 commit into from
Nov 20, 2020
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
53 changes: 53 additions & 0 deletions src/__tests__/rerender.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import '@testing-library/jest-dom'
import {defineComponent, h, computed} from 'vue'
import {render} from '@testing-library/vue'
import NumberDisplay from './components/NumberDisplay'

// It'd probably be better if you test the component that's doing the rerendering
// to ensure that the rerendered component is being updated correctly.
// That said, if you'd prefer to, for example, update the props of a rendered
// component, this function can be used to do so.
test('calling rerender remounts the component and updates the props', () => {
const {rerender, getByTestId} = render(NumberDisplay, {
props: {number: 1},
})

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

rerender({props: {number: 3}})
expect(getByTestId('number-display')).toHaveTextContent('3')

rerender({props: {number: 5}})
expect(getByTestId('number-display')).toHaveTextContent('5')

// Assert that, after rerendering and updating props, the component has been remounted,
// meaning we are testing a different component instance than we rendered initially.
expect(getByTestId('instance-id')).toHaveTextContent('3')
})

test('rerender works with composition API', () => {
const Component = defineComponent({
props: {
foo: {type: String, default: 'foo'},
},
setup(props) {
const foobar = computed(() => `${props.foo}-bar`)
return () =>
h(
'div',
{'data-testid': 'node'},
`Foo is: ${props.foo}. Foobar is: ${foobar.value}`,
)
},
})

const {rerender, getByTestId} = render(Component)

const originalNode = getByTestId('node')
expect(originalNode).toHaveTextContent('Foo is: foo. Foobar is: foo-bar')

rerender({props: {foo: 'qux'}})

const newNode = getByTestId('node')
expect(newNode).toHaveTextContent('Foo is: qux. Foobar is: qux-bar')
})
51 changes: 0 additions & 51 deletions src/__tests__/set-props.js

This file was deleted.

47 changes: 29 additions & 18 deletions src/vue-testing-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,29 @@ function render(
// additionalOptions = configurationCb(router)
// }

const wrapper = mount(
TestComponent,
merge({
attachTo: container,
global: {
plugins,
},
...mountOptions,
// ...additionalOptions,
}),
)

// this removes the additional "data-v-app" div node from VTU:
// https://github.com./vuejs/vue-test-utils-next/blob/master/src/mount.ts#L196-L213
unwrapNode(wrapper.parentElement)

mountedWrappers.add(wrapper)
const mountComponent = (Component, newProps) => {
const wrapper = mount(
Component,
merge({
attachTo: container,
global: {
plugins,
},
...mountOptions,
props: newProps || mountOptions.props,
// ...additionalOptions,
}),
)

// this removes the additional "data-v-app" div node from VTU:
// https://github.com./vuejs/vue-test-utils-next/blob/master/src/mount.ts#L196-L213
unwrapNode(wrapper.parentElement)

mountedWrappers.add(wrapper)
return wrapper
}

let wrapper = mountComponent(TestComponent)

return {
container,
Expand All @@ -73,7 +79,12 @@ function render(
unmount: () => wrapper.unmount(),
html: () => wrapper.html(),
emitted: () => wrapper.emitted(),
setProps: props => wrapper.setProps(props),
rerender: ({props}) => {
wrapper.unmount()
mountedWrappers.delete(wrapper)

wrapper = mountComponent(TestComponent, props)
},
...getQueriesForElement(baseElement),
}
}
Expand Down