/
index.test.ts
814 lines (730 loc) · 26.7 KB
/
index.test.ts
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
import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils'
createNextDescribe(
'app dir css',
{
files: __dirname,
skipDeployment: true,
dependencies: {
'@picocss/pico': '1.5.7',
react: 'latest',
'react-dom': 'latest',
sass: 'latest',
'@next/mdx': 'canary',
},
},
({ next, isNextDev: isDev }) => {
describe('css support', () => {
describe('server layouts', () => {
it.skip('should support global css inside server layouts', async () => {
const browser = await next.browser('/dashboard')
// Should body text in red
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('.p')).color`
),
'rgb(255, 0, 0)'
)
// Should inject global css for .green selectors
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('.green')).color`
),
'rgb(0, 128, 0)'
)
})
it('should support css modules inside server layouts', async () => {
const browser = await next.browser('/css/css-nested')
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('#server-cssm')).color`
),
'rgb(0, 128, 0)'
)
})
it('should support external css imports', async () => {
const browser = await next.browser('/css/css-external')
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('main')).paddingTop`
),
'80px'
)
})
})
describe('server pages', () => {
it('should support global css inside server pages', async () => {
const browser = await next.browser('/css/css-page')
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
),
'rgb(255, 0, 0)'
)
})
it('should support css modules inside server pages', async () => {
const browser = await next.browser('/css/css-page')
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('#cssm')).color`
),
'rgb(0, 0, 255)'
)
})
it('should not contain pages css in app dir page', async () => {
const html = await next.render('/css/css-page')
expect(html).not.toContain('/pages/_app.css')
})
})
describe('client layouts', () => {
it('should support css modules inside client layouts', async () => {
const browser = await next.browser('/client-nested')
// Should render h1 in red
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
),
'rgb(255, 0, 0)'
)
})
it('should support global css inside client layouts', async () => {
const browser = await next.browser('/client-nested')
// Should render button in red
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('button')).color`
),
'rgb(255, 0, 0)'
)
})
})
describe('client pages', () => {
it('should support css modules inside client pages', async () => {
const browser = await next.browser('/client-component-route')
// Should render p in red
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('p')).color`
),
'rgb(255, 0, 0)'
)
})
it('should support global css inside client pages', async () => {
const browser = await next.browser('/client-component-route')
// Should render `b` in blue
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('b')).color`
),
'rgb(0, 0, 255)'
)
})
})
describe('client components', () => {
it('should support css modules inside client page', async () => {
const browser = await next.browser('/css/css-client')
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('#css-modules')).fontSize`
),
'100px'
)
})
it('should support css modules inside client components', async () => {
const browser = await next.browser('/css/css-client/inner')
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('#client-component')).fontSize`
),
'100px'
)
})
})
describe('special entries', () => {
it('should include css imported in loading.js', async () => {
const $ = await next.render$('/loading-bug/hi')
// The link tag should be included together with loading with "next" precedence
expect($('link[data-precedence="next"]').length).toBe(1)
expect($('body h2').text()).toBe('Loading...')
})
it('should include css imported in client template.js', async () => {
const browser = await next.browser('/template/clientcomponent')
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('button')).fontSize`
),
'100px'
)
})
it('should include css imported in server template.js', async () => {
const browser = await next.browser('/template/servercomponent')
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
),
'rgb(255, 0, 0)'
)
})
it('should include css imported in client not-found.js', async () => {
const browser = await next.browser('/not-found/clientcomponent')
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
),
'rgb(255, 0, 0)'
)
})
it('should include css imported in server not-found.js', async () => {
const browser = await next.browser('/not-found/servercomponent')
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
),
'rgb(255, 0, 0)'
)
})
it('should include root layout css for root not-found.js', async () => {
const browser = await next.browser('/this-path-does-not-exist')
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
),
'rgb(210, 105, 30)'
)
})
it('should include css imported in root not-found.js', async () => {
const browser = await next.browser('/random-non-existing-path')
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
),
'rgb(210, 105, 30)'
)
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).backgroundColor`
),
'rgb(0, 0, 0)'
)
})
it('should include css imported in error.js', async () => {
const browser = await next.browser('/error/client-component')
await browser.elementByCss('button').click()
// Wait for error page to render and CSS to be loaded
await new Promise((resolve) => setTimeout(resolve, 2000))
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('button')).fontSize`
),
'50px'
)
})
})
describe('page extensions', () => {
it('should include css imported in MDX pages', async () => {
const browser = await next.browser('/mdx')
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
),
'rgb(255, 0, 0)'
)
})
})
describe('chunks', () => {
it('should bundle css resources into chunks', async () => {
const html = await next.render('/dashboard')
expect(
[
...html.matchAll(
/<link rel="stylesheet" href="[^.]+\.css(\?v=\d+)?"/g
),
].length
).toBe(3)
})
})
describe('css ordering', () => {
it('should have inner layers take precedence over outer layers', async () => {
const browser = await next.browser('/ordering')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
)
).toBe('rgb(255, 0, 0)')
})
})
if (isDev) {
it('should not affect css orders during HMR', async () => {
const filePath = 'app/ordering/page.js'
const origContent = await next.readFile(filePath)
// h1 should be red
const browser = await next.browser('/ordering')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
)
).toBe('rgb(255, 0, 0)')
try {
await next.patchFile(
filePath,
origContent.replace('<h1>Hello</h1>', '<h1>Hello!</h1>')
)
// Wait for HMR to trigger
await check(
() => browser.eval(`document.querySelector('h1').textContent`),
'Hello!'
)
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
)
).toBe('rgb(255, 0, 0)')
} finally {
await next.patchFile(filePath, origContent)
}
})
it('should not preload styles twice during HMR', async () => {
const filePath = 'app/hmr/page.js'
const origContent = await next.readFile(filePath)
const browser = await next.browser('/hmr')
try {
await next.patchFile(
filePath,
origContent.replace(
'<div>hello!</div>',
'<div>hello world!</div>'
)
)
// Wait for HMR to trigger
await check(
() => browser.elementByCss('body').text(),
'hello world!'
)
// there should be only 1 preload link
expect(
await browser.eval(
`document.querySelectorAll("link[rel=preload][href^='/_next/static/css/app/layout.css']").length`
)
).toBe(1)
} finally {
await next.patchFile(filePath, origContent)
}
})
it('should reload @import styles during HMR', async () => {
const filePath = 'app/hmr/import/actual-styles.css'
const origContent = await next.readFile(filePath)
// background should be red
const browser = await next.browser('/hmr/import')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('body')).backgroundColor`
)
).toBe('rgb(255, 0, 0)')
try {
await next.patchFile(
filePath,
origContent.replace(
'background-color: red;',
'background-color: blue;'
)
)
// Wait for HMR to trigger
await check(
() =>
browser.eval(
`window.getComputedStyle(document.querySelector('body')).backgroundColor`
),
'rgb(0, 0, 255)'
)
} finally {
await next.patchFile(filePath, origContent)
}
})
describe('multiple entries', () => {
it.skip('should only inject the same style once if used by different layers', async () => {
const browser = await next.browser('/css/css-duplicate-2/client')
expect(
await browser.eval(
`[...document.styleSheets].filter(({ cssRules }) =>
[...cssRules].some(({ cssText }) => (cssText||'').includes('_randomized_string_for_testing_'))
).length`
)
).toBe(1)
})
it('should deduplicate styles on the module level', async () => {
const browser = await next.browser('/css/css-conflict-layers')
await check(
() =>
browser.eval(
`window.getComputedStyle(document.querySelector('.btn:not(.btn-blue)')).backgroundColor`
),
'rgb(255, 255, 255)'
)
await check(
() =>
browser.eval(
`window.getComputedStyle(document.querySelector('.btn.btn-blue')).backgroundColor`
),
'rgb(0, 0, 255)'
)
})
it('should only include the same style once in the flight data', async () => {
const initialHtml = await next.render('/css/css-duplicate-2/server')
// Even if it's deduped by Float, it should still only be included once in the payload.
// There are 3 matches, one for the rendered <link>, one for float preload and one for the <link> inside flight payload.
expect(
initialHtml.match(/css-duplicate-2\/layout\.css\?v=/g).length
).toBe(3)
})
it.skip('should only load chunks for the css module that is used by the specific entrypoint', async () => {
// Visit /b first
await next.render('/css/css-duplicate/b')
const browser = await next.browser('/css/css-duplicate/a')
expect(
await browser.eval(
`[...document.styleSheets].some(({ href }) => href.includes('/a/page.css'))`
)
).toBe(true)
// Should not load the chunk from /b
expect(
await browser.eval(
`[...document.styleSheets].some(({ href }) => href.includes('/b/page.css'))`
)
).toBe(false)
})
})
}
})
describe('sass support', () => {
describe('server layouts', () => {
it('should support global sass/scss inside server layouts', async () => {
const browser = await next.browser('/css/sass/inner')
// .sass
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#sass-server-layout')).color`
)
).toBe('rgb(165, 42, 42)')
// .scss
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#scss-server-layout')).color`
)
).toBe('rgb(222, 184, 135)')
})
it('should support sass/scss modules inside server layouts', async () => {
const browser = await next.browser('/css/sass/inner')
// .sass
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#sass-server-layout')).backgroundColor`
)
).toBe('rgb(233, 150, 122)')
// .scss
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#scss-server-layout')).backgroundColor`
)
).toBe('rgb(139, 0, 0)')
})
})
describe('server pages', () => {
it('should support global sass/scss inside server pages', async () => {
const browser = await next.browser('/css/sass/inner')
// .sass
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#sass-server-page')).color`
)
).toBe('rgb(245, 222, 179)')
// .scss
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#scss-server-page')).color`
)
).toBe('rgb(255, 99, 71)')
})
it('should support sass/scss modules inside server pages', async () => {
const browser = await next.browser('/css/sass/inner')
// .sass
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#sass-server-page')).backgroundColor`
)
).toBe('rgb(75, 0, 130)')
// .scss
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#scss-server-page')).backgroundColor`
)
).toBe('rgb(0, 255, 255)')
})
})
describe('client layouts', () => {
it('should support global sass/scss inside client layouts', async () => {
const browser = await next.browser('/css/sass-client/inner')
// .sass
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#sass-client-layout')).color`
)
).toBe('rgb(165, 42, 42)')
// .scss
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#scss-client-layout')).color`
)
).toBe('rgb(222, 184, 135)')
})
it('should support sass/scss modules inside client layouts', async () => {
const browser = await next.browser('/css/sass-client/inner')
// .sass
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#sass-client-layout')).backgroundColor`
)
).toBe('rgb(233, 150, 122)')
// .scss
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#scss-client-layout')).backgroundColor`
)
).toBe('rgb(139, 0, 0)')
})
})
describe('client pages', () => {
it('should support global sass/scss inside client pages', async () => {
const browser = await next.browser('/css/sass-client/inner')
// .sass
await check(
() =>
browser.eval(
`window.getComputedStyle(document.querySelector('#sass-client-page')).color`
),
'rgb(245, 222, 179)'
)
// .scss
await check(
() =>
browser.eval(
`window.getComputedStyle(document.querySelector('#scss-client-page')).color`
),
'rgb(255, 99, 71)'
)
})
it('should support sass/scss modules inside client pages', async () => {
const browser = await next.browser('/css/sass-client/inner')
// .sass
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#sass-client-page')).backgroundColor`
)
).toBe('rgb(75, 0, 130)')
// .scss
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#scss-client-page')).backgroundColor`
)
).toBe('rgb(0, 255, 255)')
})
})
})
// Pages directory shouldn't be affected when `appDir` is enabled
describe('pages dir', () => {
if (!isDev) {
it('should include css modules and global css after page transition', async () => {
const browser = await next.browser('/css-modules/page1')
await browser.elementByCss('a').click()
await browser.waitForElementByCss('#page2')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).backgroundColor`
)
).toBe('rgb(205, 92, 92)')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('.page-2')).backgroundColor`
)
).toBe('rgb(255, 228, 181)')
})
}
})
describe('HMR', () => {
if (isDev) {
it('should support HMR for CSS imports in server components', async () => {
const filePath = 'app/css/css-page/style.css'
const origContent = await next.readFile(filePath)
// h1 should be red
const browser = await next.browser('/css/css-page')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
)
).toBe('rgb(255, 0, 0)')
try {
await next.patchFile(filePath, origContent.replace('red', 'blue'))
// Wait for HMR to trigger
await check(
() =>
browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
),
'rgb(0, 0, 255)'
)
} finally {
await next.patchFile(filePath, origContent)
}
})
it('should support HMR for CSS imports in client components', async () => {
const filePath = 'app/css/css-client/client-page.css'
const origContent = await next.readFile(filePath)
// h1 should be red
const browser = await next.browser('/css/css-client')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
)
).toBe('rgb(255, 0, 0)')
try {
await next.patchFile(filePath, origContent.replace('red', 'blue'))
await check(
() =>
browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
),
'rgb(0, 0, 255)'
)
} finally {
await next.patchFile(filePath, origContent)
}
})
it('should not break HMR when CSS is imported in a server component', async () => {
const filePath = 'app/hmr/page.js'
const origContent = await next.readFile(filePath)
const browser = await next.browser('/hmr')
await browser.eval(`window.__v = 1`)
try {
await next.patchFile(
filePath,
origContent.replace('hello!', 'hmr!')
)
await check(() => browser.elementByCss('body').text(), 'hmr!')
// Make sure it doesn't reload the page
expect(await browser.eval(`window.__v`)).toBe(1)
} finally {
await next.patchFile(filePath, origContent)
}
})
it('should not create duplicate link tags during HMR', async () => {
const filePath = 'app/hmr/global.css'
const origContent = await next.readFile(filePath)
const browser = await next.browser('/hmr')
try {
await next.patchFile(
filePath,
origContent.replace('background: gray;', 'background: red;')
)
await check(
() =>
browser.eval(
`window.getComputedStyle(document.querySelector('body')).backgroundColor`
),
'rgb(255, 0, 0)'
)
await check(
() =>
browser.eval(
`document.querySelectorAll('link[rel="stylesheet"][href*="/page.css"]').length`
),
1
)
} finally {
await next.patchFile(filePath, origContent)
}
})
it('should support HMR with sass/scss', async () => {
const filePath1 = 'app/css/sass/global.scss'
const origContent1 = await next.readFile(filePath1)
const filePath2 = 'app/css/sass/global.sass'
const origContent2 = await next.readFile(filePath2)
const browser = await next.browser('/css/sass/inner')
// .scss
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#scss-server-layout')).color`
)
).toBe('rgb(222, 184, 135)')
// .sass
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#sass-server-layout')).color`
)
).toBe('rgb(165, 42, 42)')
try {
await next.patchFile(
filePath1,
origContent1.replace('color: burlywood;', 'color: red;')
)
await check(
() =>
browser.eval(
`window.getComputedStyle(document.querySelector('#scss-server-layout')).color`
),
'rgb(255, 0, 0)'
)
} finally {
await next.patchFile(filePath1, origContent1)
}
try {
await next.patchFile(
filePath2,
origContent2.replace('color: brown', 'color: red')
)
await check(
() =>
browser.eval(
`window.getComputedStyle(document.querySelector('#sass-server-layout')).color`
),
'rgb(255, 0, 0)'
)
} finally {
await next.patchFile(filePath2, origContent2)
}
})
}
})
if (isDev) {
describe('Suspensey CSS', () => {
it('should suspend on CSS imports if its slow on client navigation', async () => {
const browser = await next.browser('/suspensey-css')
await browser.elementByCss('#slow').click()
await check(() => browser.eval(`document.body.innerText`), 'Get back')
await check(async () => {
return await browser.eval(`window.__log`)
}, /background = rgb\(255, 255, 0\)/)
})
})
}
}
)