1
1
import Chance from 'chance'
2
2
import { ExperimentalConfig } from 'next/dist/server/config-shared'
3
- import { getCustomImageResponseHeaders , getRemotePatterns , ImagesConfig } from '../../packages/runtime/src/helpers/utils'
3
+ import {
4
+ getCustomImageResponseHeaders ,
5
+ getRemotePatterns ,
6
+ ImagesConfig ,
7
+ redirectsForNext404Route ,
8
+ } from '../../packages/runtime/src/helpers/utils'
4
9
5
10
const chance = new Chance ( )
6
11
7
12
describe ( 'getCustomImageResponseHeaders' , ( ) => {
8
13
it ( 'returns null when no custom image response headers are found' , ( ) => {
9
- const mockHeaders = [ {
10
- for : '/test' ,
11
- values : {
12
- 'X-Foo' : chance . string ( )
13
- }
14
- } ]
14
+ const mockHeaders = [
15
+ {
16
+ for : '/test' ,
17
+ values : {
18
+ 'X-Foo' : chance . string ( ) ,
19
+ } ,
20
+ } ,
21
+ ]
15
22
16
23
expect ( getCustomImageResponseHeaders ( mockHeaders ) ) . toBe ( null )
17
24
} )
18
25
19
26
it ( 'returns header values when custom image response headers are found' , ( ) => {
20
27
const mockFooValue = chance . string ( )
21
28
22
- const mockHeaders = [ {
23
- for : '/_next/image/' ,
24
- values : {
25
- 'X-Foo' : mockFooValue
26
- }
27
- } ]
29
+ const mockHeaders = [
30
+ {
31
+ for : '/_next/image/' ,
32
+ values : {
33
+ 'X-Foo' : mockFooValue ,
34
+ } ,
35
+ } ,
36
+ ]
28
37
29
38
const result = getCustomImageResponseHeaders ( mockHeaders )
30
39
expect ( result ) . toStrictEqual ( {
@@ -38,31 +47,23 @@ describe('getRemotePatterns', () => {
38
47
let mockImages
39
48
beforeEach ( ( ) => {
40
49
mockExperimentalConfig = {
41
- images : { }
50
+ images : { } ,
42
51
} as ExperimentalConfig
43
-
52
+
44
53
mockImages = {
45
- deviceSizes : [
46
- 640 , 750 , 828 ,
47
- 1080 , 1200 , 1920 ,
48
- 2048 , 3840
49
- ] ,
50
- imageSizes : [
51
- 16 , 32 , 48 , 64 ,
52
- 96 , 128 , 256 , 384
53
- ] ,
54
+ deviceSizes : [ 640 , 750 , 828 , 1080 , 1200 , 1920 , 2048 , 3840 ] ,
55
+ imageSizes : [ 16 , 32 , 48 , 64 , 96 , 128 , 256 , 384 ] ,
54
56
path : '/_next/image' ,
55
57
loader : 'default' ,
56
58
domains : [ ] ,
57
59
disableStaticImages : false ,
58
60
minimumCacheTTL : 60 ,
59
- formats : [ 'image/avif' , 'image/webp' ] ,
61
+ formats : [ 'image/avif' , 'image/webp' ] ,
60
62
dangerouslyAllowSVG : false ,
61
63
contentSecurityPolicy : "script-src 'none'; frame-src 'none'; sandbox;" ,
62
64
unoptimized : false ,
63
- remotePatterns : [ ]
65
+ remotePatterns : [ ] ,
64
66
} as ImagesConfig
65
-
66
67
} )
67
68
68
69
it ( 'returns the remote patterns found under experimental.images' , ( ) => {
@@ -73,7 +74,7 @@ describe('getRemotePatterns', () => {
73
74
} ,
74
75
]
75
76
const result = getRemotePatterns ( mockExperimentalConfig , mockImages )
76
-
77
+
77
78
expect ( result ) . toStrictEqual ( mockExperimentalConfig . images ?. remotePatterns )
78
79
} )
79
80
@@ -85,12 +86,49 @@ describe('getRemotePatterns', () => {
85
86
} ,
86
87
]
87
88
const result = getRemotePatterns ( mockExperimentalConfig , mockImages )
88
-
89
- expect ( result ) . toStrictEqual ( mockImages . remotePatterns )
89
+
90
+ expect ( result ) . toStrictEqual ( mockImages . remotePatterns )
90
91
} )
91
-
92
+
92
93
it ( 'returns an empty array' , ( ) => {
93
94
const result = getRemotePatterns ( mockExperimentalConfig , mockImages )
94
95
expect ( result ) . toStrictEqual ( [ ] )
95
96
} )
96
97
} )
98
+
99
+ describe ( 'redirectsForNext404Route' , ( ) => {
100
+ it ( 'returns static 404 redirects' , ( ) => {
101
+ const mockRoute = {
102
+ route : '/test' ,
103
+ buildId : 'test' ,
104
+ basePath : '' ,
105
+ i18n : null ,
106
+ }
107
+
108
+ expect ( redirectsForNext404Route ( mockRoute ) ) . toStrictEqual ( [
109
+ { force : false , from : '/_next/data/test/test.json' , status : 404 , to : '/server/pages/404.html' } ,
110
+ { force : false , from : '/test' , status : 404 , to : '/server/pages/404.html' } ,
111
+ ] )
112
+ } )
113
+
114
+ it ( 'returns localised static 404 redirects when i18n locales are provided' , ( ) => {
115
+ const mockRoute = {
116
+ route : '/test' ,
117
+ buildId : 'test' ,
118
+ basePath : '' ,
119
+ i18n : {
120
+ defaultLocale : 'en' ,
121
+ locales : [ 'en' , 'es' , 'fr' ] ,
122
+ } ,
123
+ }
124
+
125
+ expect ( redirectsForNext404Route ( mockRoute ) ) . toStrictEqual ( [
126
+ { force : false , from : '/_next/data/test/en/test.json' , status : 404 , to : '/server/pages/en/404.html' } ,
127
+ { force : false , from : '/test' , status : 404 , to : '/server/pages/en/404.html' } ,
128
+ { force : false , from : '/_next/data/test/es/test.json' , status : 404 , to : '/server/pages/es/404.html' } ,
129
+ { force : false , from : '/es/test' , status : 404 , to : '/server/pages/es/404.html' } ,
130
+ { force : false , from : '/_next/data/test/fr/test.json' , status : 404 , to : '/server/pages/fr/404.html' } ,
131
+ { force : false , from : '/fr/test' , status : 404 , to : '/server/pages/fr/404.html' } ,
132
+ ] )
133
+ } )
134
+ } )
0 commit comments