Skip to content

Commit 969b3b6

Browse files
committed
chore: add tests for 404 redirects
1 parent f5c0aa5 commit 969b3b6

File tree

1 file changed

+69
-31
lines changed

1 file changed

+69
-31
lines changed

test/helpers/utils.spec.ts

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
11
import Chance from 'chance'
22
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'
49

510
const chance = new Chance()
611

712
describe('getCustomImageResponseHeaders', () => {
813
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+
]
1522

1623
expect(getCustomImageResponseHeaders(mockHeaders)).toBe(null)
1724
})
1825

1926
it('returns header values when custom image response headers are found', () => {
2027
const mockFooValue = chance.string()
2128

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+
]
2837

2938
const result = getCustomImageResponseHeaders(mockHeaders)
3039
expect(result).toStrictEqual({
@@ -38,31 +47,23 @@ describe('getRemotePatterns', () => {
3847
let mockImages
3948
beforeEach(() => {
4049
mockExperimentalConfig = {
41-
images: {}
50+
images: {},
4251
} as ExperimentalConfig
43-
52+
4453
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],
5456
path: '/_next/image',
5557
loader: 'default',
5658
domains: [],
5759
disableStaticImages: false,
5860
minimumCacheTTL: 60,
59-
formats: [ 'image/avif', 'image/webp' ],
61+
formats: ['image/avif', 'image/webp'],
6062
dangerouslyAllowSVG: false,
6163
contentSecurityPolicy: "script-src 'none'; frame-src 'none'; sandbox;",
6264
unoptimized: false,
63-
remotePatterns: []
65+
remotePatterns: [],
6466
} as ImagesConfig
65-
6667
})
6768

6869
it('returns the remote patterns found under experimental.images', () => {
@@ -73,7 +74,7 @@ describe('getRemotePatterns', () => {
7374
},
7475
]
7576
const result = getRemotePatterns(mockExperimentalConfig, mockImages)
76-
77+
7778
expect(result).toStrictEqual(mockExperimentalConfig.images?.remotePatterns)
7879
})
7980

@@ -85,12 +86,49 @@ describe('getRemotePatterns', () => {
8586
},
8687
]
8788
const result = getRemotePatterns(mockExperimentalConfig, mockImages)
88-
89-
expect(result).toStrictEqual(mockImages.remotePatterns)
89+
90+
expect(result).toStrictEqual(mockImages.remotePatterns)
9091
})
91-
92+
9293
it('returns an empty array', () => {
9394
const result = getRemotePatterns(mockExperimentalConfig, mockImages)
9495
expect(result).toStrictEqual([])
9596
})
9697
})
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

Comments
 (0)