Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(playwright): add tests for editor theme selection logic #54481

Merged
merged 8 commits into from
May 21, 2024
140 changes: 140 additions & 0 deletions e2e/editor.spec.ts
Expand Up @@ -28,3 +28,143 @@ test.describe('Editor Component', () => {
await expect(text).toBeVisible();
});
});

test.describe('Editor theme if the system theme is dark', () => {
test.use({ colorScheme: 'dark' });

test.describe('If the user is signed in', () => {
test.use({ storageState: 'playwright/.auth/certified-user.json' });
huyenltnguyen marked this conversation as resolved.
Show resolved Hide resolved

test('should be in dark mode the user selected theme is dark', async ({
ahmaxed marked this conversation as resolved.
Show resolved Hide resolved
page
}) => {
// Open the nav menu and toggle the theme
await page.getByRole('button', { name: 'Menu' }).click();

const toggle = page.getByRole('button', { name: 'Night Mode' });
await expect(toggle).toBeVisible();
const isDarkMode = await toggle.getAttribute('aria-pressed');

if (isDarkMode === 'false') {
const listItem = page.getByRole('listitem').filter({ has: toggle });

// The button's click event is intercepted by the `li`,
// so we need to click on the `li` to trigger the theme change action.
await listItem.click();

// Ensure that the action is completed before checking the editor.
await expect(
page.getByText('We have updated your theme')
).toBeVisible();
}

const editor = page.locator("div[role='code'].monaco-editor");
await expect(editor).toHaveClass(/vs-dark/);
});

test('should be in light mode if the user selected theme is light', async ({
page
}) => {
// Open the nav menu and toggle the theme
await page.getByRole('button', { name: 'Menu' }).click();

const toggle = page.getByRole('button', { name: 'Night Mode' });
await expect(toggle).toBeVisible();
const isDarkMode = await toggle.getAttribute('aria-pressed');

if (isDarkMode === 'true') {
const listItem = page.getByRole('listitem').filter({ has: toggle });

// The button's click event is intercepted by the `li`,
// so we need to click on the `li` to trigger the theme change action.
await listItem.click();

// Ensure that the action is completed before checking the editor.
await expect(
page.getByText('We have updated your theme')
).toBeVisible();
}

const editor = page.locator("div[role='code'].monaco-editor");
await expect(editor).toHaveClass(/vs(?!\w)/);
});
});

test.describe('If the user is signed out', () => {
test.use({ storageState: { cookies: [], origins: [] } });

test('should be in dark mode', async ({ page }) => {
const editor = page.locator("div[role='code'].monaco-editor");
await expect(editor).toHaveClass(/vs-dark/);
});
});
});

test.describe('Editor theme if the system theme is light', () => {
test.use({ colorScheme: 'light' });

test.describe('If the user is signed in', () => {
test.use({ storageState: 'playwright/.auth/certified-user.json' });

test('should be in dark mode the user selected theme is dark', async ({
ahmaxed marked this conversation as resolved.
Show resolved Hide resolved
page
}) => {
await page.getByRole('button', { name: 'Menu' }).click();

const toggle = page.getByRole('button', { name: 'Night Mode' });
await expect(toggle).toBeVisible();
const isDarkMode = await toggle.getAttribute('aria-pressed');

if (isDarkMode === 'false') {
const listItem = page.getByRole('listitem').filter({ has: toggle });

// The button's click event is intercepted by the `li`,
// so we need to click on the `li` to trigger the theme change action.
await listItem.click();

// Ensure that the action is completed before checking the editor.
await expect(
page.getByText('We have updated your theme')
).toBeVisible();
}

const editor = page.locator("div[role='code'].monaco-editor");
await expect(editor).toHaveClass(/vs-dark/);
});

test('should be in light mode if the user selected theme is light', async ({
page
}) => {
await page.getByRole('button', { name: 'Menu' }).click();

const toggle = page.getByRole('button', { name: 'Night Mode' });
await expect(toggle).toBeVisible();
const isDarkMode = await toggle.getAttribute('aria-pressed');

if (isDarkMode === 'true') {
const listItem = page.getByRole('listitem').filter({ has: toggle });

// The button's click event is intercepted by the `li`,
// so we need to click on the `li` to trigger the theme change action.
await listItem.click();

// Ensure that the action is completed before checking the editor.
await expect(
page.getByText('We have updated your theme')
).toBeVisible();
}

const editor = page.locator("div[role='code'].monaco-editor");
await expect(editor).toHaveClass(/vs(?!\w)/);
});
});

test.describe('If the user is signed out', () => {
test.use({ storageState: { cookies: [], origins: [] } });

test('should be in light mode', async ({ page }) => {
const editor = page.locator("div[role='code'].monaco-editor");
await expect(editor).toHaveClass(/vs(?!\w)/);
});
});
});