-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathdark-mode.spec.ts
More file actions
128 lines (112 loc) · 5.5 KB
/
dark-mode.spec.ts
File metadata and controls
128 lines (112 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { expect, test } from '@playwright/test';
import { ThemePage } from '../../models/theme.page';
import { addPageAnnotationBeforeEach, performLoginIfRequired, waitForZeppelinReady, PAGES } from '../../utils';
test.describe('Dark Mode Theme Switching', () => {
addPageAnnotationBeforeEach(PAGES.SHARE.THEME_TOGGLE);
let themePage: ThemePage;
test.beforeEach(async ({ page }) => {
themePage = new ThemePage(page);
await page.goto('/');
await waitForZeppelinReady(page);
// Handle authentication if shiro.ini exists
await performLoginIfRequired(page);
// Ensure a clean localStorage for each test
await themePage.clearLocalStorage();
});
test('Scenario: User can switch to dark mode and persistence is maintained', async ({ page, browserName }) => {
// GIVEN: User is on the main page, which starts in 'system' mode by default (localStorage cleared).
await test.step('GIVEN the page starts in system mode', async () => {
await themePage.assertSystemTheme(); // Robot icon for system theme
});
// WHEN: Explicitly set theme to light mode for the rest of the test.
await test.step('WHEN the user explicitly sets theme to light mode', async () => {
await themePage.setThemeInLocalStorage('light');
await page.waitForTimeout(500);
if (browserName === 'webkit') {
const currentUrl = page.url();
await page.goto(currentUrl, { waitUntil: 'load' });
} else {
page.reload();
}
await waitForZeppelinReady(page);
await themePage.assertLightTheme(); // Now it should be light mode with sun icon
});
// WHEN: User switches to dark mode by setting localStorage and reloading.
await test.step('WHEN the user explicitly sets theme to dark mode', async () => {
await themePage.setThemeInLocalStorage('dark');
await page.waitForTimeout(500);
if (browserName === 'webkit') {
const currentUrl = page.url();
await page.goto(currentUrl, { waitUntil: 'load' });
} else {
page.reload();
}
await waitForZeppelinReady(page);
await themePage.assertDarkTheme();
});
// AND: User clicks the toggle again to switch back to light mode.
await test.step('AND the user clicks the toggle to switch back to light mode', async () => {
await themePage.toggleTheme();
});
// THEN: The theme switches to system mode.
await test.step('THEN the theme switches to system mode', async () => {
await themePage.assertSystemTheme();
});
});
test('Scenario: System Theme and Local Storage Interaction', async ({ page, context }) => {
// Ensure localStorage is clear for each sub-scenario
await themePage.clearLocalStorage();
await test.step('GIVEN: No localStorage, System preference is Light', async () => {
await page.emulateMedia({ colorScheme: 'light' });
await page.goto('/');
await waitForZeppelinReady(page);
// When no explicit theme is set, it defaults to 'system' mode
// Even in system mode with light preference, the icon should be robot
await expect(themePage.rootElement).toHaveClass(/light/);
await expect(themePage.rootElement).toHaveAttribute('data-theme', 'light');
await themePage.assertSystemTheme(); // Should show robot icon
});
await test.step('GIVEN: No localStorage, System preference is Dark (initial system state)', async () => {
await themePage.setThemeInLocalStorage('system');
await page.goto('/');
await waitForZeppelinReady(page);
await themePage.assertSystemTheme(); // Robot icon for system theme
});
await test.step("GIVEN: localStorage is 'dark', System preference is Light", async () => {
await themePage.setThemeInLocalStorage('dark');
await page.emulateMedia({ colorScheme: 'light' });
await page.goto('/');
await waitForZeppelinReady(page);
await themePage.assertDarkTheme(); // localStorage should override system
});
await test.step("GIVEN: localStorage is 'system', THEN: Emulate system preference change to Light", async () => {
await themePage.setThemeInLocalStorage('system');
await page.emulateMedia({ colorScheme: 'light' });
await page.goto('/');
await waitForZeppelinReady(page);
await expect(themePage.rootElement).toHaveClass(/light/);
await expect(themePage.rootElement).toHaveAttribute('data-theme', 'light');
await themePage.assertSystemTheme(); // Robot icon for system theme
});
await test.step("GIVEN: localStorage is 'system', THEN: Emulate system preference change to Dark", async () => {
await themePage.setThemeInLocalStorage('system');
await page.emulateMedia({ colorScheme: 'dark' });
await page.goto('/');
await waitForZeppelinReady(page);
await expect(themePage.rootElement).toHaveClass(/dark/);
await expect(themePage.rootElement).toHaveAttribute('data-theme', 'dark');
await themePage.assertSystemTheme(); // Robot icon for system theme
});
});
});