|
1 | 1 | import React from 'react'; |
2 | | -import { render } from '@testing-library/react'; |
| 2 | +import { render } from '../../test-utils'; |
3 | 3 | import { generateImage } from 'jsdom-screenshot'; |
4 | 4 | import { axe } from '../../test-utils'; |
5 | 5 | import { Todos } from './Todos'; |
| 6 | +import { GET_TODOS, UPDATE_TODO } from './queries'; |
| 7 | +import { todos } from './mocks'; |
| 8 | +import { act, fireEvent, waitFor } from '@testing-library/react'; |
6 | 9 |
|
7 | 10 | describe('Todos components', () => { |
| 11 | + const mocks = [{ |
| 12 | + request: { |
| 13 | + query: GET_TODOS |
| 14 | + }, |
| 15 | + result: { |
| 16 | + data: { todos } |
| 17 | + } |
| 18 | + }]; |
| 19 | + |
8 | 20 | it('should render snapshot', async () => { |
9 | | - const { container } = render(<Todos />); |
| 21 | + const { container } = await render(<Todos />, { mocks }); |
10 | 22 | expect(container).toMatchSnapshot(); |
11 | 23 | }); |
12 | 24 |
|
13 | 25 | it('should render visual snapshot', async () => { |
14 | | - render(<Todos />); |
| 26 | + await render(<Todos />, { mocks }); |
15 | 27 |
|
16 | 28 | const screenshot = await generateImage(); |
17 | 29 | expect(screenshot).toMatchImageSnapshot(); |
18 | 30 | }); |
19 | 31 |
|
20 | 32 | it('should validate accessibility voilations', async () => { |
21 | | - const { container } = render(<Todos />); |
| 33 | + const { container } = await render(<Todos />, { mocks }); |
22 | 34 | expect(await axe(container)).toHaveNoViolations(); |
23 | 35 | }); |
| 36 | + |
| 37 | + it('should update todo on toggle task selection', async () => { |
| 38 | + const mutationMock = [{ |
| 39 | + request: { |
| 40 | + query: UPDATE_TODO, |
| 41 | + variables: { id: '1', completed: true } |
| 42 | + }, |
| 43 | + result: jest.fn(() => ({ |
| 44 | + data: { |
| 45 | + updateTodo: { |
| 46 | + ...todos[0], |
| 47 | + completed: true |
| 48 | + } |
| 49 | + } |
| 50 | + })) |
| 51 | + }]; |
| 52 | + const { getByLabelText } = await render(<Todos />, { mocks: mutationMock }); |
| 53 | + fireEvent.click(getByLabelText(todos[0].title)); |
| 54 | + const updateMutationMock = mutationMock[0].result; |
| 55 | + await waitFor(() => expect(updateMutationMock).toHaveBeenCalledWith()); |
| 56 | + }); |
24 | 57 | }); |
0 commit comments