-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.spec.jsx
More file actions
112 lines (101 loc) · 3.55 KB
/
component.spec.jsx
File metadata and controls
112 lines (101 loc) · 3.55 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import React from 'react';
import ShallowRenderer from 'react-test-renderer/shallow';
import { render, fireEvent, act } from '@testing-library/react';
import { advanceTo, clear } from 'jest-date-mock';
import NewsList from './component';
import { waitForTimeout } from '../../../../tests/helpers';
const sampleNewsItems = [1, 2, 3, 4, 5].map((idx) => ({
objectID: idx.toString(),
title: `News ${idx}`,
url: `http://news${idx}.test`,
points: idx * 10,
num_comments: idx * 6,
author: `Author ${idx}`,
created_at: `2020-04-0${idx}T03:09:30.000Z`,
}));
const defaultProps = {
currentNewsItems: [],
currentPage: 1,
currentTotal: 0,
isLoadingNews: false,
getNewsAsync: jest.fn(),
};
describe('<NewsList />', () => {
it('expects to show loading animation when loading', () => {
const { container } = render(<NewsList {...defaultProps} isLoadingNews />);
expect(container.getElementsByClassName('ant-spin-spinning').length).toEqual(1);
});
it('expects call getNewsAsync one time onLoad', () => {
const { getNewsAsync } = defaultProps;
render(<NewsList {...defaultProps} />);
expect(getNewsAsync).toHaveBeenCalledWith({ page: 1 });
expect(getNewsAsync).toHaveBeenCalledTimes(1);
});
it('expects to re-call getNewsAsync with the clicked page', () => {
const { getNewsAsync } = defaultProps;
const { container } = render(
<NewsList
{...defaultProps}
currentPage={1}
currentTotal={10}
/>,
);
expect(getNewsAsync).toHaveBeenCalledWith({ page: 1 });
expect(getNewsAsync).toHaveBeenCalledTimes(1);
fireEvent.click(container.getElementsByClassName('ant-pagination-item-2')[0]);
expect(getNewsAsync).toHaveBeenCalledWith({ page: 2 });
expect(getNewsAsync).toHaveBeenCalledTimes(2);
});
it('expects to re-call getNewsAsync with entererd search query and reset currentPage to 1', async () => {
const { getNewsAsync } = defaultProps;
const { container } = render(
<NewsList
{...defaultProps}
currentPage={2}
currentTotal={10}
/>,
);
expect(getNewsAsync).toHaveBeenCalledWith({ page: 1 });
expect(getNewsAsync).toHaveBeenCalledTimes(1);
await act(async () => {
await fireEvent.change(container.querySelector('#searchTxt'), { target: { value: 'Test Search' } });
await waitForTimeout(600);
});
expect(getNewsAsync).toHaveBeenCalledWith({ page: 1, searchQuery: 'Test Search' });
expect(getNewsAsync).toHaveBeenCalledTimes(2);
});
describe('snapshots', () => {
beforeAll(() => {
advanceTo(new Date(2020, 3, 7, 0, 0, 0));
});
afterAll(() => {
clear();
});
// Follow are two examples to compare between full snapshot and shallow rendering snapshot
// Based on what the development needs, they can mix solutions and decide when to use which
it('expects to render correctly with given list of items', () => {
const { container } = render(
<NewsList
{...defaultProps}
currentPage={1}
currentTotal={10}
currentNewsItems={sampleNewsItems}
/>,
);
expect(container).toMatchSnapshot();
});
it('expectss to render the shallow rendering correctly with given list of items', () => {
const renderer = new ShallowRenderer();
renderer.render(
<NewsList
{...defaultProps}
currentPage={1}
currentTotal={10}
currentNewsItems={sampleNewsItems}
/>,
);
const result = renderer.getRenderOutput();
expect(result).toMatchSnapshot();
});
});
});