Skip to content

Commit fbf4bbe

Browse files
committed
test(shadow-dom): adds shadow dom tests
Adds e2e and unit tests for shadow dom components. The e2e tests check for shadow DOM rendering and shared styles. The unit tests verify shadow DOM elements and style application. Removes old shadow DOM template tests.
1 parent 02ab7a2 commit fbf4bbe

File tree

4 files changed

+164
-186
lines changed

4 files changed

+164
-186
lines changed

__test__/shadow-dom.spec.js

Lines changed: 0 additions & 186 deletions
This file was deleted.

app-min/e2e__if_playwright/app.spec.ext__if_not_plugin

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,29 @@ test.beforeEach(async ({ page }) => {
77
test.describe('MyApp', () => {
88
test('shows message', async ({ page }) => {
99
await page.waitForSelector('my-app', { timeout: 10000 });
10+
11+
// @if shadow-dom
12+
// Check if shadow DOM is rendered
13+
const hasShadowRoot = await page.locator('my-app').evaluate(el => !!el.shadowRoot);
14+
await expect(hasShadowRoot).toBe(true);
15+
16+
// Use Playwright's piercing selector syntax to access shadow DOM content
17+
const shadowText = page.locator('my-app').locator(':scope .shared-shadow-text');
18+
await expect(shadowText).toBeVisible();
19+
20+
// Assert visibility of shared-style elements within shadow DOM
1021
const messageElement = page.locator('my-app').getByText('Hello World!');
1122
await expect(messageElement).toBeVisible();
23+
24+
// Optional: Take a screenshot to guard against visual regressions
25+
await page.screenshot({ path: 'shadow-dom-component.png' });
26+
// @endif
27+
28+
// @if !shadow-dom
29+
const messageElement = page.locator('my-app').getByText('Hello World!');
30+
await expect(messageElement).toBeVisible();
31+
// @endif
32+
1233
await expect(page.locator('my-app')).toContainText('Hello World!');
1334
await expect(page).toHaveTitle(/Aurelia/);
1435
});

app-min/test__if_not_no-unit-tests/my-app.spec.ext

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { MyApp } from '../src/my-app';
55
import { createFixture } from '@aurelia/testing';
66

77
describe('my-app', () => {
8+
// @if !shadow-dom
89
it('should render message', async () => {
910
const { assertText } = await createFixture(
1011
'<my-app></my-app>',
@@ -32,4 +33,57 @@ describe('my-app', () => {
3233
assertText('Hello World!', { compact: true });
3334
// @endif
3435
});
36+
// @endif
37+
38+
// @if shadow-dom
39+
it('should render with Shadow DOM and shared styles', async () => {
40+
const { appHost } = await createFixture(
41+
'<my-app></my-app>',
42+
{},
43+
[MyApp],
44+
).started;
45+
46+
// Get the my-app element from the host
47+
const element = appHost.querySelector('my-app');
48+
if (element === null) {
49+
throw new Error('Expected to find my-app element in host');
50+
}
51+
52+
// Assert that the host element has a shadow root
53+
const shadowRoot = element.shadowRoot;
54+
if (shadowRoot === null) {
55+
throw new Error('Expected shadowRoot to be present, but it was null');
56+
}
57+
58+
// Query inside the shadow root to verify the message text
59+
const messageElement = shadowRoot.querySelector('.message') ||
60+
shadowRoot.querySelector('[class*="text-"]'); // Handle TailwindCSS classes
61+
if (messageElement === null) {
62+
throw new Error('Expected to find message element in shadow root');
63+
}
64+
65+
// Verify the message contains 'Hello World!'
66+
const messageText = messageElement.textContent || '';
67+
if (!messageText.includes('Hello World!')) {
68+
throw new Error(`Expected message to contain 'Hello World!' but got: ${messageText}`);
69+
}
70+
71+
// Verify presence of shared shadow DOM style elements (structure only, not computed styles)
72+
const sharedStyleElement = shadowRoot.querySelector('.shared-shadow-style');
73+
if (sharedStyleElement === null) {
74+
throw new Error('Expected to find .shared-shadow-style element in shadow root');
75+
}
76+
77+
const sharedTextElement = shadowRoot.querySelector('.shared-shadow-text');
78+
if (sharedTextElement === null) {
79+
throw new Error('Expected to find .shared-shadow-text element in shadow root');
80+
}
81+
82+
// Verify the shared text content
83+
const sharedTextContent = sharedTextElement.textContent || '';
84+
if (!sharedTextContent.includes('This content uses shared Shadow DOM styles!')) {
85+
throw new Error(`Expected shared text content but got: ${sharedTextContent}`);
86+
}
87+
});
88+
// @endif
3589
});
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// @if vitest
2+
import { describe, it } from 'vitest';
3+
// @endif
4+
import { MyApp } from '../src/my-app';
5+
import { createFixture } from '@aurelia/testing';
6+
7+
describe('my-app', () => {
8+
// @if !shadow-dom
9+
it('should render message', async () => {
10+
const { assertText } = await createFixture(
11+
'<my-app></my-app>',
12+
{},
13+
[MyApp],
14+
).started;
15+
16+
// @if tailwindcss
17+
// For TailwindCSS templates, just check that the text is present
18+
// The assertText function will throw if no text is found at all
19+
try {
20+
assertText('Hello World!', { compact: true });
21+
} catch (e) {
22+
// If exact match fails, check if the text contains 'Hello World!'
23+
// This handles TailwindCSS templates with additional text
24+
const message = e.message || '';
25+
if (message.includes('Hello World!')) {
26+
// Text is present, test passes
27+
return;
28+
}
29+
throw e; // Re-throw if the text isn't found at all
30+
}
31+
// @endif
32+
// @if !tailwindcss
33+
assertText('Hello World!', { compact: true });
34+
// @endif
35+
});
36+
// @endif
37+
38+
// @if shadow-dom
39+
it('should render with Shadow DOM and shared styles', async () => {
40+
const { appHost } = await createFixture(
41+
'<my-app></my-app>',
42+
{},
43+
[MyApp],
44+
).started;
45+
46+
// Get the my-app element from the host
47+
const element = appHost.querySelector('my-app');
48+
if (element === null) {
49+
throw new Error('Expected to find my-app element in host');
50+
}
51+
52+
// Assert that the host element has a shadow root
53+
const shadowRoot = element.shadowRoot;
54+
if (shadowRoot === null) {
55+
throw new Error('Expected shadowRoot to be present, but it was null');
56+
}
57+
58+
// Query inside the shadow root to verify the message text
59+
const messageElement = shadowRoot.querySelector('.message') ||
60+
shadowRoot.querySelector('[class*="text-"]'); // Handle TailwindCSS classes
61+
if (messageElement === null) {
62+
throw new Error('Expected to find message element in shadow root');
63+
}
64+
65+
// Verify the message contains 'Hello World!'
66+
const messageText = messageElement.textContent || '';
67+
if (!messageText.includes('Hello World!')) {
68+
throw new Error(`Expected message to contain 'Hello World!' but got: ${messageText}`);
69+
}
70+
71+
// Verify presence of shared shadow DOM style elements (structure only, not computed styles)
72+
const sharedStyleElement = shadowRoot.querySelector('.shared-shadow-style');
73+
if (sharedStyleElement === null) {
74+
throw new Error('Expected to find .shared-shadow-style element in shadow root');
75+
}
76+
77+
const sharedTextElement = shadowRoot.querySelector('.shared-shadow-text');
78+
if (sharedTextElement === null) {
79+
throw new Error('Expected to find .shared-shadow-text element in shadow root');
80+
}
81+
82+
// Verify the shared text content
83+
const sharedTextContent = sharedTextElement.textContent || '';
84+
if (!sharedTextContent.includes('This content uses shared Shadow DOM styles!')) {
85+
throw new Error(`Expected shared text content but got: ${sharedTextContent}`);
86+
}
87+
});
88+
// @endif
89+
});

0 commit comments

Comments
 (0)