forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch-bar.spec.ts
250 lines (206 loc) · 7.96 KB
/
search-bar.spec.ts
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { test, expect, type Page } from '@playwright/test';
import translations from '../client/i18n/locales/english/translations.json';
import algoliaEightHits from './fixtures/algolia-eight-hits.json';
import algoliaFiveHits from './fixtures/algolia-five-hits.json';
import algoliaNoHits from './fixtures/algolia-no-hits.json';
const haveApiKeys =
process.env.ALGOLIA_APP_ID !== 'app_id_from_algolia_dashboard' &&
process.env.ALGOLIA_API_KEY !== 'api_key_from_algolia_dashboard';
const getSearchInput = async ({
page,
isMobile
}: {
page: Page;
isMobile: boolean;
}) => {
if (isMobile) {
const menuButton = page.getByRole('button', {
name: translations.buttons.menu
});
await expect(menuButton).toBeVisible();
await menuButton.click();
}
return page.getByLabel('Search', { exact: true });
};
const search = async ({
page,
isMobile,
query
}: {
page: Page;
isMobile: boolean;
query: string;
}) => {
const searchInput = await getSearchInput({ page, isMobile });
await searchInput.fill(query);
};
// Mock Algolia requests to prevent hitting Algolia server unnecessarily.
// Comment out the function call if you want to test against the real server.
const mockAlgolia = async ({
page,
hitsPerPage
}: {
page: Page;
hitsPerPage: number;
}) => {
if (hitsPerPage === 8) {
await page.route(/dsn.algolia.net/, async route => {
await route.fulfill({ json: algoliaEightHits });
});
} else if (hitsPerPage === 5) {
await page.route(/dsn.algolia.net/, async route => {
await route.fulfill({ json: algoliaFiveHits });
});
} else if (hitsPerPage === 0) {
await page.route(/dsn.algolia.net/, async route => {
await route.fulfill({ json: algoliaNoHits });
});
}
};
test.describe('Search bar', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/learn');
});
test('should display correctly', async ({ page, isMobile }) => {
const searchInput = await getSearchInput({ page, isMobile });
await expect(searchInput).toBeVisible();
await expect(searchInput).toHaveAttribute('placeholder', /Search/i);
await expect(
page.getByRole('button', { name: 'Submit search terms' })
).toBeVisible();
});
test('should return the search results when the user presses Enter', async ({
page,
isMobile
}) => {
test.skip(!haveApiKeys, 'This test requires Algolia API keys');
await mockAlgolia({ page, hitsPerPage: 8 });
await search({ page, isMobile, query: 'article' });
// Wait for the search results to show up
const resultList = page.getByRole('list', { name: 'Search results' });
// Initially, the dropdown contains an `li` with the text "No tutorials found",
// so we need to check the text content to ensure the correct `li` is displayed.
await expect(resultList.getByRole('listitem').first()).toContainText(
/article/i
);
await page.keyboard.press('Enter');
await page.waitForURL(
'https://www.freecodecamp.org/news/search/?query=article'
);
const title = await page.title();
expect(title).toBe('Search - freeCodeCamp.org');
});
test('should return the search results when the user clicks the search button', async ({
page,
isMobile
}) => {
test.skip(!haveApiKeys, 'This test requires Algolia API keys');
await mockAlgolia({ page, hitsPerPage: 8 });
await search({ page, isMobile, query: 'article' });
// Wait for the search results to show up
const resultList = page.getByRole('list', { name: 'Search results' });
// Initially, the dropdown contains an `li` with the text "No tutorials found",
// so we need to check the text content to ensure the correct `li` is displayed.
await expect(resultList.getByRole('listitem').first()).toContainText(
/article/i
);
await page.getByRole('button', { name: 'Submit search terms' }).click();
await page.waitForURL(
'https://www.freecodecamp.org/news/search/?query=article'
);
const title = await page.title();
expect(title).toBe('Search - freeCodeCamp.org');
});
test('should show an empty result list if no results found', async ({
page,
isMobile
}) => {
await mockAlgolia({ page, hitsPerPage: 0 });
await search({ page, isMobile, query: 'test' });
const resultList = page.getByRole('list', { name: 'Search results' });
await expect(resultList.getByRole('listitem')).toHaveCount(1);
await expect(resultList.getByRole('listitem')).toHaveText(
'No tutorials found'
);
});
test('should clear the input and hide the result dropdown when the user clicks the clear button', async ({
page,
isMobile
}) => {
const searchInput = await getSearchInput({ page, isMobile });
await expect(searchInput).toBeVisible();
await searchInput.fill('test');
await page.getByRole('button', { name: 'Clear search terms' }).click();
await expect(searchInput).toHaveValue('');
await expect(
page.getByRole('list', { name: 'Search results' })
).toBeHidden();
});
test('should close the dropdown when the user clicks outside of the search bar', async ({
page,
isMobile
}) => {
const searchInput = await getSearchInput({ page, isMobile });
await expect(searchInput).toBeVisible();
await searchInput.fill('test');
// Wait for the search results to show up
const resultList = page.getByRole('list', { name: 'Search results' });
await expect(resultList).toBeVisible();
await page.getByRole('navigation', { name: 'primary' }).click();
await expect(resultList).toBeHidden();
});
});
test.describe('Search results when viewport height is greater than 768px', () => {
test.use({
viewport: { width: 1600, height: 1200 }
});
test.beforeEach(async ({ page }) => {
await page.goto('/learn');
// Mock Algolia requests to prevent hitting Algolia server unnecessarily.
// Comment out this line if you want to test against the real server.
await mockAlgolia({ page, hitsPerPage: 8 });
});
test('should display 8 items', async ({ page, isMobile }) => {
test.skip(!haveApiKeys, 'This test requires Algolia API keys');
await search({ page, isMobile, query: 'article' });
// Wait for the search results to show up
const results = page.getByRole('list', { name: 'Search results' });
await expect(results.getByRole('listitem')).toHaveCount(9); // 8 results + the footer
});
});
test.describe('Search results when viewport height is equal to 768px', () => {
test.use({
viewport: { width: 1600, height: 768 }
});
test.beforeEach(async ({ page }) => {
await page.goto('/learn');
// Mock Algolia requests to prevent hitting Algolia server unnecessarily.
// Comment out this line if you want to test against the real server.
await mockAlgolia({ page, hitsPerPage: 8 });
});
test('should display 8 items', async ({ page, isMobile }) => {
test.skip(!haveApiKeys, 'This test requires Algolia API keys');
await search({ page, isMobile, query: 'article' });
// Wait for the search results to show up
const results = page.getByRole('list', { name: 'Search results' });
await expect(results.getByRole('listitem')).toHaveCount(9); // 8 results + the footer
});
});
test.describe('Search results when viewport height is less than 768px', () => {
test.use({
viewport: { width: 1600, height: 500 }
});
test.beforeEach(async ({ page }) => {
await page.goto('/learn');
// Mock Algolia requests to prevent hitting Algolia server unnecessarily.
// Comment out this line if you want to test against the real server.
await mockAlgolia({ page, hitsPerPage: 5 });
});
test('should display 5 items', async ({ page, isMobile }) => {
test.skip(!haveApiKeys, 'This test requires Algolia API keys');
await search({ page, isMobile, query: 'article' });
// Wait for the search results to show up
const results = page.getByRole('list', { name: 'Search results' });
await expect(results.getByRole('listitem')).toHaveCount(6); // 5 results + the footer
});
});