@@ -20,8 +20,8 @@ describe('createClientUatCookie', () => {
2020 const mockExpires = new Date ( '2024-12-31' ) ;
2121 const mockDomain = 'test.domain' ;
2222 const defaultOptions = { usePartitionedCookies : ( ) => false } ;
23- const mockSet = vi . fn ( ) ;
24- const mockRemove = vi . fn ( ) ;
23+ const mockSet = vi . fn < ( name : string , value : string , attributes ?: object ) => void > ( ) ;
24+ const mockRemove = vi . fn < ( name : string , attributes ?: object ) => void > ( ) ;
2525 const mockGet = vi . fn ( ) ;
2626
2727 beforeEach ( ( ) => {
@@ -32,9 +32,13 @@ describe('createClientUatCookie', () => {
3232 ( requiresSameSiteNone as ReturnType < typeof vi . fn > ) . mockReturnValue ( false ) ;
3333 ( getCookieDomain as ReturnType < typeof vi . fn > ) . mockReturnValue ( mockDomain ) ;
3434 ( getSecureAttribute as ReturnType < typeof vi . fn > ) . mockReturnValue ( true ) ;
35- ( createCookieHandler as ReturnType < typeof vi . fn > ) . mockImplementation ( ( ) => ( {
36- set : mockSet ,
37- remove : mockRemove ,
35+ ( createCookieHandler as ReturnType < typeof vi . fn > ) . mockImplementation ( ( name : string ) => ( {
36+ set : ( value : string , attributes ?: object ) => {
37+ mockSet ( name , value , attributes ) ;
38+ } ,
39+ remove : ( attributes ?: object ) => {
40+ mockRemove ( name , attributes ) ;
41+ } ,
3842 get : mockGet ,
3943 } ) ) ;
4044 } ) ;
@@ -55,13 +59,14 @@ describe('createClientUatCookie', () => {
5559 } ) ;
5660
5761 expect ( mockSet ) . toHaveBeenCalledTimes ( 2 ) ;
58- expect ( mockSet ) . toHaveBeenCalledWith ( '1704067200' , {
62+ expect ( mockSet ) . toHaveBeenCalledWith ( '__client_uat_test-suffix' , ' 1704067200', {
5963 domain : mockDomain ,
6064 expires : mockExpires ,
6165 sameSite : 'Strict' ,
6266 secure : true ,
6367 partitioned : false ,
6468 } ) ;
69+ expect ( mockSet ) . toHaveBeenCalledWith ( '__client_uat' , '1704067200' , expect . any ( Object ) ) ;
6570 } ) ;
6671
6772 it ( 'should set cookies with None sameSite in cross-origin context' , ( ) => {
@@ -73,7 +78,7 @@ describe('createClientUatCookie', () => {
7378 signedInSessions : [ 'session1' ] ,
7479 } ) ;
7580
76- expect ( mockSet ) . toHaveBeenCalledWith ( '1704067200' , {
81+ expect ( mockSet ) . toHaveBeenCalledWith ( '__client_uat_test-suffix' , ' 1704067200', {
7782 domain : mockDomain ,
7883 expires : mockExpires ,
7984 sameSite : 'None' ,
@@ -86,7 +91,7 @@ describe('createClientUatCookie', () => {
8691 const cookieHandler = createClientUatCookie ( mockCookieSuffix , defaultOptions ) ;
8792 cookieHandler . set ( undefined ) ;
8893
89- expect ( mockSet ) . toHaveBeenCalledWith ( '0' , {
94+ expect ( mockSet ) . toHaveBeenCalledWith ( '__client_uat_test-suffix' , ' 0', {
9095 domain : mockDomain ,
9196 expires : mockExpires ,
9297 sameSite : 'Strict' ,
@@ -103,7 +108,7 @@ describe('createClientUatCookie', () => {
103108 signedInSessions : [ ] ,
104109 } ) ;
105110
106- expect ( mockSet ) . toHaveBeenCalledWith ( '0' , {
111+ expect ( mockSet ) . toHaveBeenCalledWith ( '__client_uat_test-suffix' , ' 0', {
107112 domain : mockDomain ,
108113 expires : mockExpires ,
109114 sameSite : 'Strict' ,
@@ -139,7 +144,7 @@ describe('createClientUatCookie', () => {
139144 signedInSessions : [ 'session1' ] ,
140145 } ) ;
141146
142- expect ( mockSet ) . toHaveBeenCalledWith ( '1704067200' , {
147+ expect ( mockSet ) . toHaveBeenCalledWith ( '__client_uat_test-suffix' , ' 1704067200', {
143148 domain : mockDomain ,
144149 expires : mockExpires ,
145150 sameSite : 'None' ,
@@ -156,12 +161,73 @@ describe('createClientUatCookie', () => {
156161 signedInSessions : [ 'session1' ] ,
157162 } ) ;
158163
159- expect ( mockSet ) . toHaveBeenCalledWith ( '1704067200' , {
164+ expect ( mockSet ) . toHaveBeenCalledWith ( '__client_uat_test-suffix' , ' 1704067200', {
160165 domain : mockDomain ,
161166 expires : mockExpires ,
162167 sameSite : 'None' ,
163168 secure : true ,
164169 partitioned : true ,
165170 } ) ;
166171 } ) ;
172+
173+ it ( 'clears non-partitioned domain variants before writing partitioned cookies' , ( ) => {
174+ let usePartitionedCookies = false ;
175+ const cookieHandler = createClientUatCookie ( mockCookieSuffix , {
176+ usePartitionedCookies : ( ) => usePartitionedCookies ,
177+ } ) ;
178+ const client = {
179+ id : 'test-client' ,
180+ updatedAt : new Date ( '2024-01-01' ) ,
181+ signedInSessions : [ 'session1' ] ,
182+ } ;
183+
184+ cookieHandler . set ( client ) ;
185+ usePartitionedCookies = true ;
186+ mockSet . mockClear ( ) ;
187+ mockRemove . mockClear ( ) ;
188+ cookieHandler . set ( client ) ;
189+
190+ expect ( mockRemove . mock . calls ) . toEqual ( [
191+ [ '__client_uat_test-suffix' , undefined ] ,
192+ [ '__client_uat' , undefined ] ,
193+ [ '__client_uat_test-suffix' , { domain : mockDomain , sameSite : 'Strict' , secure : true , partitioned : false } ] ,
194+ [ '__client_uat' , { domain : mockDomain , sameSite : 'Strict' , secure : true , partitioned : false } ] ,
195+ [ '__client_uat_test-suffix' , { domain : mockDomain , sameSite : 'None' , secure : true , partitioned : false } ] ,
196+ [ '__client_uat' , { domain : mockDomain , sameSite : 'None' , secure : true , partitioned : false } ] ,
197+ ] ) ;
198+ expect ( mockSet . mock . calls ) . toEqual ( [
199+ [
200+ '__client_uat_test-suffix' ,
201+ '1704067200' ,
202+ {
203+ domain : mockDomain ,
204+ expires : mockExpires ,
205+ sameSite : 'None' ,
206+ secure : true ,
207+ partitioned : true ,
208+ } ,
209+ ] ,
210+ [
211+ '__client_uat' ,
212+ '1704067200' ,
213+ {
214+ domain : mockDomain ,
215+ expires : mockExpires ,
216+ sameSite : 'None' ,
217+ secure : true ,
218+ partitioned : true ,
219+ } ,
220+ ] ,
221+ ] ) ;
222+ const firstInvocationOrder = mockRemove . mock . invocationCallOrder [ 0 ] ;
223+ expect ( mockRemove . mock . invocationCallOrder ) . toEqual ( [
224+ firstInvocationOrder ,
225+ firstInvocationOrder + 1 ,
226+ firstInvocationOrder + 4 ,
227+ firstInvocationOrder + 5 ,
228+ firstInvocationOrder + 6 ,
229+ firstInvocationOrder + 7 ,
230+ ] ) ;
231+ expect ( mockSet . mock . invocationCallOrder ) . toEqual ( [ firstInvocationOrder + 8 , firstInvocationOrder + 9 ] ) ;
232+ } ) ;
167233} ) ;
0 commit comments