|
| 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 | +test('calling rerender remounts the component and updates the props', () => { |
| 7 | + const {rerender, getByTestId} = render(NumberDisplay, { |
| 8 | + props: {number: 1}, |
| 9 | + }) |
| 10 | + |
| 11 | + expect(getByTestId('number-display')).toHaveTextContent('1') |
| 12 | + |
| 13 | + rerender({props: {number: 3}}) |
| 14 | + expect(getByTestId('number-display')).toHaveTextContent('3') |
| 15 | + |
| 16 | + // Assert that, after rerendering and updating props, the component has been remounted, |
| 17 | + // meaning we are testing a different component instance than we rendered initially. |
| 18 | + expect(getByTestId('instance-id')).toHaveTextContent('2') |
| 19 | +}) |
| 20 | + |
| 21 | +test('rerender works with composition API', () => { |
| 22 | + const Component = defineComponent({ |
| 23 | + props: { |
| 24 | + foo: {type: String, default: 'foo'}, |
| 25 | + }, |
| 26 | + setup(props) { |
| 27 | + const foobar = computed(() => `${props.foo}-bar`) |
| 28 | + return () => |
| 29 | + h( |
| 30 | + 'div', |
| 31 | + {'data-testid': 'node'}, |
| 32 | + `Foo is: ${props.foo}. Foobar is: ${foobar.value}`, |
| 33 | + ) |
| 34 | + }, |
| 35 | + }) |
| 36 | + |
| 37 | + const {rerender, getByTestId} = render(Component) |
| 38 | + |
| 39 | + const originalNode = getByTestId('node') |
| 40 | + expect(originalNode).toHaveTextContent('Foo is: foo. Foobar is: foo-bar') |
| 41 | + |
| 42 | + rerender({props: {foo: 'qux'}}) |
| 43 | + |
| 44 | + const newNode = getByTestId('node') |
| 45 | + expect(newNode).toHaveTextContent('Foo is: qux. Foobar is: qux-bar') |
| 46 | +}) |
0 commit comments