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