forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch-bar-optimized.spec.ts
111 lines (92 loc) · 3.05 KB
/
search-bar-optimized.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
import { test, expect, type Page } from '@playwright/test';
import translations from '../client/i18n/locales/english/translations.json';
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');
};
test.describe('Search bar optimized', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/');
});
test('should display correct placeholder', async ({ page, isMobile }) => {
const searchInput = await getSearchInput({ page, isMobile });
await expect(searchInput).toBeVisible();
// Because we're mocking Algolia requests, the placeholder
// should be the default one.
await expect(searchInput).toHaveAttribute(
'placeholder',
translations.search.placeholder.default
);
});
test('should return the search results when the user presses Enter', async ({
context,
page,
isMobile
}) => {
const searchInput = await getSearchInput({ page, isMobile });
await expect(searchInput).toBeVisible();
await searchInput.fill('test');
await page.keyboard.press('Enter');
// Wait for the new page to load.
const newPage = await context.waitForEvent('page');
await newPage.waitForLoadState();
expect(newPage.url()).toBe(
'https://www.freecodecamp.org/news/search/?query=test'
);
});
test('should return the search results when the user clicks the search button', async ({
context,
page,
isMobile
}) => {
const searchInput = await getSearchInput({ page, isMobile });
await expect(searchInput).toBeVisible();
await searchInput.fill('test');
await page
.getByRole('button', { name: translations.icons.magnifier })
.click();
// Wait for the new page to load.
const newPage = await context.waitForEvent('page');
await newPage.waitForLoadState();
expect(newPage.url()).toBe(
'https://www.freecodecamp.org/news/search/?query=test'
);
});
test('should clear the search input 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: translations.icons['input-reset'] })
.click();
await expect(searchInput).toHaveValue('');
});
test('The optimized searchbar component should not render when not on the landing page', async ({
page,
isMobile
}) => {
// This means that the default search bar should be rendered ^.
await page.getByTestId('curriculum-map-button').nth(0).click();
if (isMobile) {
const menuButton = page.getByTestId('header-menu-button');
await expect(menuButton).toBeVisible();
await menuButton.click();
}
await expect(page.getByTestId('header-search')).toBeVisible();
});
});