| 1 |
import { expect } from '@playwright/test'; |
| 2 |
import type { Page } from '@playwright/test'; |
| 3 |
import type Swup from '../../src/Swup.js'; |
| 4 |
import { expectNumberCloseTo } from './commands.js'; |
| 5 |
|
| 6 |
declare global { |
| 7 |
interface Window { |
| 8 |
_swup: Swup; |
| 9 |
data: any; |
| 10 |
} |
| 11 |
} |
| 12 |
|
| 13 |
export async function waitForSwup(page: Page) { |
| 14 |
await page.waitForSelector('html.swup-enabled'); |
| 15 |
} |
| 16 |
|
| 17 |
export function navigateWithSwup(page: Page, url: string, options?: Parameters<Swup['navigate']>[1]) { |
| 18 |
return page.evaluate(({ url, options }) => window._swup.navigate(url, options), { url, options }); |
| 19 |
} |
| 20 |
|
| 21 |
export async function pushSwupHistoryState(page: Page, url: string, data: Record<string, unknown> = {}) { |
| 22 |
const state = { url, random: Math.random(), source: 'swup', ...data }; |
| 23 |
await page.evaluate(({ url, state }) => window.history.pushState(state, '', url), { url, state }); |
| 24 |
} |
| 25 |
|
| 26 |
export async function expectSwupToHaveCacheEntry(page: Page, url: string) { |
| 27 |
const entry = await page.evaluate((url) => window._swup.cache.get(url), url); |
| 28 |
expect(entry).toBeTruthy(); |
| 29 |
expect(entry).toHaveProperty('url', url); |
| 30 |
} |
| 31 |
|
| 32 |
export async function expectSwupToHaveCacheEntries(page: Page, urls: string[]) { |
| 33 |
for (const url of urls) { |
| 34 |
await expectSwupToHaveCacheEntry(page, url); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
export async function expectSwupAnimationDuration(page: Page, expected: { total?: number, out?: number, in?: number }) { |
| 39 |
// Make sure we're ready to animate |
| 40 |
await waitForSwup(page); |
| 41 |
|
| 42 |
// Make sure we're not disabling animations |
| 43 |
await page.emulateMedia({ reducedMotion: null }); |
| 44 |
|
| 45 |
await page.evaluate(() => { |
| 46 |
window.data = {}; |
| 47 |
const measure = (key: string, value?: number) => window.data[key] = value ?? performance.now(); |
| 48 |
window._swup.hooks.on('visit:start', () => measure('start')); |
| 49 |
window._swup.hooks.on('visit:end', () => measure('end')); |
| 50 |
window._swup.hooks.on('animation:out:start', () => measure('outStart')); |
| 51 |
window._swup.hooks.on('animation:out:end', () => measure('outEnd')); |
| 52 |
window._swup.hooks.on('animation:in:start', () => measure('inStart')); |
| 53 |
window._swup.hooks.on('animation:in:end', () => measure('inEnd')); |
| 54 |
window._swup.hooks.on('animation:skip', () => measure('inStart', 0)); |
| 55 |
window._swup.hooks.on('animation:skip', () => measure('inEnd', 0)); |
| 56 |
window._swup.hooks.on('animation:skip', () => measure('outStart', 0)); |
| 57 |
window._swup.hooks.on('animation:skip', () => measure('outEnd', 0)); |
| 58 |
}); |
| 59 |
|
| 60 |
await navigateWithSwup(page, page.url()); |
| 61 |
|
| 62 |
await page.waitForFunction(() => window.data.end > 0); |
| 63 |
|
| 64 |
const timing: { start: 0, end: 0, outStart: 0, outEnd: 0, inStart: 0, inEnd: 0 } = await page.evaluate(() => window.data); |
| 65 |
const seen = { total: timing.end - timing.start, out: timing.outEnd - timing.outStart, in: timing.inEnd - timing.inStart }; |
| 66 |
|
| 67 |
const tolerance = expected ? [0.85, 1.35] : [1, 1]; |
| 68 |
|
| 69 |
if (typeof expected.out === 'number') { |
| 70 |
expectNumberCloseTo(seen.out, expected.out, tolerance); |
| 71 |
} |
| 72 |
if (typeof expected.in === 'number') { |
| 73 |
expectNumberCloseTo(seen.in, expected.in, tolerance); |
| 74 |
} |
| 75 |
if (typeof expected.total === 'number') { |
| 76 |
expectNumberCloseTo(seen.total, expected.total, tolerance); |
| 77 |
} |
| 78 |
} |