-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubRepos.test.tsx
More file actions
215 lines (158 loc) · 5.33 KB
/
GitHubRepos.test.tsx
File metadata and controls
215 lines (158 loc) · 5.33 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* eslint-disable testing-library/no-render-in-setup */
import { GitHubRepos } from './GitHubRepos';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { GitHubRepo } from '../types';
import { fakeRepo, nextFakeRepo, server } from '../mocks/GitHubRepos';
beforeAll(() => server.listen());
beforeEach(() => {
server.resetHandlers();
render(<GitHubRepos />);
});
afterAll(() => server.close());
describe('When component is mounted', () => {
it('should render search input', () => {
expect(
screen.getByPlaceholderText('Search GitHub repositories')
).toBeInTheDocument();
});
it('should render a search button', () => {
expect(screen.getByRole('button', { name: 'Search' })).toBeInTheDocument();
});
it('should render an no results message', () => {
expect(screen.getByText('No results.')).toBeInTheDocument();
});
it('should not render next and previous buttons', () => {
expect(
screen.queryByRole('button', { name: 'Next' })
).not.toBeInTheDocument();
expect(
screen.queryByRole('button', { name: 'Previous' })
).not.toBeInTheDocument();
});
});
describe('When query is entered and search button is clicked', () => {
it('should render loading message', () => {
changeQuery('test');
clickSearchButton();
expect(screen.getByText('Loading...')).toBeInTheDocument();
});
it('should render next and previous buttons', async () => {
changeQuery('test');
clickSearchButton();
await waitForLoadingToNotBeVisible();
expect(screen.getByRole('button', { name: 'Next' })).toBeInTheDocument();
expect(
screen.getByRole('button', { name: 'Previous' })
).toBeInTheDocument();
});
it('should render results', async () => {
changeQuery('test');
clickSearchButton();
await waitForLoadingToNotBeVisible();
expectRepo(fakeRepo);
});
it('should render and error message when request fails', async () => {
changeQuery('error');
clickSearchButton();
await waitFor(() => {
expect(screen.getByText('An error occurred.')).toBeInTheDocument();
});
});
it('should render next page when next button is clicked', async () => {
changeQuery('test');
clickSearchButton();
await waitForLoadingToNotBeVisible();
clickNextPageButton();
await waitForLoadingToNotBeVisible();
expectRepo(nextFakeRepo);
clickPreviousPageButton();
await waitFor(() => {
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
});
expectRepo(fakeRepo);
});
it('should render many results', async () => {
changeQuery('many');
clickSearchButton();
await waitForLoadingToNotBeVisible();
expect(screen.getAllByRole('row').length).toBe(31);
clickNextPageButton();
await waitForLoadingToNotBeVisible();
expect(screen.getAllByRole('row').length).toBe(31);
clickNextPageButton();
await waitForLoadingToNotBeVisible();
expect(screen.getAllByRole('row').length).toBe(6);
clickPreviousPageButton();
await waitForLoadingToNotBeVisible();
expect(screen.getAllByRole('row').length).toBe(31);
clickPreviousPageButton();
await waitForLoadingToNotBeVisible();
expect(screen.getAllByRole('row').length).toBe(31);
});
it('should render first page when search button is clicked again', async () => {
changeQuery('test');
clickSearchButton();
await waitForLoadingToNotBeVisible();
expectRepo(fakeRepo);
clickNextPageButton();
clickSearchButton();
await waitForLoadingToNotBeVisible();
expectRepo(fakeRepo);
});
});
/**
* Helper functions
*/
const waitForLoadingToNotBeVisible = async () => {
await waitFor(() => {
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
});
};
const clickSearchButton = () => {
const searchButton = screen.getByRole('button', { name: 'Search' });
fireEvent.click(searchButton);
};
const clickNextPageButton = () => {
const nextPageButton = screen.getByRole('button', { name: 'Next' });
fireEvent.click(nextPageButton);
};
const clickPreviousPageButton = () => {
const previousPageButton = screen.getByRole('button', {
name: 'Previous',
});
fireEvent.click(previousPageButton);
};
const changeQuery = (query: string) => {
const searchInput = screen.getByPlaceholderText('Search GitHub repositories');
fireEvent.change(searchInput, { target: { value: query } });
};
const expectRepo = (repo: GitHubRepo) => {
expect(
screen.getByRole('columnheader', { name: 'Name' })
).toBeInTheDocument();
expect(screen.getByRole('cell', { name: repo.name })).toBeInTheDocument();
expect(
screen.getByRole('columnheader', { name: 'Description' })
).toBeInTheDocument();
expect(
screen.getByRole('cell', { name: repo.description })
).toBeInTheDocument();
expect(
screen.getByRole('columnheader', { name: 'Owner' })
).toBeInTheDocument();
expect(
screen.getByRole('cell', { name: repo.owner.login })
).toBeInTheDocument();
expect(
screen.getByRole('columnheader', { name: 'Stars' })
).toBeInTheDocument();
expect(
screen.getByRole('cell', { name: repo.stargazers_count.toString() })
).toBeInTheDocument();
expect(
screen.getByRole('columnheader', { name: 'Forks' })
).toBeInTheDocument();
expect(
screen.getByRole('cell', { name: repo.forks_count.toString() })
).toBeInTheDocument();
};