Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions react/e2e/shell.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { expect, test } from '@playwright/test';

const FEEDS = ['news', 'newest', 'show', 'ask', 'jobs'] as const;

// These tests only cover the shell, so the live HN API is blocked to keep them independent of it.
test.beforeEach(async ({ page }) => {
await page.route('https://node-hnapi.herokuapp.com/**', (route) => route.abort());
});

test.describe('app shell routing', () => {
test('unknown URLs keep the shell instead of the boot loader', async ({ page }) => {
await page.goto('/news');
await expect(page.getByRole('banner')).toBeVisible();
await expect(page.locator('.app-loader')).toHaveCSS('opacity', '0');
});

test('skip link moves focus to the header navigation', async ({ page }) => {
await page.goto('/news/1');
await page.keyboard.press('Tab');
await page.keyboard.press('Enter');
await expect(page.locator('#navigation')).toBeFocused();
});

test('redirects / to /news/1', async ({ page }) => {
await page.goto('/');
await expect(page).toHaveURL(/\/news\/1$/);
await expect(page.getByTestId('feed')).toHaveAttribute('data-feed-type', 'news');
});

for (const feed of FEEDS) {
test(`deep link /${feed}/2 resolves the ${feed} feed`, async ({ page }) => {
await page.goto(`/${feed}/2`);
const feedEl = page.getByTestId('feed');
await expect(feedEl).toHaveAttribute('data-feed-type', feed);
await expect(feedEl).toHaveAttribute('data-page', '2');
});
}

test('header navigation updates the URL and active link', async ({ page }) => {
await page.goto('/news/1');
const nav = page.getByRole('navigation');
for (const [label, feed] of [
['new', 'newest'],
['show', 'show'],
['ask', 'ask'],
['jobs', 'jobs'],
] as const) {
await nav.getByRole('link', { name: label, exact: true }).click();
await expect(page).toHaveURL(new RegExp(`/${feed}/1$`));
await expect(nav.getByRole('link', { name: label, exact: true })).toHaveClass(/active/);
await expect(page.getByTestId('feed')).toHaveAttribute('data-feed-type', feed);
}
await page.getByRole('link', { name: 'Logo' }).click();
await expect(page).toHaveURL(/\/news\/1$/);
});

test('item and user routes load', async ({ page }) => {
await page.goto('/item/8863');
await expect(page.getByTestId('item-details')).toHaveAttribute('data-item-id', '8863');
await page.goto('/user/pg');
await expect(page.getByTestId('user-profile')).toHaveAttribute('data-user-id', 'pg');
});
});

test.describe('theming', () => {
test('switches and persists themes from the settings dialog', async ({ page }) => {
await page.goto('/news/1');
const root = page.getByTestId('theme-root');
await expect(root).toHaveClass('default');
const headerBg = () => page.locator('#header').evaluate((el) => getComputedStyle(el).backgroundColor);
expect(await headerBg()).toBe('rgb(185, 43, 39)');

await page.getByRole('button', { name: 'Settings' }).click();
const dialog = page.getByRole('dialog', { name: 'Settings' });
await expect(dialog).toBeVisible();

await dialog.getByRole('radio', { name: 'Night' }).check();
await expect(root).toHaveClass('night');
expect(await headerBg()).toBe('rgb(38, 50, 56)');

await dialog.getByRole('radio', { name: 'Black (AMOLED)' }).check();
await expect(root).toHaveClass('amoledblack');
expect(await headerBg()).toBe('rgb(0, 0, 0)');

await dialog.getByRole('button', { name: 'Close settings' }).click();
await expect(dialog).toBeHidden();

await page.reload();
await expect(root).toHaveClass('amoledblack');
expect(await page.evaluate(() => localStorage.getItem('theme'))).toBe('amoledblack');
});

test('defaults to the night theme when the OS prefers dark mode', async ({ page }) => {
await page.emulateMedia({ colorScheme: 'dark' });
await page.goto('/news/1');
await expect(page.getByTestId('theme-root')).toHaveClass('night');
});
});
6 changes: 0 additions & 6 deletions react/e2e/smoke.spec.ts

This file was deleted.

4 changes: 4 additions & 0 deletions react/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ export default tseslint.config(
'react-refresh/only-export-components': ['warn', { allowConstantExport: true }],
},
},
{
files: ['src/test/**', 'src/**/*.test.{ts,tsx}'],
rules: { 'react-refresh/only-export-components': 'off' },
},
prettier
);
24 changes: 24 additions & 0 deletions react/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,33 @@
<link rel="icon" type="image/png" href="/assets/icons/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="/assets/icons/favicon-16x16.png" sizes="16x16" />
<link rel="apple-touch-icon" sizes="180x180" href="/assets/icons/apple-touch-icon.png" />
<style>
#skip a {
position: absolute;
left: -10000px;
top: auto;
width: 1px;
height: 1px;
overflow: hidden;
}
#skip a:focus {
position: static;
width: auto;
height: auto;
}
</style>
</head>
<body>
<div id="skip"><a href="#navigation">skip to navigation</a></div>
<div id="root"></div>
<div class="app-loader" id="content">
<img class="logo" src="/assets/images/logo.svg" alt="Logo" />
<noscript>
<h4 style="font-family: 'HelveticaNeue-Light', 'Helvetica Neue Light', 'Helvetica Neue', Helvetica">
Sorry, JavaScript needs to be enabled in order to run this application.
</h4>
</noscript>
</div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
Loading
Loading