|
| 1 | +import { importLocale } from '@node-core/website-i18n'; |
| 2 | +import { test, expect, type Page } from '@playwright/test'; |
| 3 | + |
| 4 | +const englishLocale = await importLocale('en'); |
| 5 | + |
| 6 | +// TODO(@avivkeller): It would be ideal for all the Test IDs to not exist in the |
| 7 | +// ui-components package, and instead be passed as props. |
| 8 | +const locators = { |
| 9 | + // Navigation elements |
| 10 | + mobileMenuToggleName: |
| 11 | + englishLocale.components.containers.navBar.controls.toggle, |
| 12 | + navLinksLocator: `[aria-label="${englishLocale.components.containers.navBar.controls.toggle}"] + div`, |
| 13 | + // Global UI controls |
| 14 | + languageDropdownName: englishLocale.components.common.languageDropdown.label, |
| 15 | + themeToggleName: englishLocale.components.common.themeToggle.label, |
| 16 | + |
| 17 | + // Search components (from Orama library) |
| 18 | + searchButtonTag: 'orama-button', |
| 19 | + searchInputTag: 'orama-input', |
| 20 | + searchResultsTag: 'orama-search-results', |
| 21 | +}; |
| 22 | + |
| 23 | +const getTheme = (page: Page) => |
| 24 | + page.evaluate(() => document.documentElement.dataset.theme); |
| 25 | + |
| 26 | +const openLanguageMenu = async (page: Page) => { |
| 27 | + const button = page.getByRole('button', { |
| 28 | + name: locators.languageDropdownName, |
| 29 | + }); |
| 30 | + const selector = `[aria-labelledby=${await button.getAttribute('id')}]`; |
| 31 | + await button.click(); |
| 32 | + |
| 33 | + await page.waitForSelector(selector); |
| 34 | + return page.locator(selector); |
| 35 | +}; |
| 36 | + |
| 37 | +const verifyTranslation = async ( |
| 38 | + page: Page, |
| 39 | + locale: string | Record<string, unknown> |
| 40 | +) => { |
| 41 | + // Load locale data if string code provided (e.g., 'es', 'fr') |
| 42 | + const localeData = |
| 43 | + typeof locale === 'string' ? await importLocale(locale) : locale; |
| 44 | + |
| 45 | + // Get navigation links and expected translations |
| 46 | + const links = await page |
| 47 | + .locator(locators.navLinksLocator) |
| 48 | + .locator('a > span') |
| 49 | + .all(); |
| 50 | + const expectedTexts = Object.values( |
| 51 | + localeData.components.containers.navBar.links |
| 52 | + ); |
| 53 | + |
| 54 | + // Verify each navigation link text matches an expected translation |
| 55 | + for (const link of links) { |
| 56 | + const linkText = await link.textContent(); |
| 57 | + expect(expectedTexts).toContain(linkText!.trim()); |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +test.describe('Node.js Website', () => { |
| 62 | + // Start each test from the English homepage |
| 63 | + test.beforeEach(async ({ page }) => { |
| 64 | + await page.goto('/en'); |
| 65 | + }); |
| 66 | + |
| 67 | + test.describe('Theme', () => { |
| 68 | + test('should toggle between light/dark themes', async ({ page }) => { |
| 69 | + const themeToggle = page.getByRole('button', { |
| 70 | + name: locators.themeToggleName, |
| 71 | + }); |
| 72 | + await expect(themeToggle).toBeVisible(); |
| 73 | + |
| 74 | + const initialTheme = await getTheme(page); |
| 75 | + await themeToggle.click(); |
| 76 | + |
| 77 | + const newTheme = await getTheme(page); |
| 78 | + expect(newTheme).not.toEqual(initialTheme); |
| 79 | + expect(['light', 'dark']).toContain(newTheme); |
| 80 | + }); |
| 81 | + |
| 82 | + test('should persist theme across page navigation', async ({ page }) => { |
| 83 | + const themeToggle = page.getByRole('button', { |
| 84 | + name: locators.themeToggleName, |
| 85 | + }); |
| 86 | + await themeToggle.click(); |
| 87 | + const selectedTheme = await getTheme(page); |
| 88 | + |
| 89 | + await page.reload(); |
| 90 | + expect(await getTheme(page)).toBe(selectedTheme); |
| 91 | + }); |
| 92 | + |
| 93 | + test('should respect system preference initially', async ({ browser }) => { |
| 94 | + const context = await browser.newContext({ colorScheme: 'dark' }); |
| 95 | + const page = await context.newPage(); |
| 96 | + |
| 97 | + await page.goto('/en'); |
| 98 | + expect(await getTheme(page)).toBe('dark'); |
| 99 | + |
| 100 | + await context.close(); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + test.describe('Language', () => { |
| 105 | + test('should correctly translate UI elements according to language files', async ({ |
| 106 | + page, |
| 107 | + }) => { |
| 108 | + await verifyTranslation(page, englishLocale); |
| 109 | + |
| 110 | + // Change to Spanish and verify translations |
| 111 | + const menu = await openLanguageMenu(page); |
| 112 | + await menu.getByText(/español/i).click(); |
| 113 | + await page.waitForURL(/\/es$/); |
| 114 | + |
| 115 | + await verifyTranslation(page, 'es'); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + test.describe('Search', () => { |
| 120 | + test('should show and operate search functionality', async ({ page }) => { |
| 121 | + // Open search dialog |
| 122 | + await page.locator(locators.searchButtonTag).click(); |
| 123 | + |
| 124 | + // Verify search input is visible and enter a search term |
| 125 | + const searchInput = page.locator(locators.searchInputTag); |
| 126 | + await expect(searchInput).toBeVisible(); |
| 127 | + await searchInput.pressSequentially('express'); |
| 128 | + |
| 129 | + // Verify search results appear |
| 130 | + const searchResults = page.locator(locators.searchResultsTag); |
| 131 | + await expect(searchResults).toBeVisible(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + test.describe('Navigation', () => { |
| 136 | + test('should have functioning mobile menu on small screens', async ({ |
| 137 | + page, |
| 138 | + }) => { |
| 139 | + // Set mobile viewport size |
| 140 | + await page.setViewportSize({ width: 375, height: 667 }); |
| 141 | + |
| 142 | + // Locate mobile menu toggle button and verify it's visible |
| 143 | + const mobileToggle = page.getByRole('button', { |
| 144 | + name: locators.mobileMenuToggleName, |
| 145 | + }); |
| 146 | + await expect(mobileToggle).toBeVisible(); |
| 147 | + |
| 148 | + const navLinks = page.locator(locators.navLinksLocator); |
| 149 | + |
| 150 | + // Toggle menu open and verify it's visible |
| 151 | + await mobileToggle.click(); |
| 152 | + await expect(navLinks.first()).toBeVisible(); |
| 153 | + |
| 154 | + // Toggle menu closed and verify it's hidden |
| 155 | + await mobileToggle.click(); |
| 156 | + await expect(navLinks.first()).not.toBeVisible(); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments