39 lines
1.7 KiB
TypeScript
39 lines
1.7 KiB
TypeScript
import { test, expect } from '@playwright/test';
|
|
|
|
test.describe('深色模式', () => {
|
|
test('没有手动选择时跟随系统', async ({ page }) => {
|
|
await page.emulateMedia({ colorScheme: 'dark' });
|
|
await page.goto('/login');
|
|
await expect(page.locator('html')).toHaveClass(/dark/);
|
|
const solid = await page.getByRole('button', { name: '进入系统' }).evaluate((node) => {
|
|
const style = getComputedStyle(node);
|
|
return { background: style.backgroundColor, color: style.color };
|
|
});
|
|
expect(solid.background).toBe('rgb(127, 216, 180)');
|
|
expect(solid.color).toBe('rgb(17, 26, 22)');
|
|
await page.emulateMedia({ colorScheme: 'light' });
|
|
await expect(page.locator('html')).not.toHaveClass(/dark/);
|
|
});
|
|
|
|
test('手动切换覆盖系统并在刷新后保持', async ({ page }) => {
|
|
await page.emulateMedia({ colorScheme: 'dark' });
|
|
await page.goto('/login');
|
|
await expect(page.locator('html')).toHaveClass(/dark/);
|
|
await page.getByRole('button', { name: '切到浅色' }).click();
|
|
await expect(page.locator('html')).not.toHaveClass(/dark/);
|
|
expect(await page.evaluate(() => localStorage.getItem('theme'))).toBe('light');
|
|
await page.reload();
|
|
await expect(page.locator('html')).not.toHaveClass(/dark/);
|
|
await expect(page.getByRole('button', { name: '切到深色' })).toBeVisible();
|
|
});
|
|
|
|
test('手动选择后不再跟随系统变化', async ({ page }) => {
|
|
await page.emulateMedia({ colorScheme: 'light' });
|
|
await page.goto('/login');
|
|
await page.getByRole('button', { name: '切到深色' }).click();
|
|
await expect(page.locator('html')).toHaveClass(/dark/);
|
|
await page.emulateMedia({ colorScheme: 'light' });
|
|
await expect(page.locator('html')).toHaveClass(/dark/);
|
|
});
|
|
});
|