|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file ToastViewport.hostMove.interaction.browser.spec.ts |
| 5 | + * @input The AppToastOverDialog story, recorded frame by frame |
| 6 | + * @output Tests that a toast plays its entrance once. A Dialog that opens or |
| 7 | + * closes over it moves the viewport, and the toast does not enter again |
| 8 | + * @position Testing; validates ToastViewport.tsx and Toast.tsx in a real |
| 9 | + * browser |
| 10 | + */ |
| 11 | + |
| 12 | +import {expect, test, type Page} from '@astryxdesign/a11y-spec/playwright'; |
| 13 | +import { |
| 14 | + openStory, |
| 15 | + waitForRest, |
| 16 | + withStorybook, |
| 17 | +} from '@astryxdesign/interaction-harness'; |
| 18 | + |
| 19 | +const storybook = withStorybook(test); |
| 20 | + |
| 21 | +const STORY = 'core-toast--app-toast-over-dialog'; |
| 22 | +const MESSAGE = 'Toast from inside the dialog!'; |
| 23 | + |
| 24 | +/** The toast's card on one frame. */ |
| 25 | +interface Frame { |
| 26 | + opacity: number; |
| 27 | + /** Whether the card was inside a dialog, the viewport's modal outlet. */ |
| 28 | + isInDialog: boolean; |
| 29 | +} |
| 30 | + |
| 31 | +/** What the recorder saw of the toast's card. */ |
| 32 | +interface Recording { |
| 33 | + /** The card on each frame it was in the page. */ |
| 34 | + frames: Frame[]; |
| 35 | + /** The opacity transitions the card ran: one for each entrance. */ |
| 36 | + entrances: number; |
| 37 | +} |
| 38 | + |
| 39 | +interface Recorder { |
| 40 | + frames: Frame[]; |
| 41 | + transitions: Set<Animation>; |
| 42 | + isRecording: boolean; |
| 43 | +} |
| 44 | + |
| 45 | +/** Records the toast card's opacity and its opacity transitions on each frame. */ |
| 46 | +async function startRecording(page: Page): Promise<void> { |
| 47 | + await page.evaluate(() => { |
| 48 | + const recorder: Recorder = { |
| 49 | + frames: [], |
| 50 | + transitions: new Set(), |
| 51 | + isRecording: true, |
| 52 | + }; |
| 53 | + (window as unknown as {__toastRecorder: Recorder}).__toastRecorder = |
| 54 | + recorder; |
| 55 | + const record = () => { |
| 56 | + const card = document.querySelector('[data-toast-id] .astryx-toast'); |
| 57 | + if (card != null) { |
| 58 | + recorder.frames.push({ |
| 59 | + opacity: Number(getComputedStyle(card).opacity), |
| 60 | + isInDialog: card.closest('dialog') != null, |
| 61 | + }); |
| 62 | + for (const animation of card.getAnimations()) { |
| 63 | + if ( |
| 64 | + animation instanceof CSSTransition && |
| 65 | + animation.transitionProperty === 'opacity' |
| 66 | + ) { |
| 67 | + recorder.transitions.add(animation); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + if (recorder.isRecording) { |
| 72 | + requestAnimationFrame(record); |
| 73 | + } |
| 74 | + }; |
| 75 | + requestAnimationFrame(record); |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +async function stopRecording(page: Page): Promise<Recording> { |
| 80 | + return page.evaluate(() => { |
| 81 | + const recorder = (window as unknown as {__toastRecorder: Recorder}) |
| 82 | + .__toastRecorder; |
| 83 | + recorder.isRecording = false; |
| 84 | + return { |
| 85 | + frames: recorder.frames, |
| 86 | + entrances: recorder.transitions.size, |
| 87 | + }; |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +/** How many frames the recorder has seen the card in a dialog so far. */ |
| 92 | +async function framesInDialog(page: Page): Promise<number> { |
| 93 | + return page.evaluate( |
| 94 | + () => |
| 95 | + ( |
| 96 | + window as unknown as {__toastRecorder: Recorder} |
| 97 | + ).__toastRecorder.frames.filter(frame => frame.isInDialog).length, |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +/** How many frames the recorder has seen the card so far. */ |
| 102 | +async function frameCount(page: Page): Promise<number> { |
| 103 | + return page.evaluate( |
| 104 | + () => |
| 105 | + (window as unknown as {__toastRecorder: Recorder}).__toastRecorder.frames |
| 106 | + .length, |
| 107 | + ); |
| 108 | +} |
| 109 | + |
| 110 | +/** The card's places in frame order, each run once: `['dialog', 'page']`. */ |
| 111 | +function placesOf(frames: Frame[]): string[] { |
| 112 | + const places: string[] = []; |
| 113 | + for (const {isInDialog} of frames) { |
| 114 | + const place = isInDialog ? 'dialog' : 'page'; |
| 115 | + if (places.at(-1) !== place) { |
| 116 | + places.push(place); |
| 117 | + } |
| 118 | + } |
| 119 | + return places; |
| 120 | +} |
| 121 | + |
| 122 | +/** Whether the card faded back out after it had shown: a second entrance. */ |
| 123 | +function fadesBack(frames: Frame[]): boolean { |
| 124 | + let hasShown = false; |
| 125 | + for (const {opacity} of frames) { |
| 126 | + if (opacity > 0.9) { |
| 127 | + hasShown = true; |
| 128 | + } else if (hasShown && opacity < 0.3) { |
| 129 | + return true; |
| 130 | + } |
| 131 | + } |
| 132 | + return false; |
| 133 | +} |
| 134 | + |
| 135 | +async function openDialog(page: Page) { |
| 136 | + await page.getByRole('button', {name: 'Open dialog'}).click(); |
| 137 | + const dialog = page.getByRole('dialog'); |
| 138 | + await waitForRest(dialog); |
| 139 | + return dialog; |
| 140 | +} |
| 141 | + |
| 142 | +const toastRow = (page: Page) => page.locator('[data-toast-id]'); |
| 143 | + |
| 144 | +/** The message in the toast's row, not in the viewport's live region. */ |
| 145 | +const toastText = (page: Page) => toastRow(page).getByText(MESSAGE); |
| 146 | + |
| 147 | +/** The row is shown, with a height, after the move. */ |
| 148 | +async function expectRowShown(page: Page) { |
| 149 | + await expect(toastRow(page)).toBeVisible(); |
| 150 | + const box = await toastRow(page).boundingBox(); |
| 151 | + expect(box?.height ?? 0).toBeGreaterThan(0); |
| 152 | +} |
| 153 | + |
| 154 | +const opacitiesOf = (frames: Frame[]) => frames.map(frame => frame.opacity); |
| 155 | + |
| 156 | +test.describe('a toast in a top-layer viewport', () => { |
| 157 | + test('a new toast plays its entrance once', async ({page}) => { |
| 158 | + await openStory(page, storybook(), STORY); |
| 159 | + const dialog = await openDialog(page); |
| 160 | + await startRecording(page); |
| 161 | + await dialog.getByRole('button', {name: 'Show toast'}).click(); |
| 162 | + await expect(toastText(page)).toBeVisible(); |
| 163 | + await waitForRest(toastRow(page)); |
| 164 | + const {frames, entrances} = await stopRecording(page); |
| 165 | + await expectRowShown(page); |
| 166 | + expect(placesOf(frames)).toEqual(['dialog']); |
| 167 | + // It fades in from nearly clear to fully shown. |
| 168 | + expect(Math.min(...opacitiesOf(frames))).toBeLessThan(0.5); |
| 169 | + expect(Math.max(...opacitiesOf(frames))).toBeGreaterThan(0.99); |
| 170 | + expect(entrances).toBe(1); |
| 171 | + }); |
| 172 | + |
| 173 | + test('a toast raised in a Dialog that then closes enters once', async ({ |
| 174 | + page, |
| 175 | + }) => { |
| 176 | + await openStory(page, storybook(), STORY); |
| 177 | + const dialog = await openDialog(page); |
| 178 | + await startRecording(page); |
| 179 | + await dialog.getByRole('button', {name: 'Show toast'}).click(); |
| 180 | + // Closes once the toast has shown in the dialog, usually while it still |
| 181 | + // enters. |
| 182 | + await expect.poll(async () => framesInDialog(page)).toBeGreaterThan(0); |
| 183 | + await dialog.getByRole('button', {name: 'Close'}).click(); |
| 184 | + await expect(dialog).toBeHidden(); |
| 185 | + await expect(toastText(page)).toBeVisible(); |
| 186 | + await waitForRest(toastRow(page)); |
| 187 | + const {frames, entrances} = await stopRecording(page); |
| 188 | + await expectRowShown(page); |
| 189 | + expect(placesOf(frames)).toEqual(['dialog', 'page']); |
| 190 | + expect(fadesBack(frames)).toBe(false); |
| 191 | + expect(entrances).toBe(1); |
| 192 | + }); |
| 193 | + |
| 194 | + test('a toast already shown stays still when the Dialog that hosts it closes', async ({ |
| 195 | + page, |
| 196 | + }) => { |
| 197 | + await openStory(page, storybook(), STORY); |
| 198 | + const dialog = await openDialog(page); |
| 199 | + await dialog.getByRole('button', {name: 'Show toast'}).click(); |
| 200 | + await waitForRest(toastRow(page)); |
| 201 | + await startRecording(page); |
| 202 | + await expect.poll(async () => framesInDialog(page)).toBeGreaterThan(0); |
| 203 | + await dialog.getByRole('button', {name: 'Close'}).click(); |
| 204 | + await expect(dialog).toBeHidden(); |
| 205 | + await waitForRest(toastRow(page)); |
| 206 | + const {frames, entrances} = await stopRecording(page); |
| 207 | + await expectRowShown(page); |
| 208 | + expect(placesOf(frames)).toEqual(['dialog', 'page']); |
| 209 | + expect(Math.min(...opacitiesOf(frames))).toBeGreaterThan(0.99); |
| 210 | + expect(entrances).toBe(0); |
| 211 | + }); |
| 212 | + |
| 213 | + test('a toast already shown stays still when a Dialog opens over it', async ({ |
| 214 | + page, |
| 215 | + }) => { |
| 216 | + await openStory(page, storybook(), STORY); |
| 217 | + const dialog = await openDialog(page); |
| 218 | + await dialog.getByRole('button', {name: 'Show toast'}).click(); |
| 219 | + await dialog.getByRole('button', {name: 'Close'}).click(); |
| 220 | + await expect(dialog).toBeHidden(); |
| 221 | + await waitForRest(toastRow(page)); |
| 222 | + await startRecording(page); |
| 223 | + await expect.poll(async () => frameCount(page)).toBeGreaterThan(0); |
| 224 | + await openDialog(page); |
| 225 | + await waitForRest(toastRow(page)); |
| 226 | + const {frames, entrances} = await stopRecording(page); |
| 227 | + await expectRowShown(page); |
| 228 | + // The toast moved into the dialog that opened. |
| 229 | + expect(placesOf(frames)).toEqual(['page', 'dialog']); |
| 230 | + expect(Math.min(...opacitiesOf(frames))).toBeGreaterThan(0.99); |
| 231 | + expect(entrances).toBe(0); |
| 232 | + }); |
| 233 | +}); |
0 commit comments