|
| 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 | +}); |
0 commit comments