|
1 | 1 | import { test, expect } from '@playwright/test'; |
2 | 2 |
|
3 | | -const apps = [ |
4 | | - { |
5 | | - name: 'App 1', |
6 | | - header: 'Host Application - React Version', |
7 | | - url: 'http://localhost:3001', |
8 | | - }, |
9 | | - { |
10 | | - name: 'App 2', |
11 | | - header: 'Remote Application - React Version', |
12 | | - url: 'http://localhost:3002', |
13 | | - }, |
14 | | -]; |
| 3 | +const hostUrl = 'http://localhost:3001'; |
| 4 | +const remoteUrl = 'http://localhost:3002'; |
15 | 5 |
|
16 | 6 | test.describe('CSS isolation example', () => { |
17 | | - for (const { name, header, url } of apps) { |
18 | | - test(`${name} renders expected headings`, async ({ page }) => { |
19 | | - await page.goto(url); |
| 7 | + test.describe('host application', () => { |
| 8 | + test.beforeEach(async ({ page }) => { |
| 9 | + await page.goto(hostUrl); |
| 10 | + }); |
20 | 11 |
|
21 | | - const heading = page.getByRole('heading', { level: 1, name: new RegExp(header) }); |
22 | | - await expect(heading).toBeVisible(); |
23 | | - await expect(page.getByRole('heading', { level: 2, name })).toHaveText(name); |
| 12 | + test('renders host headings', async ({ page }) => { |
| 13 | + await expect( |
| 14 | + page.getByRole('heading', { level: 1, name: /Host Application - React Version/ }), |
| 15 | + ).toBeVisible(); |
| 16 | + await expect(page.getByRole('heading', { level: 2, name: 'App 1' })).toBeVisible(); |
24 | 17 | }); |
25 | | - } |
26 | 18 |
|
27 | | - test('host renders remote button inside shadow DOM', async ({ page }) => { |
28 | | - await page.goto('http://localhost:3001'); |
| 19 | + test('mounts the remote inside a shadow DOM', async ({ page }) => { |
| 20 | + const remoteMount = page.locator('#parent'); |
| 21 | + await expect(remoteMount).toBeVisible(); |
| 22 | + |
| 23 | + const hasShadowRoot = await remoteMount.evaluate(element => Boolean(element.shadowRoot)); |
| 24 | + expect(hasShadowRoot).toBe(true); |
| 25 | + |
| 26 | + const remoteHeading = remoteMount.getByRole('heading', { |
| 27 | + level: 1, |
| 28 | + name: /Remote Application - React Version/, |
| 29 | + }); |
| 30 | + const remoteButton = remoteMount.getByRole('button', { name: 'Make Everything Yellow' }); |
29 | 31 |
|
30 | | - const buttonTextHandle = await page.waitForFunction(() => { |
31 | | - const host = document.querySelector('#parent'); |
| 32 | + await expect(remoteHeading).toBeVisible(); |
| 33 | + await expect(remoteButton).toBeVisible(); |
32 | 34 |
|
33 | | - return host?.shadowRoot?.querySelector('button')?.textContent?.trim() ?? null; |
| 35 | + await expect( |
| 36 | + page.getByRole('heading', { level: 1, name: /Host Application - React Version/ }), |
| 37 | + ).toHaveCSS('font-style', 'italic'); |
34 | 38 | }); |
35 | 39 |
|
36 | | - expect(await buttonTextHandle.jsonValue()).toBe('Make Everything Yellow'); |
| 40 | + test('retains host styles after interacting with the remote', async ({ page }) => { |
| 41 | + const remoteMount = page.locator('#parent'); |
| 42 | + const remoteButton = remoteMount.getByRole('button', { name: 'Make Everything Yellow' }); |
| 43 | + |
| 44 | + await remoteButton.click(); |
| 45 | + |
| 46 | + await expect(remoteButton).toBeVisible(); |
| 47 | + await expect(remoteMount.getByRole('heading', { level: 2, name: 'App 2' })).toBeVisible(); |
| 48 | + await expect( |
| 49 | + page.getByRole('heading', { level: 1, name: /Host Application - React Version/ }), |
| 50 | + ).toHaveCSS('font-style', 'italic'); |
| 51 | + }); |
37 | 52 | }); |
38 | 53 |
|
39 | | - test.describe('standalone remote', () => { |
40 | | - test('button applies yellow styles after click', async ({ page }) => { |
41 | | - await page.goto('http://localhost:3002'); |
| 54 | + test.describe('standalone remote application', () => { |
| 55 | + test.beforeEach(async ({ page }) => { |
| 56 | + await page.goto(remoteUrl); |
| 57 | + }); |
42 | 58 |
|
43 | | - const button = page.getByRole('button', { name: 'Make Everything Yellow' }); |
44 | | - await expect(button).toBeVisible(); |
| 59 | + test('renders remote headings and button', async ({ page }) => { |
| 60 | + await expect( |
| 61 | + page.getByRole('heading', { level: 1, name: /Remote Application - React Version/ }), |
| 62 | + ).toBeVisible(); |
| 63 | + await expect(page.getByRole('heading', { level: 2, name: 'App 2' })).toBeVisible(); |
| 64 | + await expect(page.getByRole('button', { name: 'Make Everything Yellow' })).toBeVisible(); |
| 65 | + }); |
45 | 66 |
|
| 67 | + test('applies yellow styles after clicking the button', async ({ page }) => { |
| 68 | + const button = page.getByRole('button', { name: 'Make Everything Yellow' }); |
46 | 69 | await button.click(); |
47 | 70 |
|
| 71 | + await expect(page.locator('#root')).toHaveCSS('background-color', 'rgb(255, 255, 0)'); |
48 | 72 | await expect(button).toHaveCSS('background-color', 'rgb(255, 255, 0)'); |
49 | 73 | }); |
50 | 74 | }); |
|
0 commit comments