diff --git a/e2e/specs/themes.e2e.ts b/e2e/specs/themes.e2e.ts new file mode 100644 index 00000000..8d1a943f --- /dev/null +++ b/e2e/specs/themes.e2e.ts @@ -0,0 +1,82 @@ +import { openEditCardsDialog } from '../utils/edit-cards'; +import { enterEditMode, openHarness } from '../utils/harness'; +import { type Page, expect, test } from '@playwright/test'; + +const NON_DEFAULT_HORIZON_THEMES = [ + 'sap_horizon_dark', + 'sap_horizon_hcb', + 'sap_horizon_hcw', +] as const; + +type OpenUI5ThemingStub = { + __mfpSwitchTheme: (theme: string) => void; +}; + +async function stubOpenUI5(page: Page): Promise { + await page.addInitScript(() => { + let currentTheme = 'sap_horizon'; + const appliedHandlers: (() => void)[] = []; + const modules: Record = { + 'sap/ui/core/Theming': { + getTheme: () => currentTheme, + attachApplied: (handler: () => void) => appliedHandlers.push(handler), + }, + 'sap/base/i18n/Localization': { + getLanguageTag: () => 'en', + attachChange: () => undefined, + }, + }; + (window as unknown as { sap: unknown }).sap = { + ui: { + require: (deps: string[], callback: (module: unknown) => void) => + callback(modules[deps[0]]), + }, + }; + (window as unknown as OpenUI5ThemingStub).__mfpSwitchTheme = (theme) => { + currentTheme = theme; + appliedHandlers.forEach((handler) => handler()); + }; + }); +} + +function sapBackgroundColor(page: Page): Promise { + return page.evaluate(() => + getComputedStyle(document.documentElement).getPropertyValue( + '--sapBackgroundColor', + ), + ); +} + +test.describe('Themes', () => { + for (const theme of NON_DEFAULT_HORIZON_THEMES) { + test(`switching to ${theme} registers the theme for every UI5 package`, async ({ + page, + }) => { + const themeErrors: string[] = []; + page.on('console', (message) => { + if ( + message.type() === 'error' && + message.text().includes('not registered for package') + ) { + themeErrors.push(message.text()); + } + }); + await stubOpenUI5(page); + await openHarness(page); + const defaultBackground = await sapBackgroundColor(page); + + await page.evaluate( + (themeName) => + (window as unknown as OpenUI5ThemingStub).__mfpSwitchTheme(themeName), + theme, + ); + await expect + .poll(() => sapBackgroundColor(page)) + .not.toBe(defaultBackground); + await enterEditMode(page); + await openEditCardsDialog(page); + + expect(themeErrors).toEqual([]); + }); + } +}); diff --git a/projects/webcomponents-dashboard/main.spec.ts b/projects/webcomponents-dashboard/main.spec.ts index 897a4c33..ca966760 100644 --- a/projects/webcomponents-dashboard/main.spec.ts +++ b/projects/webcomponents-dashboard/main.spec.ts @@ -16,6 +16,7 @@ vi.mock('@ui5/webcomponents-base/dist/config/Language.js', () => ({ setLanguage, })); vi.mock('@ui5/webcomponents-theming/dist/Assets.js', () => ({})); +vi.mock('@ui5/webcomponents/dist/generated/json-imports/Themes.js', () => ({})); // The real declarative-ui barrel (which main.ts imports) pulls in the dashboard // component, which imports gridstack; gridstack fails to resolve under jsdom. diff --git a/projects/webcomponents-dashboard/main.ts b/projects/webcomponents-dashboard/main.ts index 75feb9aa..b96f2444 100644 --- a/projects/webcomponents-dashboard/main.ts +++ b/projects/webcomponents-dashboard/main.ts @@ -8,6 +8,7 @@ import { ignoreCustomElements } from '@ui5/webcomponents-base/dist/IgnoreCustomE import { setLanguage } from '@ui5/webcomponents-base/dist/config/Language.js'; import { setTheme } from '@ui5/webcomponents-base/dist/config/Theme.js'; import '@ui5/webcomponents-theming/dist/Assets.js'; +import '@ui5/webcomponents/dist/generated/json-imports/Themes.js'; ignoreCustomElements('mfp'); diff --git a/projects/webcomponents/main.spec.ts b/projects/webcomponents/main.spec.ts index d33c563b..24c17a03 100644 --- a/projects/webcomponents/main.spec.ts +++ b/projects/webcomponents/main.spec.ts @@ -17,6 +17,13 @@ vi.mock('@ui5/webcomponents-base/dist/IgnoreCustomElements.js', () => ({ ignoreCustomElements, })); +vi.mock('@ui5/webcomponents-theming/dist/Assets.js', () => ({})); +vi.mock('@ui5/webcomponents/dist/generated/json-imports/Themes.js', () => ({})); +vi.mock( + '@ui5/webcomponents-fiori/dist/generated/json-imports/Themes.js', + () => ({}), +); + // gridstack fails to resolve under jsdom; the real declarative-ui barrel pulls // it in transitively via the dashboard component. Stub it as the dashboard // spec does so the barrel can load. diff --git a/projects/webcomponents/main.ts b/projects/webcomponents/main.ts index ab63a8b2..8b55cfad 100644 --- a/projects/webcomponents/main.ts +++ b/projects/webcomponents/main.ts @@ -12,6 +12,9 @@ import { ResourceFormPatch, } from '@openmfp/webcomponents/declarative-ui'; import { ignoreCustomElements } from '@ui5/webcomponents-base/dist/IgnoreCustomElements.js'; +import '@ui5/webcomponents-fiori/dist/generated/json-imports/Themes.js'; +import '@ui5/webcomponents-theming/dist/Assets.js'; +import '@ui5/webcomponents/dist/generated/json-imports/Themes.js'; ignoreCustomElements('mfp');