-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
view-transitions.test.js
1113 lines (900 loc) · 35.5 KB
/
view-transitions.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { expect } from '@playwright/test';
import { testFactory, waitForHydrate } from './test-utils.js';
const test = testFactory({ root: './fixtures/view-transitions/' });
let devServer;
test.beforeAll(async ({ astro }) => {
devServer = await astro.startDevServer();
});
test.afterAll(async () => {
await devServer.stop();
});
function scrollToBottom(page) {
return page.evaluate(() => {
window.scrollY = document.documentElement.scrollHeight;
window.dispatchEvent(new Event('scroll'));
});
}
function collectPreloads(page) {
return page.evaluate(() => {
window.preloads = [];
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) =>
mutation.addedNodes.forEach((node) => {
if (node.nodeName === 'LINK' && node.rel === 'preload') preloads.push(node.href);
})
);
});
observer.observe(document.head, { childList: true });
});
}
test.describe('View Transitions', () => {
test('Moving from page 1 to page 2', async ({ page, astro }) => {
const loads = [];
page.addListener('load', (p) => {
loads.push(p.title());
});
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// go to page 2
await page.click('#click-two');
p = page.locator('#two');
await expect(p, 'should have content').toHaveText('Page 2');
expect(loads.length, 'There should only be 1 page load').toEqual(1);
});
test('Back button is captured', async ({ page, astro }) => {
const loads = [];
page.addListener('load', (p) => {
loads.push(p.title());
});
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// go to page 2
await page.click('#click-two');
p = page.locator('#two');
await expect(p, 'should have content').toHaveText('Page 2');
// Back to page 1
await page.goBack();
p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
expect(loads.length, 'There should only be 1 page load').toEqual(1);
});
test('Clicking on a link with nested content', async ({ page, astro }) => {
const loads = [];
page.addListener('load', (p) => {
loads.push(p.title());
});
// Go to page 4
await page.goto(astro.resolveUrl('/four'));
let p = page.locator('#four');
await expect(p, 'should have content').toHaveText('Page 4');
// Go to page 1
await page.click('#click-one');
p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
expect(loads.length, 'There should only be 1 page load').toEqual(1);
});
test('Clicking on a link to a page with non-recommended headers', async ({page, astro}) => {
const loads = [];
page.addListener('load', (p) => {
loads.push(p.title());
});
// Go to page 4
await page.goto(astro.resolveUrl('/one'));
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// Go to page 1
await page.click('#click-seven');
p = page.locator('#seven');
await expect(p, 'should have content').toHaveText('Page 7');
expect(loads.length, 'There should only be 1 page load').toEqual(1);
});
test('Moving to a page without ViewTransitions triggers a full page navigation', async ({
page,
astro,
}) => {
const loads = [];
page.addListener('load', (p) => {
loads.push(p.title());
});
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// Go to page 3 which does *not* have ViewTransitions enabled
await page.click('#click-three');
p = page.locator('#three');
await expect(p, 'should have content').toHaveText('Page 3');
expect(
loads.length,
'There should be 2 page loads. The original, then going from 3 to 2'
).toEqual(2);
});
test('Moving within a page without ViewTransitions does not trigger a full page navigation', async ({
page,
astro,
}) => {
const loads = [];
page.addListener('load', async (p) => {
loads.push(p.title());
});
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// Go to page 3 which does *not* have ViewTransitions enabled
await page.click('#click-three');
p = page.locator('#three');
await expect(p, 'should have content').toHaveText('Page 3');
// click a hash link to navigate further down the page
await page.click('#click-hash');
// still on page 3
p = page.locator('#three');
await expect(p, 'should have content').toHaveText('Page 3');
// check that we are further down the page
const Y = await page.evaluate(() => window.scrollY);
expect(Y, 'The target is further down the page').toBeGreaterThan(0);
expect(
loads.length,
'There should be only 2 page loads (for page one & three), but no additional loads for the hash change'
).toEqual(2);
});
test('Moving from a page without ViewTransitions w/ back button', async ({ page, astro }) => {
const loads = [];
page.addListener('load', (p) => {
loads.push(p.title());
});
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// Go to page 3 which does *not* have ViewTransitions enabled
await page.click('#click-three');
p = page.locator('#three');
await expect(p, 'should have content').toHaveText('Page 3');
// Back to page 1
await page.goBack();
p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
});
test('Stylesheets in the head are waited on', async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
await collectPreloads(page);
// Go to page 2
await page.click('#click-two');
p = page.locator('#two');
await expect(p, 'should have content').toHaveText('Page 2');
await expect(p, 'imported CSS updated').toHaveCSS('font-size', '24px');
const preloads = await page.evaluate(() => window.preloads);
expect(preloads.length === 1 && preloads[0].endsWith('/two.css')).toBeTruthy();
});
test('astro:page-load event fires when navigating to new page', async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
const p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// go to page 2
await page.click('#click-two');
const article = page.locator('#twoarticle');
await expect(article, 'should have script content').toHaveText('works');
});
test('astro:page-load event fires when navigating directly to a page', async ({
page,
astro,
}) => {
// Go to page 2
await page.goto(astro.resolveUrl('/two'));
const article = page.locator('#twoarticle');
await expect(article, 'should have script content').toHaveText('works');
});
test('astro:after-swap event fires right after the swap', async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// go to page 2
await page.click('#click-two');
p = page.locator('#two');
const h = page.locator('html');
await expect(h, 'imported CSS updated').toHaveCSS('background-color', 'rgba(0, 0, 0, 0)');
});
test('No page rendering during swap()', async ({ page, astro }) => {
// This has been a problem with theme switchers (e.g. for drakmode)
// Swap() should not trigger any page renders and give users the chance to
// correct attributes in the astro:after-swap handler before they become visible
// This test uses a CSS animation to detect page rendering
// The test succeeds if no additional animation beside those of the
// view transition is triggered during swap()
await page.goto(astro.resolveUrl('/listener-one'));
let p = page.locator('#totwo');
await expect(p, 'should have content').toHaveText('Go to listener two');
// setting the blue class on the html element triggers a CSS animation
let animations = await page.evaluate(async () => {
document.documentElement.classList.add('blue');
return document.getAnimations();
});
expect(animations.length).toEqual(1);
// go to page 2
await page.click('#totwo');
p = page.locator('#toone');
await expect(p, 'should have content').toHaveText('Go to listener one');
// swap() resets the "blue" class, as it is not set in the static html of page 2
// The astro:after-swap listener (defined in the layout) sets it to "blue" again.
// The temporarily missing class must not trigger page rendering.
// When the after-swap listener starts, no animations should be running
// after-swap listener sets animations to document.getAnimations().length
// and we expect this to be zero
await expect(page.locator('html')).toHaveAttribute('animations', '0');
});
test('click hash links does not do navigation', async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
const p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// Clicking 1 stays put
await page.click('#click-one');
await expect(p, 'should have content').toHaveText('Page 1');
});
test('click self link (w/o hash) does not do navigation', async ({ page, astro }) => {
const loads = [];
page.addListener('load', (p) => {
loads.push(p.title());
});
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
const p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// Clicking href="" stays on page
await page.click('#click-self');
await expect(p, 'should have content').toHaveText('Page 1');
expect(loads.length, 'There should only be 1 page load').toEqual(1);
});
test('Scroll position restored on back button', async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/long-page'));
let article = page.locator('#longpage');
await expect(article, 'should have script content').toBeVisible('exists');
await scrollToBottom(page);
const oldScrollY = await page.evaluate(() => window.scrollY);
// go to page long-page
await page.click('#click-one');
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// Back to page 1
await page.goBack();
const newScrollY = await page.evaluate(() => window.scrollY);
expect(oldScrollY).toEqual(newScrollY);
});
test('Fragment scroll position restored on back button', async ({ page, astro }) => {
// Go to the long page
await page.goto(astro.resolveUrl('/long-page'));
let locator = page.locator('#longpage');
await expect(locator).toBeInViewport();
// Scroll down to middle fragment
await page.click('#click-scroll-down');
locator = page.locator('#click-one-again');
await expect(locator).toBeInViewport();
// Scroll up to top fragment
await page.click('#click-scroll-up');
locator = page.locator('#longpage');
await expect(locator).toBeInViewport();
// Back to middle of the page
await page.goBack();
locator = page.locator('#click-one-again');
await expect(locator).toBeInViewport();
});
test('Scroll position restored when transitioning back to fragment', async ({ page, astro }) => {
// Go to the long page
await page.goto(astro.resolveUrl('/long-page'));
let locator = page.locator('#longpage');
await expect(locator).toBeInViewport();
// Scroll down to middle fragment
await page.click('#click-scroll-down');
locator = page.locator('#click-one-again');
await expect(locator).toBeInViewport();
// goto page 1
await page.click('#click-one-again');
locator = page.locator('#one');
await expect(locator).toHaveText('Page 1');
// Back to middle of the previous page
await page.goBack();
locator = page.locator('#click-one-again');
await expect(locator).toBeInViewport();
});
test('Scroll position restored on forward button', async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// go to page long-page
await page.click('#click-longpage');
let article = page.locator('#longpage');
await expect(article, 'should have script content').toBeVisible('exists');
await scrollToBottom(page);
const oldScrollY = await page.evaluate(() => window.scrollY);
// Back to page 1
await page.goBack();
// Go forward
await page.goForward();
article = page.locator('#longpage');
await expect(article, 'should have script content').toBeVisible('exists');
const newScrollY = await page.evaluate(() => window.scrollY);
expect(oldScrollY).toEqual(newScrollY);
});
test('Fragment scroll position restored on forward button', async ({ page, astro }) => {
// Go to the long page
await page.goto(astro.resolveUrl('/long-page'));
let locator = page.locator('#longpage');
await expect(locator).toBeInViewport();
// Scroll down to middle fragment
await page.click('#click-scroll-down');
locator = page.locator('#click-one-again');
await expect(locator).toBeInViewport();
// Scroll back to top
// back returns immediately, but we need to wait for navigate() to complete
const waitForReady = page.waitForEvent('console');
await page.goBack();
await waitForReady;
locator = page.locator('#longpage');
await expect(locator).toBeInViewport();
// Forward to middle of page
await page.goForward();
locator = page.locator('#click-one-again');
await expect(locator).toBeInViewport();
});
test('<Image /> component forwards transitions to the <img>', async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/image-one'));
const img = page.locator('img[data-astro-transition-scope]');
await expect(img).toBeVisible('The image tag should have the transition scope attribute.');
});
test('<video> can persist using transition:persist', async ({ page, astro }) => {
const getTime = () => document.querySelector('video').currentTime;
// Go to page 1
await page.goto(astro.resolveUrl('/video-one'));
const vid = page.locator('video');
await expect(vid).toBeVisible();
const firstTime = await page.evaluate(getTime);
// Navigate to page 2
await page.click('#click-two');
const p = page.locator('#video-two');
await expect(p).toBeVisible();
const secondTime = await page.evaluate(getTime);
expect(secondTime).toBeGreaterThanOrEqual(firstTime);
});
test('Islands can persist using transition:persist', async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/island-one'));
let cnt = page.locator('.counter pre');
await expect(cnt).toHaveText('5');
await page.click('.increment');
await expect(cnt).toHaveText('6');
// Navigate to page 2
await page.click('#click-two');
const p = page.locator('#island-two');
await expect(p).toBeVisible();
cnt = page.locator('.counter pre');
// Count should remain
await expect(cnt).toHaveText('6');
});
test('Scripts are only executed once', async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
const p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// go to page 2
await page.click('#click-two');
const article = page.locator('#twoarticle');
await expect(article, 'should have script content').toHaveText('works');
const meta = page.locator('[name="script-executions"]');
await expect(meta).toHaveAttribute('content', '0');
});
test('Navigating to the same path but with different query params should result in transition', async ({
page,
astro,
}) => {
const loads = [];
page.addListener('load', (p) => {
loads.push(p.title());
});
// Go to page 1
await page.goto(astro.resolveUrl('/query'));
let p = page.locator('#query-page');
await expect(p, 'should have content').toHaveText('Page 1');
// go to page 2
await page.click('#click-two');
p = page.locator('#query-page');
await expect(p, 'should have content').toHaveText('Page 2');
await expect(loads.length, 'There should only be 1 page load').toEqual(1);
});
test('Importing ViewTransitions w/o using the component must not mess with history', async ({
page,
astro,
}) => {
const loads = [];
page.addListener('load', async (p) => {
loads.push(p);
});
// Go to the half bakeed page
await page.goto(astro.resolveUrl('/half-baked'));
let p = page.locator('#half-baked');
await expect(p, 'should have content').toHaveText('Half Baked');
// click a hash link to navigate further down the page
await page.click('#click-hash');
// still on page
p = page.locator('#half-baked');
await expect(p, 'should have content').toHaveText('Half Baked');
// go back within same page without reloading
await page.goBack();
p = page.locator('#half-baked');
await expect(p, 'should have content').toHaveText('Half Baked');
expect(
loads.length,
'There should be only 1 page load. No additional loads for going back on same page'
).toEqual(1);
});
test('Navigation also swaps the attributes of the document root', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/some-attributes'));
let p = page.locator('#heading');
await expect(p, 'should have content').toHaveText('Page with some attributes');
let h = page.locator('html');
await expect(h, 'should have content').toHaveAttribute('lang', 'en');
await page.click('#click-other-attributes');
p = page.locator('#heading');
await expect(p, 'should have content').toHaveText('Page with other attributes');
h = page.locator('html');
await expect(h, 'should have content').toHaveAttribute('lang', 'es');
await expect(h, 'should have content').toHaveAttribute('style', 'background-color: green');
await expect(h, 'should have content').toHaveAttribute('data-other-name', 'value');
await expect(h, 'should have content').toHaveAttribute('data-astro-fake', 'value');
await expect(h, 'should have content').toHaveAttribute('data-astro-transition', 'forward');
await expect(h, 'should be absent').not.toHaveAttribute('class', /.*/);
});
test('Link with data-astro-reload attribute should trigger page load, no tranistion', async ({
page,
astro,
}) => {
const loads = [];
page.addListener('load', (p) => {
loads.push(p.title());
});
// Go to page 4
await page.goto(astro.resolveUrl('/four'));
let p = page.locator('#four');
await expect(p, 'should have content').toHaveText('Page 4');
// go to page 2
await page.click('#click-two');
p = page.locator('#two');
await expect(p, 'should have content').toHaveText('Page 2');
expect(loads.length, 'There should be 2 page load').toEqual(2);
});
test('Link with download attribute should trigger download, no transition', async ({
page,
astro,
}) => {
// Go to page 4
await page.goto(astro.resolveUrl('/four'));
let p = page.locator('#four');
await expect(p, 'should have content').toHaveText('Page 4');
// Start waiting for download before clicking. Note no await.
const downloadPromise = page.waitForEvent('download', { timeout: 4000 });
await page.click('#click-logo');
await downloadPromise;
});
test('data-astro-reload not required for non-html content', async ({ page, astro }) => {
const loads = [];
page.addListener('load', (p) => {
loads.push(p.title());
});
// Go to page 4
await page.goto(astro.resolveUrl('/four'));
let p = page.locator('#four');
await expect(p, 'should have content').toHaveText('Page 4');
await page.click('#click-svg');
p = page.locator('svg');
await expect(p).toBeVisible();
expect(loads.length, 'There should be 2 page load').toEqual(2);
});
test('Scroll position is restored on back navigation from page w/o ViewTransitions', async ({
page,
astro,
}) => {
// Go to middle of long page
await page.goto(astro.resolveUrl('/long-page#click-external'));
let locator = page.locator('#click-external');
await expect(locator).toBeInViewport();
// Go to a page that has not enabled ViewTransistions
await page.click('#click-external');
locator = page.locator('#three');
await expect(locator).toHaveText('Page 3');
// Scroll back to long page
await page.goBack();
locator = page.locator('#click-external');
await expect(locator).toBeInViewport();
});
test("Non transition navigation doesn't loose handlers", async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// go to page 3
await page.click('#click-three');
p = page.locator('#three');
await expect(p, 'should have content').toHaveText('Page 3');
// go to page 5
await page.click('#click-five');
p = page.locator('#five');
await expect(p, 'should have content').toHaveText('Page 5');
await page.goBack();
p = page.locator('#three');
await expect(p, 'should have content').toHaveText('Page 3');
await page.goBack();
p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
});
test('Moving to a page which redirects to another', async ({ page, astro }) => {
const loads = [];
page.addListener('load', (p) => {
loads.push(p.title());
});
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// go to page 2
await page.click('#click-redirect-two');
p = page.locator('#two');
await expect(p, 'should have content').toHaveText('Page 2');
// go back
await page.goBack();
p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
expect(
loads.length,
'There should only be the initial page load and two normal transitions'
).toEqual(1);
});
test('Redirect to external site causes page load', async ({ page, astro }) => {
const loads = [];
page.addListener('load', (p) => {
loads.push(p.title());
});
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// go to external page
await page.click('#click-redirect-external');
// doesn't work for playwright when we are too fast
p = page.locator('h1');
await expect(p, 'should have content').toBeVisible();
await page.waitForURL('http://example.com');
await page.waitForFunction((arr) => arr.length === 2, loads);
expect(loads.length, 'There should be 2 page loads').toEqual(2);
});
test('client:only styles are retained on transition (1/2)', async ({ page, astro }) => {
const totalExpectedStyles = 9;
await page.goto(astro.resolveUrl('/client-only-one'));
let msg = page.locator('.counter-message');
await expect(msg).toHaveText('message here');
let styles = await page.locator('style').all();
expect(styles.length).toEqual(totalExpectedStyles);
await page.click('#click-two');
let pageTwo = page.locator('#page-two');
await expect(pageTwo, 'should have content').toHaveText('Page 2');
styles = await page.locator('style').all();
expect(styles.length).toEqual(totalExpectedStyles, 'style count has not changed');
});
test('client:only styles are retained on transition (2/2)', async ({ page, astro }) => {
const totalExpectedStyles_page_three = 11;
const totalExpectedStyles_page_four = 9;
await page.goto(astro.resolveUrl('/client-only-three'));
let msg = page.locator('#name');
await expect(msg).toHaveText('client-only-three');
await waitForHydrate(page, page.getByText('Vue'));
await waitForHydrate(page, page.getByText('Svelte'));
let styles = await page.locator('style').all();
expect(styles.length).toEqual(totalExpectedStyles_page_three);
await page.click('#click-four');
let pageTwo = page.locator('#page-four');
await expect(pageTwo, 'should have content').toHaveText('Page 4');
styles = await page.locator('style').all();
expect(styles.length).toEqual(totalExpectedStyles_page_four, 'style count has not changed');
});
test('Horizontal scroll position restored on back button', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/wide-page'));
let article = page.locator('#widepage');
await expect(article, 'should have script content').toBeVisible('exists');
let locator = page.locator('#click-one');
await expect(locator).not.toBeInViewport();
await page.click('#click-right');
locator = page.locator('#click-one');
await expect(locator).toBeInViewport();
locator = page.locator('#click-top');
await expect(locator).toBeInViewport();
await page.click('#click-one');
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
await page.goBack();
locator = page.locator('#click-one');
await expect(locator).toBeInViewport();
locator = page.locator('#click-top');
await expect(locator).toBeInViewport();
await page.click('#click-top');
locator = page.locator('#click-one');
await expect(locator).not.toBeInViewport();
});
test('Use the client side router', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/six'));
// page six loads the router and automatically uses the router to navigate to page 1
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// nudge to jump to page 2
await page.evaluate(() => {
window.dispatchEvent(new Event('jumpToTwo'));
});
p = page.locator('#two');
await expect(p, 'should have content').toHaveText('Page 2');
// jump to page 3
await page.evaluate(() => {
// get the router from its fixture park position
const navigate = window.clientSideRouterForTestsParkedHere;
navigate('/three');
});
p = page.locator('#three');
await expect(p, 'should have content').toHaveText('Page 3');
// go back
await page.goBack();
p = page.locator('#two');
await expect(p, 'should have content').toHaveText('Page 2');
// no bad things happen when we revisit redirecting to page 6
await page.goto(astro.resolveUrl('/six'));
p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
});
test('Use the client side router in framework components', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/client-load'));
// the button is set to naviagte() to /two
const button = page.locator('#react-client-load-navigate-button');
await expect(button, 'should have content').toHaveText('Navigate to `/two`');
await button.click();
const p = page.locator('#two');
await expect(p, 'should have content').toHaveText('Page 2');
});
test('body inline scripts do not re-execute on navigation', async ({ page, astro }) => {
const errors = [];
page.addListener('pageerror', (err) => {
errors.push(err);
});
await page.goto(astro.resolveUrl('/inline-script-one'));
let article = page.locator('#counter');
await expect(article, 'should have script content').toBeVisible('exists');
await page.click('#click-one');
article = page.locator('#counter');
await expect(article, 'should have script content').toHaveText('Count: 3');
expect(errors).toHaveLength(0);
});
test('replace history', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/one'));
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// go to page 2
await page.click('#click-two');
p = page.locator('#two');
await expect(p, 'should have content').toHaveText('Page 2');
// replace with long page
await page.click('#click-longpage');
let article = page.locator('#longpage');
await expect(article, 'should have script content').toBeVisible('exists');
// one step back == #1
await page.goBack();
p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
});
test('CSR replace history', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/six'));
// page six loads the router and automatically uses the router to navigate to page 1
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
// goto #2
await page.evaluate(() => {
window.clientSideRouterForTestsParkedHere('/two', { history: 'auto' });
});
p = page.locator('#two');
await expect(p, 'should have content').toHaveText('Page 2');
// replace with long page
await page.evaluate(() => {
window.clientSideRouterForTestsParkedHere('/long-page', { history: 'replace' });
});
let article = page.locator('#longpage');
await expect(article, 'should have script content').toBeVisible('exists');
// one step back == #1
await page.goBack();
p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
});
test('Keep focus on transition', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/page-with-persistent-form'));
let locator = page.locator('h2');
await expect(locator, 'should have content').toHaveText('Form 1');
locator = page.locator('#input');
await locator.type('Hello');
await expect(locator).toBeFocused();
await locator.press('Enter');
await page.waitForURL(/.*name=Hello/);
locator = page.locator('h2');
await expect(locator, 'should have content').toHaveText('Form 1');
locator = page.locator('#input');
await expect(locator).toBeFocused();
await locator.type(' World');
await expect(locator).toHaveValue('Hello World');
});
test('form POST that redirects to another page is handled', async ({ page, astro }) => {
const loads = [];
page.addListener('load', async (p) => {
loads.push(p);
});
await page.goto(astro.resolveUrl('/form-one'));
let locator = page.locator('h2');
await expect(locator, 'should have content').toHaveText('Contact Form');
// Submit the form
await page.click('#submit');
const span = page.locator('#contact-name');
await expect(span, 'should have content').toHaveText('Testing');
expect(
loads.length,
'There should be only 1 page load. No additional loads for the form submission'
).toEqual(1);
});
test('form GET that redirects to another page is handled', async ({ page, astro }) => {
const loads = [];
page.addListener('load', async (p) => {
loads.push(p);
});
await page.goto(astro.resolveUrl('/form-one?method=get'));
let locator = page.locator('h2');
await expect(locator, 'should have content').toHaveText('Contact Form');
// Submit the form
await page.click('#submit');
const span = page.locator('#contact-name');
await expect(span, 'should have content').toHaveText('Testing');
expect(
loads.length,
'There should be only 1 page load. No additional loads for the form submission'
).toEqual(1);
});
test('form POST when there is an error shows the error', async ({ page, astro }) => {
const loads = [];
page.addListener('load', async (p) => {
loads.push(p);
});
await page.goto(astro.resolveUrl('/form-one?throw'));
let locator = page.locator('h2');
await expect(locator, 'should have content').toHaveText('Contact Form');
// Submit the form
await page.click('#submit');
const overlay = page.locator('vite-error-overlay');
await expect(overlay).toBeVisible();
expect(
loads.length,
'There should be only 1 page load. No additional loads for the form submission'
).toEqual(1);
});
test('Route announcer is invisible on page transition', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/no-directive-one'));
let locator = page.locator('#one');
await expect(locator, 'should have content').toHaveText('One');
await page.click('a');
locator = page.locator('#two');
await expect(locator, 'should have content').toHaveText('Two');
let announcer = page.locator('.astro-route-announcer');
await expect(announcer, 'should have content').toHaveCSS('width', '1px');
});
test('should prefetch on hover by default', async ({ page, astro }) => {
/** @type {string[]} */
const reqUrls = [];
page.on('request', (req) => {
reqUrls.push(new URL(req.url()).pathname);
});
await page.goto(astro.resolveUrl('/prefetch'));
expect(reqUrls).not.toContainEqual('/one');