-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathtasksStats.test.js
141 lines (133 loc) · 4.78 KB
/
tasksStats.test.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { ReduxIntlProviders } from '../../../utils/testWithIntl';
import { tasksStats } from '../../../network/tests/mockData/tasksStats';
import { TasksStats } from '../tasksStats';
import userEvent from '@testing-library/user-event';
// This is a late import in a React.lazy call; it takes awhile for date-fns to resolve, so we import it here manually.
// In the event you remove it, please measure test times before ''and'' after removal.
import '../../../utils/chart';
jest.mock('react-chartjs-2', () => ({
Bar: () => null,
}));
describe('TasksStats', () => {
const setQuery = jest.fn();
const retryFn = jest.fn();
it('render basic elements', async () => {
render(
<ReduxIntlProviders>
<TasksStats
stats={tasksStats.taskStats}
query={{ startDate: null, endDate: null, campaign: null, location: null }}
/>
</ReduxIntlProviders>,
);
// wait for useTagAPI to act on the states
expect(
await screen.findByRole('group', {
name: 'Campaign',
}),
);
expect(screen.getByText('From')).toBeInTheDocument();
expect(screen.getByText('To')).toBeInTheDocument();
expect(
screen.getByRole('group', {
name: 'Campaign',
}),
).toBeInTheDocument();
expect(
screen.getByRole('group', {
name: 'Location',
}),
).toBeInTheDocument();
expect(screen.getByText('165')).toBeInTheDocument();
expect(screen.getByText('Tasks mapped')).toBeInTheDocument();
expect(screen.getByText('46')).toBeInTheDocument();
expect(screen.getByText('Tasks validated')).toBeInTheDocument();
expect(screen.getByText('211')).toBeInTheDocument();
expect(screen.getByText('Completed actions')).toBeInTheDocument();
});
it('load correct query values', async () => {
const { container } = render(
<ReduxIntlProviders>
<TasksStats
stats={tasksStats.taskStats}
setQuery={setQuery}
query={{ startDate: '2020-04-05', endDate: '2021-01-01', campaign: null, location: null }}
/>
</ReduxIntlProviders>,
);
expect(
await screen.findByRole('group', {
name: 'Campaign',
}),
).toBeInTheDocument();
const startDateInput = container.querySelectorAll('input')[0];
const endDateInput = container.querySelectorAll('input')[1];
expect(startDateInput.placeholder).toBe('Click to select a start date');
expect(startDateInput.value).toBe('2020-04-05');
expect(endDateInput.placeholder).toBe('Click to select an end date');
expect(endDateInput.value).toBe('2021-01-01');
});
it('show error message if date range exceeds the maximum value', async () => {
render(
<ReduxIntlProviders>
<TasksStats
stats={tasksStats.taskStats}
setQuery={setQuery}
query={{ startDate: '2019-04-05', endDate: null, campaign: null, location: null }}
error={true}
/>
</ReduxIntlProviders>,
);
expect(
await screen.findByRole('group', {
name: 'Campaign',
}),
).toBeInTheDocument();
expect(screen.getByText('An error occurred while loading stats.')).toBeInTheDocument();
expect(screen.getByText('Date range is longer than one year.')).toBeInTheDocument();
});
it('show error message if start date is after end date', async () => {
render(
<ReduxIntlProviders>
<TasksStats
stats={tasksStats.taskStats}
setQuery={setQuery}
query={{ startDate: '2019-04-05', endDate: '2018-08-05', campaign: null, location: null }}
error={true}
/>
</ReduxIntlProviders>,
);
expect(
await screen.findByRole('group', {
name: 'Campaign',
}),
).toBeInTheDocument();
expect(screen.getByText('An error occurred while loading stats.')).toBeInTheDocument();
expect(screen.getByText('Start date should not be later than end date.')).toBeInTheDocument();
});
it('render "Try again" button case the error is not on the dates', async () => {
const user = userEvent.setup();
render(
<ReduxIntlProviders>
<TasksStats
stats={tasksStats.taskStats}
setQuery={setQuery}
query={{ startDate: '2020-04-05', endDate: '2021-01-01', campaign: null, location: null }}
error={true}
retryFn={retryFn}
/>
</ReduxIntlProviders>,
);
expect(
await screen.findByRole('group', {
name: 'Campaign',
}),
).toBeInTheDocument();
expect(screen.getByText('An error occurred while loading stats.')).toBeInTheDocument();
expect(screen.getByText('Try again')).toBeInTheDocument();
await user.click(screen.getByText('Try again'));
expect(retryFn).toHaveBeenCalled();
});
});