Skip to content

Commit 9746cf5

Browse files
authored
feat: add no vary on root index test (#31)
1 parent 8ad5af4 commit 9746cf5

File tree

2 files changed

+79
-13
lines changed

2 files changed

+79
-13
lines changed

.eslintrc.cjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,12 @@ module.exports = {
2525
'import/no-anonymous-default-export': 'off',
2626
},
2727
},
28+
{
29+
files: ['src/**/*.test.*'],
30+
rules: {
31+
'max-statements': off,
32+
'max-lines-per-function': off,
33+
},
34+
},
2835
],
2936
}

src/helpers/headers.test.ts

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,26 @@ describe('headers', () => {
2222

2323
setVaryHeaders(headers, request, defaultConfig)
2424

25-
expect(headers.set).toBeCalledWith('netlify-vary', 'cookie=__prerender_bypass|__next_preview_data')
25+
expect(headers.set).toBeCalledWith(
26+
'netlify-vary',
27+
'cookie=__prerender_bypass|__next_preview_data',
28+
)
2629
})
2730

2831
test('with expected vary headers', () => {
2932
const givenHeaders = {
30-
'vary': 'Accept, Accept-Language',
33+
vary: 'Accept, Accept-Language',
3134
}
3235
const headers = new Headers(givenHeaders)
3336
const request = new Request(defaultUrl)
3437
vi.spyOn(headers, 'set')
3538

3639
setVaryHeaders(headers, request, defaultConfig)
3740

38-
expect(headers.set).toBeCalledWith('netlify-vary', 'header=Accept|Accept-Language,cookie=__prerender_bypass|__next_preview_data')
41+
expect(headers.set).toBeCalledWith(
42+
'netlify-vary',
43+
'header=Accept|Accept-Language,cookie=__prerender_bypass|__next_preview_data',
44+
)
3945
})
4046

4147
test('with no languages if i18n config has localeDetection disabled', () => {
@@ -53,7 +59,30 @@ describe('headers', () => {
5359

5460
setVaryHeaders(headers, request, config)
5561

56-
expect(headers.set).toBeCalledWith('netlify-vary', 'cookie=__prerender_bypass|__next_preview_data')
62+
expect(headers.set).toBeCalledWith(
63+
'netlify-vary',
64+
'cookie=__prerender_bypass|__next_preview_data',
65+
)
66+
})
67+
68+
test('with no languages if path is root index', () => {
69+
const headers = new Headers()
70+
const request = new Request(`${defaultUrl}/another/path`)
71+
const config = {
72+
...defaultConfig,
73+
i18n: {
74+
locales: ['en', 'de'],
75+
defaultLocale: 'default',
76+
},
77+
}
78+
vi.spyOn(headers, 'set')
79+
80+
setVaryHeaders(headers, request, config)
81+
82+
expect(headers.set).toBeCalledWith(
83+
'netlify-vary',
84+
'cookie=__prerender_bypass|__next_preview_data',
85+
)
5786
})
5887

5988
test('with expected languages if i18n config has locales', () => {
@@ -70,7 +99,10 @@ describe('headers', () => {
7099

71100
setVaryHeaders(headers, request, config)
72101

73-
expect(headers.set).toBeCalledWith('netlify-vary', 'language=en|de|fr,cookie=__prerender_bypass|__next_preview_data|NEXT_LOCALE')
102+
expect(headers.set).toBeCalledWith(
103+
'netlify-vary',
104+
'language=en|de|fr,cookie=__prerender_bypass|__next_preview_data|NEXT_LOCALE',
105+
)
74106
})
75107

76108
test('with expected languages if i18n config has locales and basePath matches the current path', () => {
@@ -88,7 +120,10 @@ describe('headers', () => {
88120

89121
setVaryHeaders(headers, request, config)
90122

91-
expect(headers.set).toBeCalledWith('netlify-vary', 'language=en|de|fr,cookie=__prerender_bypass|__next_preview_data|NEXT_LOCALE')
123+
expect(headers.set).toBeCalledWith(
124+
'netlify-vary',
125+
'language=en|de|fr,cookie=__prerender_bypass|__next_preview_data|NEXT_LOCALE',
126+
)
92127
})
93128
})
94129
})
@@ -138,8 +173,16 @@ describe('headers', () => {
138173

139174
setCacheControlHeaders(headers)
140175

141-
expect(headers.set).toHaveBeenNthCalledWith(1, 'cache-control', 'public, max-age=0, must-revalidate')
142-
expect(headers.set).toHaveBeenNthCalledWith(2, 'netlify-cdn-cache-control', 'public, max-age=0, must-revalidate')
176+
expect(headers.set).toHaveBeenNthCalledWith(
177+
1,
178+
'cache-control',
179+
'public, max-age=0, must-revalidate',
180+
)
181+
expect(headers.set).toHaveBeenNthCalledWith(
182+
2,
183+
'netlify-cdn-cache-control',
184+
'public, max-age=0, must-revalidate',
185+
)
143186
})
144187

145188
test('should remove "s-maxage" from "cache-control" header', () => {
@@ -152,7 +195,11 @@ describe('headers', () => {
152195
setCacheControlHeaders(headers)
153196

154197
expect(headers.set).toHaveBeenNthCalledWith(1, 'cache-control', 'public')
155-
expect(headers.set).toHaveBeenNthCalledWith(2, 'netlify-cdn-cache-control', 'public, s-maxage=604800')
198+
expect(headers.set).toHaveBeenNthCalledWith(
199+
2,
200+
'netlify-cdn-cache-control',
201+
'public, s-maxage=604800',
202+
)
156203
})
157204

158205
test('should remove "stale-while-revalidate" from "cache-control" header', () => {
@@ -165,10 +212,14 @@ describe('headers', () => {
165212
setCacheControlHeaders(headers)
166213

167214
expect(headers.set).toHaveBeenNthCalledWith(1, 'cache-control', 'max-age=604800')
168-
expect(headers.set).toHaveBeenNthCalledWith(2, 'netlify-cdn-cache-control', 'max-age=604800, stale-while-revalidate=86400')
215+
expect(headers.set).toHaveBeenNthCalledWith(
216+
2,
217+
'netlify-cdn-cache-control',
218+
'max-age=604800, stale-while-revalidate=86400',
219+
)
169220
})
170221

171-
test('should set default "cache-control" header if it contains both "s-maxage" and "stale-whie-revalidate"', () => {
222+
test('should set default "cache-control" header if it contains only "s-maxage" and "stale-whie-revalidate"', () => {
172223
const givenHeaders = {
173224
'cache-control': 's-maxage=604800, stale-while-revalidate=86400',
174225
}
@@ -177,8 +228,16 @@ describe('headers', () => {
177228

178229
setCacheControlHeaders(headers)
179230

180-
expect(headers.set).toHaveBeenNthCalledWith(1, 'cache-control', 'public, max-age=0, must-revalidate')
181-
expect(headers.set).toHaveBeenNthCalledWith(2, 'netlify-cdn-cache-control', 's-maxage=604800, stale-while-revalidate=86400')
231+
expect(headers.set).toHaveBeenNthCalledWith(
232+
1,
233+
'cache-control',
234+
'public, max-age=0, must-revalidate',
235+
)
236+
expect(headers.set).toHaveBeenNthCalledWith(
237+
2,
238+
'netlify-cdn-cache-control',
239+
's-maxage=604800, stale-while-revalidate=86400',
240+
)
182241
})
183242
})
184243

0 commit comments

Comments
 (0)