Debian Codegraph @

tests/functional/navigation.spec.ts

raw
1 import { test, expect } from '@playwright/test';
2
3 import { clickOnLink, delayRequest, expectToBeAt, sleep } from '../support/commands.js';
4 import { navigateWithSwup, waitForSwup } from '../support/swup.js';
5
6 test.describe('navigation', () => {
7 test.beforeEach(async ({ page }) => {
8 await page.goto('/page-1.html');
9 });
10
11 test('navigates to other pages', async ({ page }) => {
12 await clickOnLink(page, '/page-2.html');
13 await expectToBeAt(page, '/page-2.html', 'Page 2');
14 await clickOnLink(page, '/page-3.html');
15 await expectToBeAt(page, '/page-3.html', 'Page 3');
16 });
17
18 test('navigates if no animation selector defined', async ({ page }) => {
19 await page.evaluate(() => {
20 window._swup.options.animationSelector = false;
21 });
22 await clickOnLink(page, '/page-2.html');
23 await expectToBeAt(page, '/page-2.html', 'Page 2');
24 });
25
26 // Seems to break in CI :(
27 // test('ignores visit when meta key pressed', async ({ page, context }) => {
28 // await clickOnLink(page, '/page-2.html', { modifiers: ['Meta'] });
29 // await sleep(300);
30 // await expectToBeAt(page, '/page-1.html', 'Page 1');
31 // });
32
33 test('ignores visit if a new visit has started', async ({ page }) => {
34 await delayRequest(page, '/page-2.html', 500);
35 await navigateWithSwup(page, '/page-2.html');
36 await page.waitForTimeout(100);
37 await navigateWithSwup(page, '/page-3.html');
38 await expectToBeAt(page, '/page-3.html', 'Page 3');
39 await page.waitForTimeout(700);
40 await expectToBeAt(page, '/page-3.html', 'Page 3');
41 });
42
43 test('ignores consecutive clicks on the same link', async ({ page }) => {
44 await page.evaluate(() => {
45 window.data = { clickCount: 0 }
46 window._swup.hooks.on('link:click', () => (window.data.clickCount += 1));
47 });
48 await clickOnLink(page, '/page-2.html', { clickCount: 3 });
49 expect(await page.evaluate(() => window.data.clickCount)).toBe(1);
50 });
51
52 test('handles rapid consecutive navigations', async ({ page }) => {
53 await page.goto('/rapid-navigation/page-1.html');
54 await waitForSwup(page);
55 const expected = [
56 'visit:start',
57 'animation:out:start',
58 'fetch:request',
59 'page:load',
60 'animation:out:await',
61 'animation:out:end',
62 'content:replace',
63 'scroll:top',
64 'content:scroll',
65 'page:view',
66 'animation:in:start',
67 'animation:in:await',
68 'animation:in:end',
69 'visit:transition',
70 'visit:end'
71 ];
72 await clickOnLink(page, '/rapid-navigation/page-2.html');
73 await sleep(300);
74 await clickOnLink(page, '/rapid-navigation/page-2.html');
75
76 await page.waitForSelector('html:not([aria-busy=true])');
77 const received = await page.evaluate(() => window.data.received);
78 expect(received).toEqual(expected);
79 });
80
81 test('ignores aborted visits', async ({ page }) => {
82 await page.goto('/rapid-navigation/page-1.html');
83 await waitForSwup(page);
84 await page.evaluate(() => {
85 window._swup.hooks.on('content:replace', (visit) => visit.abort());
86 });
87 const expected = [
88 'visit:start',
89 'animation:out:start',
90 'fetch:request',
91 'page:load',
92 'animation:out:await',
93 'animation:out:end'
94 ];
95 await clickOnLink(page, '/rapid-navigation/page-2.html');
96 await sleep(1000); // we have to wait here, since we cannot rely on anything from swup (the visit is being exited)
97 const received = await page.evaluate(() => window.data.received);
98 expect(received).toEqual(expected);
99 });
100 });