Skip to content

Commit e8e5c4e

Browse files
authored
refactor(dark): use palettes through url queries in test pages (#29238)
Issue number: N/A --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> If a dev wants to view a test page in dark mode, they have to manually add the styles. This can lead to a slowdown. Plus they can't use Playwright's `goto` to test both light and dark. In order to test dark mode with Playwright, the dev would need to use `setContent` instead of `goto`. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> Dark mode can be added to any page by appending `palette=dark` to the URL. - The param will be used to add a link tag with the correct palette file. - Playwright will load the correct palette file when a dev uses `goto` and `{ themes: ['dark'] }` ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change: 1. Describe the impact and migration path for existing applications below. 2. Update the BREAKING.md file with the breaking change. 3. Add "BREAKING CHANGE: [...]" to the commit description when merging. See https://github.com/ionic-team/ionic-framework/blob/main/.github/CONTRIBUTING.md#footer for more information. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> I recommend using badge to try this out. It already has a `goto` in the basic tests.
1 parent 2fc81dd commit e8e5c4e

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

core/scripts/testing/scripts.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
document.head.appendChild(style);
1515
}
1616

17+
/**
18+
* The term `palette` is used to as a param to match the
19+
* Ionic docs, plus here is already a `ionic:theme` query being
20+
* used for `md`, `ios`, and `ionic` themes.
21+
*/
22+
const palette = window.location.search.match(/palette=([a-z]+)/);
23+
if (palette && palette[1] !== 'light') {
24+
const linkTag = document.createElement('link');
25+
linkTag.setAttribute('rel', 'stylesheet');
26+
linkTag.setAttribute('type', 'text/css');
27+
linkTag.setAttribute('href', `/css/palettes/${palette[1]}.always.css`);
28+
document.head.appendChild(linkTag);
29+
}
30+
1731
window.Ionic = window.Ionic || {};
1832
window.Ionic.config = window.Ionic.config || {};
1933

core/src/utils/test/playwright/page/utils/goto.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Page, TestInfo } from '@playwright/test';
2-
import type { E2EPageOptions, Mode, Direction } from '@utils/test/playwright';
2+
import type { E2EPageOptions, Mode, Direction, Palette } from '@utils/test/playwright';
33

44
/**
55
* This is an extended version of Playwright's
@@ -28,13 +28,16 @@ configs().forEach(({ config, title }) => {
2828

2929
let mode: Mode;
3030
let direction: Direction;
31+
let palette: Palette;
3132

3233
if (options == undefined) {
3334
mode = testInfo.project.metadata.mode;
3435
direction = testInfo.project.metadata.rtl ? 'rtl' : 'ltr';
36+
palette = testInfo.project.metadata.palette;
3537
} else {
3638
mode = options.mode;
3739
direction = options.direction;
40+
palette = options.palette;
3841
}
3942

4043
const rtlString = direction === 'rtl' ? 'true' : undefined;
@@ -49,13 +52,15 @@ configs().forEach(({ config, title }) => {
4952
const urlToParams = new URLSearchParams(paramsString);
5053
const formattedMode = urlToParams.get('ionic:mode') ?? mode;
5154
const formattedRtl = urlToParams.get('rtl') ?? rtlString;
55+
const formattedPalette = urlToParams.get('palette') ?? palette;
5256
const ionicTesting = urlToParams.get('ionic:_testing') ?? true;
5357

5458
/**
5559
* Pass through other custom query params
5660
*/
5761
urlToParams.delete('ionic:mode');
5862
urlToParams.delete('rtl');
63+
urlToParams.delete('palette');
5964
urlToParams.delete('ionic:_testing');
6065

6166
/**
@@ -66,7 +71,7 @@ configs().forEach(({ config, title }) => {
6671
const remainingQueryParams = decodeURIComponent(urlToParams.toString());
6772
const remainingQueryParamsString = remainingQueryParams == '' ? '' : `&${remainingQueryParams}`;
6873

69-
const formattedUrl = `${splitUrl[0]}?ionic:_testing=${ionicTesting}&ionic:mode=${formattedMode}&rtl=${formattedRtl}${remainingQueryParamsString}`;
74+
const formattedUrl = `${splitUrl[0]}?ionic:_testing=${ionicTesting}&ionic:mode=${formattedMode}&rtl=${formattedRtl}&palette=${formattedPalette}${remainingQueryParamsString}`;
7075

7176
testInfo.annotations.push({
7277
type: 'mode',
@@ -78,6 +83,11 @@ configs().forEach(({ config, title }) => {
7883
description: formattedRtl === 'true' ? 'rtl' : 'ltr',
7984
});
8085

86+
testInfo.annotations.push({
87+
type: 'palette',
88+
description: formattedPalette,
89+
});
90+
8191
const result = await Promise.all([
8292
page.waitForFunction(() => (window as any).testAppLoaded === true, { timeout: 4750 }),
8393
originalFn(formattedUrl, options),

docs/CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Before creating a pull request, please read our requirements that explains the m
144144
3. From here, navigate to one of the component's tests to preview your changes.
145145
4. If a test showing your change doesn't exist, [add a new test or update an existing one](#modifying-tests).
146146
5. To test in RTL mode, once you are in the desired component's test, add `?rtl=true` at the end of the url; for example: `http://localhost:3333/src/components/alert/test/basic?rtl=true`.
147+
6. To test in dark mode, once you are in the desired component's test, add `?palette=dark` at the end of the url; for example: `http://localhost:3333/src/components/alert/test/basic?palette=dark`.
147148

148149
##### Previewing in an external app
149150

0 commit comments

Comments
 (0)