@@ -26,7 +26,170 @@ let appPort
2626
2727const locales = [ 'en-US' , 'nl-NL' , 'nl-BE' , 'nl' , 'fr-BE' , 'fr' , 'en' ]
2828
29+ async function addDefaultLocaleCookie ( browser ) {
30+ // make sure default locale is used in case browser isn't set to
31+ // favor en-US by default, (we use all caps to ensure it's case-insensitive)
32+ await browser . manage ( ) . addCookie ( { name : 'NEXT_LOCALE' , value : 'EN-US' } )
33+ await browser . get ( browser . initUrl )
34+ }
35+
2936function runTests ( isDev ) {
37+ it ( 'should navigate with locale prop correctly' , async ( ) => {
38+ const browser = await webdriver ( appPort , '/links?nextLocale=fr' )
39+ await addDefaultLocaleCookie ( browser )
40+
41+ expect ( await browser . elementByCss ( '#router-pathname' ) . text ( ) ) . toBe ( '/links' )
42+ expect ( await browser . elementByCss ( '#router-as-path' ) . text ( ) ) . toBe (
43+ '/links?nextLocale=fr'
44+ )
45+ expect ( await browser . elementByCss ( '#router-locale' ) . text ( ) ) . toBe ( 'en-US' )
46+ expect (
47+ JSON . parse ( await browser . elementByCss ( '#router-locales' ) . text ( ) )
48+ ) . toEqual ( locales )
49+ expect (
50+ JSON . parse ( await browser . elementByCss ( '#router-query' ) . text ( ) )
51+ ) . toEqual ( { nextLocale : 'fr' } )
52+
53+ await browser . elementByCss ( '#to-another' ) . click ( )
54+ await browser . waitForElementByCss ( '#another' )
55+
56+ expect ( await browser . elementByCss ( '#router-pathname' ) . text ( ) ) . toBe (
57+ '/another'
58+ )
59+ expect ( await browser . elementByCss ( '#router-as-path' ) . text ( ) ) . toBe (
60+ '/another'
61+ )
62+ expect ( await browser . elementByCss ( '#router-locale' ) . text ( ) ) . toBe ( 'fr' )
63+ expect (
64+ JSON . parse ( await browser . elementByCss ( '#router-locales' ) . text ( ) )
65+ ) . toEqual ( locales )
66+ expect (
67+ JSON . parse ( await browser . elementByCss ( '#router-query' ) . text ( ) )
68+ ) . toEqual ( { } )
69+
70+ let parsedUrl = url . parse ( await browser . eval ( 'window.location.href' ) , true )
71+ expect ( parsedUrl . pathname ) . toBe ( '/fr/another' )
72+ expect ( parsedUrl . query ) . toEqual ( { } )
73+
74+ await browser . eval ( 'window.history.back()' )
75+ await browser . waitForElementByCss ( '#links' )
76+
77+ expect ( await browser . elementByCss ( '#router-pathname' ) . text ( ) ) . toBe ( '/links' )
78+ expect ( await browser . elementByCss ( '#router-as-path' ) . text ( ) ) . toBe (
79+ '/links?nextLocale=fr'
80+ )
81+ expect ( await browser . elementByCss ( '#router-locale' ) . text ( ) ) . toBe ( 'fr' )
82+ expect (
83+ JSON . parse ( await browser . elementByCss ( '#router-locales' ) . text ( ) )
84+ ) . toEqual ( locales )
85+ expect (
86+ JSON . parse ( await browser . elementByCss ( '#router-query' ) . text ( ) )
87+ ) . toEqual ( { nextLocale : 'fr' } )
88+
89+ parsedUrl = url . parse ( await browser . eval ( 'window.location.href' ) , true )
90+ expect ( parsedUrl . pathname ) . toBe ( '/fr/links' )
91+ expect ( parsedUrl . query ) . toEqual ( { nextLocale : 'fr' } )
92+
93+ await browser . eval ( 'window.history.forward()' )
94+ await browser . waitForElementByCss ( '#another' )
95+
96+ expect ( await browser . elementByCss ( '#router-pathname' ) . text ( ) ) . toBe (
97+ '/another'
98+ )
99+ expect ( await browser . elementByCss ( '#router-as-path' ) . text ( ) ) . toBe (
100+ '/another'
101+ )
102+ expect ( await browser . elementByCss ( '#router-locale' ) . text ( ) ) . toBe ( 'fr' )
103+ expect (
104+ JSON . parse ( await browser . elementByCss ( '#router-locales' ) . text ( ) )
105+ ) . toEqual ( locales )
106+ expect (
107+ JSON . parse ( await browser . elementByCss ( '#router-query' ) . text ( ) )
108+ ) . toEqual ( { } )
109+
110+ parsedUrl = url . parse ( await browser . eval ( 'window.location.href' ) , true )
111+ expect ( parsedUrl . pathname ) . toBe ( '/fr/another' )
112+ expect ( parsedUrl . query ) . toEqual ( { } )
113+ } )
114+
115+ it ( 'should navigate with locale prop correctly GSP' , async ( ) => {
116+ const browser = await webdriver ( appPort , '/links?nextLocale=nl' )
117+ await addDefaultLocaleCookie ( browser )
118+
119+ expect ( await browser . elementByCss ( '#router-pathname' ) . text ( ) ) . toBe ( '/links' )
120+ expect ( await browser . elementByCss ( '#router-as-path' ) . text ( ) ) . toBe (
121+ '/links?nextLocale=nl'
122+ )
123+ expect ( await browser . elementByCss ( '#router-locale' ) . text ( ) ) . toBe ( 'en-US' )
124+ expect (
125+ JSON . parse ( await browser . elementByCss ( '#router-locales' ) . text ( ) )
126+ ) . toEqual ( locales )
127+ expect (
128+ JSON . parse ( await browser . elementByCss ( '#router-query' ) . text ( ) )
129+ ) . toEqual ( { nextLocale : 'nl' } )
130+
131+ await browser . elementByCss ( '#to-fallback-first' ) . click ( )
132+ await browser . waitForElementByCss ( '#gsp' )
133+
134+ expect ( await browser . elementByCss ( '#router-pathname' ) . text ( ) ) . toBe (
135+ '/gsp/fallback/[slug]'
136+ )
137+ expect ( await browser . elementByCss ( '#router-as-path' ) . text ( ) ) . toBe (
138+ '/gsp/fallback/first'
139+ )
140+ expect ( await browser . elementByCss ( '#router-locale' ) . text ( ) ) . toBe ( 'nl' )
141+ expect (
142+ JSON . parse ( await browser . elementByCss ( '#router-locales' ) . text ( ) )
143+ ) . toEqual ( locales )
144+ expect (
145+ JSON . parse ( await browser . elementByCss ( '#router-query' ) . text ( ) )
146+ ) . toEqual ( { slug : 'first' } )
147+
148+ let parsedUrl = url . parse ( await browser . eval ( 'window.location.href' ) , true )
149+ expect ( parsedUrl . pathname ) . toBe ( '/nl/gsp/fallback/first' )
150+ expect ( parsedUrl . query ) . toEqual ( { } )
151+
152+ await browser . eval ( 'window.history.back()' )
153+ await browser . waitForElementByCss ( '#links' )
154+
155+ expect ( await browser . elementByCss ( '#router-pathname' ) . text ( ) ) . toBe ( '/links' )
156+ expect ( await browser . elementByCss ( '#router-as-path' ) . text ( ) ) . toBe (
157+ '/links?nextLocale=nl'
158+ )
159+ expect ( await browser . elementByCss ( '#router-locale' ) . text ( ) ) . toBe ( 'nl' )
160+ expect (
161+ JSON . parse ( await browser . elementByCss ( '#router-locales' ) . text ( ) )
162+ ) . toEqual ( locales )
163+ expect (
164+ JSON . parse ( await browser . elementByCss ( '#router-query' ) . text ( ) )
165+ ) . toEqual ( { nextLocale : 'nl' } )
166+
167+ parsedUrl = url . parse ( await browser . eval ( 'window.location.href' ) , true )
168+ expect ( parsedUrl . pathname ) . toBe ( '/nl/links' )
169+ expect ( parsedUrl . query ) . toEqual ( { nextLocale : 'nl' } )
170+
171+ await browser . eval ( 'window.history.forward()' )
172+ await browser . waitForElementByCss ( '#gsp' )
173+
174+ expect ( await browser . elementByCss ( '#router-pathname' ) . text ( ) ) . toBe (
175+ '/gsp/fallback/[slug]'
176+ )
177+ expect ( await browser . elementByCss ( '#router-as-path' ) . text ( ) ) . toBe (
178+ '/gsp/fallback/first'
179+ )
180+ expect ( await browser . elementByCss ( '#router-locale' ) . text ( ) ) . toBe ( 'nl' )
181+ expect (
182+ JSON . parse ( await browser . elementByCss ( '#router-locales' ) . text ( ) )
183+ ) . toEqual ( locales )
184+ expect (
185+ JSON . parse ( await browser . elementByCss ( '#router-query' ) . text ( ) )
186+ ) . toEqual ( { slug : 'first' } )
187+
188+ parsedUrl = url . parse ( await browser . eval ( 'window.location.href' ) , true )
189+ expect ( parsedUrl . pathname ) . toBe ( '/nl/gsp/fallback/first' )
190+ expect ( parsedUrl . query ) . toEqual ( { } )
191+ } )
192+
30193 it ( 'should update asPath on the client correctly' , async ( ) => {
31194 for ( const check of [ 'en' , 'En' ] ) {
32195 const browser = await webdriver ( appPort , `/${ check } ` )
@@ -509,10 +672,7 @@ function runTests(isDev) {
509672
510673 it ( 'should navigate client side for default locale with no prefix' , async ( ) => {
511674 const browser = await webdriver ( appPort , '/' )
512- // make sure default locale is used in case browser isn't set to
513- // favor en-US by default, (we use all caps to ensure it's case-insensitive)
514- await browser . manage ( ) . addCookie ( { name : 'NEXT_LOCALE' , value : 'EN-US' } )
515- await browser . get ( browser . initUrl )
675+ await addDefaultLocaleCookie ( browser )
516676
517677 const checkIndexValues = async ( ) => {
518678 expect ( await browser . elementByCss ( '#router-locale' ) . text ( ) ) . toBe ( 'en-US' )
0 commit comments