Skip to content

Commit f650d40

Browse files
committed
Playwright tests
1 parent 2e5eea0 commit f650d40

File tree

11 files changed

+2039
-493
lines changed

11 files changed

+2039
-493
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
harness
2+
test-results
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @license
3+
* Copyright 2017 Google Inc. All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import {expect, test} from '@playwright/test';
19+
20+
test.beforeEach(async ({page}) => {
21+
await page.goto('/react');
22+
})
23+
24+
25+
test.describe("basic support", () => {
26+
test.describe("no children", () => {
27+
test("can display a Custom Element with no children", async ({page}) => {
28+
await expect(page.locator('#ce-without-children')).toBeAttached();
29+
});
30+
31+
test.describe('with children', () => {
32+
const expectHasChildren = async (wc) => {
33+
await expect(wc.locator('h1')).toHaveText('Test h1');
34+
await expect(wc.locator('p')).toHaveText('Test p');
35+
}
36+
37+
test("can display a Custom Element with children in a Shadow Root", async ({page}) => {
38+
await expectHasChildren(page.locator('#ce-with-children'))
39+
})
40+
41+
test("can display a Custom Element with children in a Shadow Root and pass in Light DOM children", async ({page}) => {
42+
const ce = page.locator('#ce-with-children-renderer');
43+
await expectHasChildren(ce);
44+
await expect(ce).toHaveText(/2/);
45+
})
46+
47+
test('can display a Custom Element with children in the Shadow DOM and handle hiding and showing the element', async ({page}) => {
48+
const ce = page.locator('#ce-with-different-views')
49+
const toggle = page.getByRole('button', {name: /toggle views/i});
50+
51+
await expectHasChildren(ce);
52+
53+
await toggle.click();
54+
await expect(ce).toHaveText(/dummy view/i);
55+
56+
await toggle.click();
57+
await expectHasChildren(ce);
58+
})
59+
})
60+
});
61+
62+
test.describe('attributes and properties', () => {
63+
const getPropOrAttr = async (ce, name) => {
64+
const prop = await (await (await ce.elementHandle()).getProperty(name)).jsonValue()
65+
const attr = ce.getAttribute(name);
66+
return prop ?? attr;
67+
}
68+
69+
test("will pass boolean data as either an attribute or a property", async ({page}) => {
70+
const ce = page.locator('#ce-with-properties');
71+
expect(await getPropOrAttr(ce, 'bool')).toEqual(true)
72+
});
73+
74+
test("will pass numeric data as either an attribute or a property", async ({page}) => {
75+
const ce = page.locator('#ce-with-properties');
76+
expect(parseInt(await getPropOrAttr(ce, 'num'))).toEqual(42)
77+
});
78+
79+
test("will pass string data as either an attribute or a property", async ({page}) => {
80+
const ce = page.locator('#ce-with-properties');
81+
expect(await getPropOrAttr(ce, 'str')).toEqual("React")
82+
});
83+
});
84+
85+
test.describe('events', () => {
86+
test("can imperatively listen to a DOM event dispatched by a Custom Element", async ({page}) => {
87+
const ce = page.locator('#ce-with-imperative-event');
88+
const result = page.locator('#ce-with-imperative-event-handled');
89+
await expect(result).toHaveText(/false/i);
90+
await ce.click();
91+
await expect(result).toHaveText(/true/i);
92+
})
93+
})
94+
95+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "tests",
3+
"version": "1.0.0",
4+
"description": "",
5+
"author": "",
6+
"license": "ISC",
7+
"private": true,
8+
"scripts": {
9+
"serve": "http-server harness",
10+
"test": "playwright test"
11+
},
12+
"devDependencies": {
13+
"@playwright/test": "^1.56.1",
14+
"http-server": "^14.1.1"
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {defineConfig, devices} from '@playwright/test';
2+
3+
export default defineConfig({
4+
use: {
5+
baseURL: 'http://localhost:8080',
6+
},
7+
projects: [
8+
{
9+
name: 'chromium',
10+
use: { ...devices['Desktop Chrome'] },
11+
},
12+
],
13+
webServer: {
14+
command: 'npm run serve',
15+
url: 'http://localhost:8080',
16+
reuseExistingServer: !process.env.CI,
17+
stdout: 'ignore',
18+
stderr: 'pipe'
19+
}
20+
})

libraries/react/package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"scripts": {
3+
"build:test": "wireit",
34
"test": "wireit",
45
"build": "wireit"
56
},
@@ -16,13 +17,20 @@
1617
"karma-firefox-launcher": "2.1.3",
1718
"karma-sourcemap-loader": "0.4.0",
1819
"karma-webpack": "5.0.1",
19-
"webpack": "5.101.0"
20+
"webpack": "5.101.0",
21+
"vite": "^7.2.2"
2022
},
2123
"dependencies": {
2224
"react": "^19",
2325
"react-dom": "^19"
2426
},
27+
"imports": {
28+
"#*": "../__shared__/webcomponents/src/*"
29+
},
2530
"wireit": {
31+
"build:test": {
32+
"command": "vite build"
33+
},
2634
"test": {
2735
"dependencies": [
2836
"../..:karma-plugins"

0 commit comments

Comments
 (0)