Skip to content

Commit 3de7fa3

Browse files
committed
Added unit test for TodoItem component
1 parent eac5de5 commit 3de7fa3

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { fireEvent } from '@testing-library/react';
2+
import React from 'react';
3+
import { render } from '../../test-utils';
4+
import { TodoItem } from './TodoItem';
5+
6+
describe('Todos components', () => {
7+
const todo = { id: "1", title: "lorem ipsum", completed: false };
8+
9+
it('should render snapshot', async () => {
10+
const { container } = await render(<TodoItem todo={todo} onChange={jest.fn()} />);
11+
12+
expect(container).toMatchSnapshot();
13+
});
14+
15+
it('should toggle status', async () => {
16+
const onChangeMock = jest.fn();
17+
const { getByLabelText } = await render(<TodoItem todo={todo} onChange={onChangeMock} />);
18+
19+
fireEvent.click(getByLabelText(todo.title));
20+
21+
expect(onChangeMock).toHaveBeenCalledWith({ id: todo.id, completed: true });
22+
});
23+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Todos components should render snapshot 1`] = `
4+
<div>
5+
<div
6+
class="Todo__item"
7+
>
8+
<label
9+
for="1-checkbox"
10+
>
11+
lorem ipsum
12+
</label>
13+
<input
14+
id="1-checkbox"
15+
type="checkbox"
16+
/>
17+
</div>
18+
</div>
19+
`;

src/test-utils/Provider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { cache } from '../apollo';
66

77
const customRender = async (
88
childComponent: JSX.Element,
9-
options: MockedProviderProps
9+
options?: MockedProviderProps
1010
): Promise<RenderResult> => {
11-
const { mocks, resolvers = {} } = options;
11+
const { mocks = [], resolvers = {} } = options || {};
1212
const wrapper = render(<MockedProvider
1313
mocks={mocks}
1414
resolvers={resolvers}

0 commit comments

Comments
 (0)