|
1 | 1 | import CSwitch from '..'
|
2 |
| -import { render, userEvent } from '@/tests/test-utils' |
| 2 | +import { render, userEvent, screen } from '@/tests/test-utils' |
3 | 3 |
|
4 |
| -const renderComponent = (props) => { |
| 4 | +const renderComponent = ({ inlineAttrs = '', ...props } = { }) => { |
5 | 5 | const base = {
|
| 6 | + template: `<CSwitch data-testid="switch" ${inlineAttrs} />`, |
6 | 7 | components: { CSwitch },
|
7 | 8 | ...props
|
8 | 9 | }
|
9 | 10 | return render(base)
|
10 | 11 | }
|
11 | 12 | it('should render correctly', () => {
|
12 |
| - const { asFragment } = renderComponent( |
13 |
| - { |
14 |
| - template: ` |
15 |
| - <CSwitch /> |
16 |
| - ` |
17 |
| - } |
18 |
| - ) |
| 13 | + const { asFragment } = renderComponent() |
19 | 14 | expect(asFragment()).toMatchSnapshot()
|
20 | 15 | })
|
21 | 16 |
|
22 | 17 | it('should switch', async () => {
|
23 |
| - const { getByTestId, getByRole } = renderComponent( |
24 |
| - { |
25 |
| - template: '<CSwitch data-testid="label" />' |
26 |
| - } |
27 |
| - ) |
28 |
| - const label = getByTestId('label') |
29 |
| - const input = getByRole('checkbox') |
30 |
| - await userEvent.click(label) |
31 |
| - expect(input).toBeChecked() |
| 18 | + renderComponent() |
| 19 | + |
| 20 | + await userEvent.click(screen.getByTestId('switch')) |
| 21 | + expect(screen.getByRole('checkbox')).toBeChecked() |
32 | 22 | })
|
33 | 23 |
|
34 | 24 | it('should emit a change event', async () => {
|
35 |
| - const spy = jest.fn() |
36 |
| - const { getByTestId } = renderComponent( |
37 |
| - { |
38 |
| - methods: { |
39 |
| - handleChange: spy |
40 |
| - }, |
41 |
| - template: '<CSwitch data-testid="label" @change="handleChange" /> ' |
42 |
| - } |
43 |
| - ) |
| 25 | + const onChange = jest.fn() |
| 26 | + const inlineAttrs = '@change="handleChange"' |
| 27 | + renderComponent({ inlineAttrs, methods: { handleChange: onChange } }) |
44 | 28 |
|
45 |
| - const label = getByTestId('label') |
46 |
| - await userEvent.click(label) |
| 29 | + await userEvent.click(screen.getByTestId('switch')) |
47 | 30 |
|
48 |
| - expect(spy).toHaveBeenCalledTimes(1) |
| 31 | + expect(onChange).toHaveBeenCalledTimes(1) |
49 | 32 | })
|
50 | 33 |
|
51 | 34 | test('properly handles v-model', async () => {
|
52 |
| - const { getByTestId, getByText } = renderComponent( |
53 |
| - { |
54 |
| - data: () => ({ |
55 |
| - enable: false |
56 |
| - }), |
57 |
| - template: ` |
| 35 | + renderComponent({ |
| 36 | + data: () => ({ |
| 37 | + enable: false |
| 38 | + }), |
| 39 | + template: ` |
58 | 40 | <div>
|
59 | 41 | <span>{{enable ? 'enabled' : 'disabled'}}</span>
|
60 |
| - <CSwitch v-model="enable" data-testid="label" /> |
| 42 | + <CSwitch v-model="enable" data-testid="switch" /> |
61 | 43 | </div>`
|
62 |
| - } |
63 |
| - ) |
64 |
| - const label = getByTestId('label') |
| 44 | + }) |
65 | 45 |
|
66 |
| - expect(getByText('disabled')).toBeInTheDocument() |
| 46 | + expect(screen.getByText('disabled')).toBeInTheDocument() |
67 | 47 |
|
68 |
| - await userEvent.click(label) |
| 48 | + await userEvent.click(screen.getByTestId('switch')) |
69 | 49 |
|
70 |
| - expect(getByText('enabled')).toBeInTheDocument() |
| 50 | + expect(screen.getByText('enabled')).toBeInTheDocument() |
71 | 51 | })
|
0 commit comments