-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathrerender.js
52 lines (41 loc) · 1.61 KB
/
rerender.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import '@testing-library/jest-dom'
import {defineComponent, h, computed} from 'vue'
import {render} from '..'
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('rerender re-renders the element', async () => {
const {rerender, getByTestId} = render(NumberDisplay, {
props: {number: 1},
})
expect(getByTestId('number-display')).toHaveTextContent('1')
await rerender({number: 3})
expect(getByTestId('number-display')).toHaveTextContent('3')
await rerender({number: 5})
expect(getByTestId('number-display')).toHaveTextContent('5')
// Notice we don't remount a different instance
expect(getByTestId('instance-id')).toHaveTextContent('1')
})
test('rerender works with composition API', async () => {
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 getContent = () => getByTestId('node')
expect(getContent()).toHaveTextContent('Foo is: foo. Foobar is: foo-bar')
await rerender({foo: 'qux'})
expect(getContent()).toHaveTextContent('Foo is: qux. Foobar is: qux-bar')
})