Skip to content

Commit 239232c

Browse files
committed
test(toast): add e2e test for customizing via parts
1 parent b694e24 commit 239232c

File tree

2 files changed

+135
-2
lines changed

2 files changed

+135
-2
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import { expect } from '@playwright/test';
2+
import { configs, test } from '@utils/test/playwright';
3+
4+
/**
5+
* This behavior does not vary across directions
6+
*/
7+
configs({ directions: ['ltr'] }).forEach(({ config, title }) => {
8+
test.describe(title('toast: custom'), () => {
9+
test('should be able to customize toast wrapper, container, and content using css parts', async ({ page }) => {
10+
await page.setContent(
11+
`
12+
<ion-toast is-open="true" header="Header" message="Hello World"></ion-toast>
13+
14+
<style>
15+
ion-toast::part(wrapper) {
16+
background-color: red;
17+
}
18+
ion-toast::part(container) {
19+
background-color: green;
20+
}
21+
ion-toast::part(content) {
22+
background-color: blue;
23+
}
24+
</style>
25+
`,
26+
config
27+
);
28+
29+
const wrapperColor = await page.locator('ion-toast').evaluate((el: any) => {
30+
const partEl = el.shadowRoot?.querySelector('[part="wrapper"]') as HTMLElement | null;
31+
return partEl ? getComputedStyle(partEl).backgroundColor : null;
32+
});
33+
34+
expect(wrapperColor).toBe('rgb(255, 0, 0)');
35+
36+
const containerColor = await page.locator('ion-toast').evaluate((el: any) => {
37+
const partEl = el.shadowRoot?.querySelector('[part="container"]') as HTMLElement | null;
38+
return partEl ? getComputedStyle(partEl).backgroundColor : null;
39+
});
40+
41+
expect(containerColor).toBe('rgb(0, 128, 0)');
42+
43+
const contentColor = await page.locator('ion-toast').evaluate((el: any) => {
44+
const partEl = el.shadowRoot?.querySelector('[part="content"]') as HTMLElement | null;
45+
return partEl ? getComputedStyle(partEl).backgroundColor : null;
46+
});
47+
48+
expect(contentColor).toBe('rgb(0, 0, 255)');
49+
});
50+
51+
test('should be able to customize toast header and message using css parts', async ({ page }) => {
52+
await page.setContent(
53+
`
54+
<ion-toast is-open="true" header="Header" message="Hello World"></ion-toast>
55+
56+
<style>
57+
ion-toast::part(header) {
58+
color: red;
59+
}
60+
ion-toast::part(message) {
61+
color: green;
62+
}
63+
</style>
64+
`,
65+
config
66+
);
67+
68+
const headerColor = await page.locator('ion-toast').evaluate((el: any) => {
69+
const partEl = el.shadowRoot?.querySelector('[part="header"]') as HTMLElement | null;
70+
return partEl ? getComputedStyle(partEl).color : null;
71+
});
72+
73+
expect(headerColor).toBe('rgb(255, 0, 0)');
74+
75+
const messageColor = await page.locator('ion-toast').evaluate((el: any) => {
76+
const partEl = el.shadowRoot?.querySelector('[part="message"]') as HTMLElement | null;
77+
return partEl ? getComputedStyle(partEl).color : null;
78+
});
79+
80+
expect(messageColor).toBe('rgb(0, 128, 0)');
81+
});
82+
83+
test('should be able to customize toast icon, button, and button cancel using css parts', async ({ page }) => {
84+
await page.setContent(
85+
`
86+
<ion-toast is-open="true" header="Header" message="Hello World" icon="alert"></ion-toast>
87+
88+
<style>
89+
ion-toast::part(icon) {
90+
color: red;
91+
}
92+
ion-toast::part(button) {
93+
color: green;
94+
}
95+
ion-toast::part(button cancel) {
96+
color: blue;
97+
}
98+
</style>
99+
100+
<script>
101+
const toast = document.querySelector('ion-toast');
102+
toast.buttons = [
103+
{ text: 'Cancel', role: 'cancel' },
104+
{ text: 'OK' }
105+
];
106+
</script>
107+
`,
108+
config
109+
);
110+
111+
const iconColor = await page.locator('ion-toast').evaluate((el: any) => {
112+
const partEl = el.shadowRoot?.querySelector('[part="icon"]') as HTMLElement | null;
113+
return partEl ? getComputedStyle(partEl).color : null;
114+
});
115+
116+
expect(iconColor).toBe('rgb(255, 0, 0)');
117+
118+
const buttonColor = await page.locator('ion-toast').evaluate((el: any) => {
119+
const partEl = el.shadowRoot?.querySelector('[part="button"]') as HTMLElement | null;
120+
return partEl ? getComputedStyle(partEl).color : null;
121+
});
122+
123+
expect(buttonColor).toBe('rgb(0, 128, 0)');
124+
125+
const buttonCancelColor = await page.locator('ion-toast').evaluate((el: any) => {
126+
const partEl = el.shadowRoot?.querySelector('[part="button cancel"]') as HTMLElement | null;
127+
return partEl ? getComputedStyle(partEl).color : null;
128+
});
129+
130+
expect(buttonCancelColor).toBe('rgb(0, 0, 255)');
131+
});
132+
});
133+
});

core/src/components/toast/toast.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ import type {
4747
/**
4848
* @virtualProp {"ios" | "md"} mode - The mode determines which platform styles to use.
4949
*
50-
* @part button - Any button element that is displayed inside of the toast.
51-
* @part button cancel - Any button element with role "cancel" that is displayed inside of the toast.
5250
* @part wrapper - The outer wrapper for the toast overlay.
5351
* @part container - Groups the icon, content, and buttons.
5452
* @part content - The live region that contains the header and message.
5553
* @part header - The header text of the toast.
5654
* @part message - The body text of the toast.
5755
* @part icon - The icon that appears next to the toast content.
56+
* @part button - Any button element that is displayed inside of the toast.
57+
* @part button cancel - Any button element with role "cancel" that is displayed inside of the toast.*
5858
*/
5959
@Component({
6060
tag: 'ion-toast',

0 commit comments

Comments
 (0)