mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-20 12:03:11 -04:00
test(e2e): refactor search.spec.ts (#55109)
This commit is contained in:
93
e2e/search-bar-optimized.spec.ts
Normal file
93
e2e/search-bar-optimized.spec.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
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();
|
||||
await expect(searchInput).toHaveAttribute(
|
||||
'placeholder',
|
||||
translations.search.placeholder
|
||||
);
|
||||
});
|
||||
|
||||
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('');
|
||||
});
|
||||
});
|
||||
@@ -1,152 +0,0 @@
|
||||
import { test, expect } from '@playwright/test';
|
||||
import translations from '../client/i18n/locales/english/translations.json';
|
||||
|
||||
test.describe('Search', () => {
|
||||
test.beforeEach(async ({ page }) => {
|
||||
await page.goto('/');
|
||||
});
|
||||
|
||||
test('should display correct placeholder', async ({ page, isMobile }) => {
|
||||
if (isMobile) {
|
||||
const menuButton = page.getByRole('button', {
|
||||
name: translations.buttons.menu
|
||||
});
|
||||
await expect(menuButton).toBeVisible();
|
||||
await menuButton.click();
|
||||
|
||||
const searchInput = page.getByLabel(translations.search.label);
|
||||
|
||||
await expect(searchInput).toBeVisible();
|
||||
await expect(searchInput).toHaveAttribute(
|
||||
'placeholder',
|
||||
'Search 9,000+ tutorials'
|
||||
);
|
||||
} else {
|
||||
const searchInput = page.getByLabel(translations.search.label);
|
||||
|
||||
await expect(searchInput).toBeVisible();
|
||||
await expect(searchInput).toHaveAttribute(
|
||||
'placeholder',
|
||||
'Search 10,700+ tutorials'
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('should return the search results when the user presses Enter', async ({
|
||||
context,
|
||||
page,
|
||||
isMobile
|
||||
}) => {
|
||||
if (isMobile) {
|
||||
const menuButton = page.getByRole('button', {
|
||||
name: translations.buttons.menu
|
||||
});
|
||||
await menuButton.click();
|
||||
|
||||
const searchInput = page.getByLabel(translations.search.label);
|
||||
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'
|
||||
);
|
||||
} else {
|
||||
const searchInput = page.getByLabel(translations.search.label);
|
||||
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
|
||||
}) => {
|
||||
if (isMobile) {
|
||||
const menuButton = page.getByRole('button', {
|
||||
name: translations.buttons.menu
|
||||
});
|
||||
await menuButton.click();
|
||||
|
||||
const searchInput = page.getByLabel(translations.search.label);
|
||||
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'
|
||||
);
|
||||
} else {
|
||||
const searchInput = page.getByLabel(translations.search.label);
|
||||
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
|
||||
}) => {
|
||||
if (isMobile) {
|
||||
const menuButton = page.getByRole('button', {
|
||||
name: translations.buttons.menu
|
||||
});
|
||||
await menuButton.click();
|
||||
|
||||
const searchInput = page.getByLabel(translations.search.label);
|
||||
await expect(searchInput).toBeVisible();
|
||||
|
||||
await searchInput.fill('test');
|
||||
await page
|
||||
.getByRole('button', { name: translations.icons['input-reset'] })
|
||||
.click();
|
||||
|
||||
await expect(searchInput).toHaveValue('');
|
||||
} else {
|
||||
const searchInput = page.getByLabel(translations.search.label);
|
||||
await expect(searchInput).toBeVisible();
|
||||
|
||||
await searchInput.fill('test');
|
||||
await page
|
||||
.getByRole('button', { name: translations.icons['input-reset'] })
|
||||
.click();
|
||||
|
||||
await expect(searchInput).toHaveValue('');
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user