1
1
import { NextRequest , NextFetchEvent , NextResponse } from 'next/server' ;
2
2
3
3
import { STALE_WHILE_REVALIDATE_ONE_HOUR } from '@/cache/constants' ;
4
- import { SUPPORTED_LOCALE_CODES , SUPPORTED_LOCALES_SET } from '@/config/locale' ;
4
+ import {
5
+ SUPPORTED_LOCALE_CODES ,
6
+ SUPPORTED_LOCALES_SET ,
7
+ SupportedLocale ,
8
+ } from '@/config/locale' ;
5
9
import { getStage } from '@/config/stage' ;
6
10
import { getContentfulSlug } from '@/contentful/slug/getContentfulSlug' ;
7
11
@@ -40,15 +44,19 @@ describe('withLocale middleware', () => {
40
44
response : { cookies : { set : jest . fn ( ) } } ,
41
45
} as unknown as NextRequest ;
42
46
43
- SUPPORTED_LOCALES_SET . add ( 'zh-TW' ) ;
47
+ SUPPORTED_LOCALES_SET . add ( 'zh-TW' as SupportedLocale ) ;
44
48
await withLocale ( next ) ( request , mockEvent ) ;
45
49
46
50
expect ( next ) . toHaveBeenCalledWith ( request , mockEvent ) ;
47
51
expect ( getContentfulSlug ) . not . toHaveBeenCalled ( ) ;
48
- expect ( cookieMock . set ) . toHaveBeenCalledWith ( 'language_' , 'zh-TW' , {
49
- domain : undefined ,
50
- path : '/' ,
51
- } ) ;
52
+ expect ( cookieMock . set ) . toHaveBeenCalledWith (
53
+ 'language_' ,
54
+ 'zh-TW' as SupportedLocale ,
55
+ {
56
+ domain : undefined ,
57
+ path : '/' ,
58
+ } ,
59
+ ) ;
52
60
} ) ;
53
61
54
62
it ( 'should not redirect if the path contains a supported locale - development' , async ( ) => {
@@ -60,26 +68,30 @@ describe('withLocale middleware', () => {
60
68
} as unknown as NextRequest ;
61
69
( getStage as jest . Mock ) . mockReturnValue ( 'production' ) ;
62
70
63
- SUPPORTED_LOCALES_SET . add ( 'zh-TW' ) ;
71
+ SUPPORTED_LOCALES_SET . add ( 'zh-TW' as SupportedLocale ) ;
64
72
await withLocale ( next ) ( request , mockEvent ) ;
65
73
66
74
expect ( next ) . toHaveBeenCalledWith ( request , mockEvent ) ;
67
75
expect ( getContentfulSlug ) . not . toHaveBeenCalled ( ) ;
68
- expect ( cookieMock . set ) . toHaveBeenCalledWith ( 'language_' , 'zh-TW' , {
69
- domain : '.code.org' ,
70
- path : '/' ,
71
- } ) ;
76
+ expect ( cookieMock . set ) . toHaveBeenCalledWith (
77
+ 'language_' ,
78
+ 'zh-TW' as SupportedLocale ,
79
+ {
80
+ domain : '.code.org' ,
81
+ path : '/' ,
82
+ } ,
83
+ ) ;
72
84
} ) ;
73
85
74
86
it ( 'should redirect to the locale path if no locale is present in the path for cookies' , async ( ) => {
75
87
const request = {
76
88
url : 'https://test.code.org' ,
77
89
nextUrl : { pathname : '/home' , url : 'https://test.code.org/home' } ,
78
- cookies : { get : jest . fn ( ( ) => ( { value : 'zh-TW' } ) ) } ,
90
+ cookies : { get : jest . fn ( ( ) => ( { value : 'zh-TW' as SupportedLocale } ) ) } ,
79
91
headers : { get : jest . fn ( ) } ,
80
92
} as unknown as NextRequest ;
81
93
82
- SUPPORTED_LOCALES_SET . add ( 'zh-TW' ) ;
94
+ SUPPORTED_LOCALES_SET . add ( 'zh-TW' as SupportedLocale ) ;
83
95
( getContentfulSlug as jest . Mock ) . mockReturnValue ( 'home' ) ;
84
96
85
97
const response = await withLocale ( next ) ( request , mockEvent ) ;
@@ -103,7 +115,7 @@ describe('withLocale middleware', () => {
103
115
} ,
104
116
} as unknown as NextRequest ;
105
117
106
- SUPPORTED_LOCALE_CODES . push ( 'zh-TW' ) ;
118
+ SUPPORTED_LOCALE_CODES . push ( 'zh-TW' as SupportedLocale ) ;
107
119
( getContentfulSlug as jest . Mock ) . mockReturnValue ( 'home' ) ;
108
120
109
121
const response = await withLocale ( next ) ( request , mockEvent ) ;
@@ -127,7 +139,7 @@ describe('withLocale middleware', () => {
127
139
} ,
128
140
} as unknown as NextRequest ;
129
141
130
- SUPPORTED_LOCALE_CODES . push ( 'en-US' ) ;
142
+ SUPPORTED_LOCALE_CODES . push ( 'en-US' as SupportedLocale ) ;
131
143
( getContentfulSlug as jest . Mock ) . mockReturnValue ( 'home' ) ;
132
144
133
145
const response = await withLocale ( next ) ( request , mockEvent ) ;
@@ -182,11 +194,11 @@ describe('withLocale middleware', () => {
182
194
pathname : '/engineering/all-the-things' ,
183
195
url : 'https://test.code.org/engineering/all-the-things' ,
184
196
} ,
185
- cookies : { get : jest . fn ( ( ) => ( { value : 'zh-TW' } ) ) } ,
197
+ cookies : { get : jest . fn ( ( ) => ( { value : 'zh-TW' as SupportedLocale } ) ) } ,
186
198
headers : { get : jest . fn ( ) } ,
187
199
} as unknown as NextRequest ;
188
200
189
- SUPPORTED_LOCALES_SET . add ( 'zh-TW' ) ;
201
+ SUPPORTED_LOCALES_SET . add ( 'zh-TW' as SupportedLocale ) ;
190
202
( getContentfulSlug as jest . Mock ) . mockReturnValue (
191
203
'engineering/all-the-things' ,
192
204
) ;
@@ -198,4 +210,23 @@ describe('withLocale middleware', () => {
198
210
'https://test.code.org/zh-TW/engineering/all-the-things' ,
199
211
) ;
200
212
} ) ;
213
+
214
+ it ( 'should not set language_ cookie or redirect to studio base url when brand is not CODE_DOT_ORG' , async ( ) => {
215
+ const request = {
216
+ nextUrl : { pathname : '/zh-TW/home' } ,
217
+ cookies : { get : jest . fn ( ) } ,
218
+ headers : { get : jest . fn ( ) . mockReturnValue ( 'csforall.org' ) } ,
219
+ response : { cookies : { set : jest . fn ( ) } } ,
220
+ url : 'https://not-code.org/zh-TW/home' ,
221
+ } as unknown as NextRequest ;
222
+
223
+ SUPPORTED_LOCALES_SET . add ( 'zh-TW' as SupportedLocale ) ;
224
+ await withLocale ( next ) ( request , mockEvent ) ;
225
+
226
+ expect ( cookieMock . set ) . not . toHaveBeenCalledWith (
227
+ 'language_' ,
228
+ expect . anything ( ) ,
229
+ expect . anything ( ) ,
230
+ ) ;
231
+ } ) ;
201
232
} ) ;
0 commit comments