|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { createDevBrowserCookie } from '../devBrowser'; |
| 4 | + |
| 5 | +const { cookieStore, removeCalls, setCalls } = vi.hoisted(() => ({ |
| 6 | + cookieStore: new Map<string, string>(), |
| 7 | + removeCalls: [] as Array<{ name: string; attributes?: object }>, |
| 8 | + setCalls: [] as Array<{ name: string; value: string; attributes?: object }>, |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock('@clerk/shared/cookie', () => ({ |
| 12 | + createCookieHandler: (name: string) => ({ |
| 13 | + get: () => cookieStore.get(name), |
| 14 | + remove: (attributes?: object) => { |
| 15 | + removeCalls.push({ name, attributes }); |
| 16 | + cookieStore.delete(name); |
| 17 | + }, |
| 18 | + set: (value: string, attributes?: object) => { |
| 19 | + setCalls.push({ name, value, attributes }); |
| 20 | + cookieStore.set(name, value); |
| 21 | + }, |
| 22 | + }), |
| 23 | +})); |
| 24 | + |
| 25 | +describe('createDevBrowserCookie', () => { |
| 26 | + const cookieSuffix = 'test-suffix'; |
| 27 | + const suffixedCookieName = '__clerk_db_jwt_test-suffix'; |
| 28 | + const unsuffixedCookieName = '__clerk_db_jwt'; |
| 29 | + const devBrowser = 'test-dev-browser'; |
| 30 | + const now = new Date('2024-01-01T00:00:00.000Z'); |
| 31 | + const expires = new Date('2025-01-01T00:00:00.000Z'); |
| 32 | + const defaultOptions = { usePartitionedCookies: () => false }; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + vi.useFakeTimers(); |
| 36 | + vi.setSystemTime(now); |
| 37 | + cookieStore.clear(); |
| 38 | + removeCalls.length = 0; |
| 39 | + setCalls.length = 0; |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + vi.useRealTimers(); |
| 44 | + vi.unstubAllGlobals(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('removes current, non-partitioned, and partitioned cookie variants for both dev browser cookie names', () => { |
| 48 | + const cookieHandler = createDevBrowserCookie(cookieSuffix, defaultOptions); |
| 49 | + |
| 50 | + cookieHandler.remove(); |
| 51 | + |
| 52 | + expect(removeCalls).toEqual([ |
| 53 | + { |
| 54 | + name: suffixedCookieName, |
| 55 | + attributes: { |
| 56 | + sameSite: 'Lax', |
| 57 | + secure: false, |
| 58 | + partitioned: false, |
| 59 | + }, |
| 60 | + }, |
| 61 | + { name: suffixedCookieName, attributes: undefined }, |
| 62 | + { |
| 63 | + name: suffixedCookieName, |
| 64 | + attributes: { |
| 65 | + sameSite: 'None', |
| 66 | + secure: true, |
| 67 | + partitioned: true, |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: unsuffixedCookieName, |
| 72 | + attributes: { |
| 73 | + sameSite: 'Lax', |
| 74 | + secure: false, |
| 75 | + partitioned: false, |
| 76 | + }, |
| 77 | + }, |
| 78 | + { name: unsuffixedCookieName, attributes: undefined }, |
| 79 | + { |
| 80 | + name: unsuffixedCookieName, |
| 81 | + attributes: { |
| 82 | + sameSite: 'None', |
| 83 | + secure: true, |
| 84 | + partitioned: true, |
| 85 | + }, |
| 86 | + }, |
| 87 | + ]); |
| 88 | + }); |
| 89 | + |
| 90 | + it('clears stale partitioned cookie variants before writing a new dev browser', () => { |
| 91 | + const cookieHandler = createDevBrowserCookie(cookieSuffix, defaultOptions); |
| 92 | + |
| 93 | + cookieHandler.set(devBrowser); |
| 94 | + |
| 95 | + expect(removeCalls).toContainEqual({ |
| 96 | + name: suffixedCookieName, |
| 97 | + attributes: { |
| 98 | + sameSite: 'None', |
| 99 | + secure: true, |
| 100 | + partitioned: true, |
| 101 | + }, |
| 102 | + }); |
| 103 | + expect(removeCalls).toContainEqual({ |
| 104 | + name: unsuffixedCookieName, |
| 105 | + attributes: { |
| 106 | + sameSite: 'None', |
| 107 | + secure: true, |
| 108 | + partitioned: true, |
| 109 | + }, |
| 110 | + }); |
| 111 | + expect(setCalls).toEqual([ |
| 112 | + { |
| 113 | + name: suffixedCookieName, |
| 114 | + value: devBrowser, |
| 115 | + attributes: { |
| 116 | + expires, |
| 117 | + sameSite: 'Lax', |
| 118 | + secure: false, |
| 119 | + partitioned: false, |
| 120 | + }, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: unsuffixedCookieName, |
| 124 | + value: devBrowser, |
| 125 | + attributes: { |
| 126 | + expires, |
| 127 | + sameSite: 'Lax', |
| 128 | + secure: false, |
| 129 | + partitioned: false, |
| 130 | + }, |
| 131 | + }, |
| 132 | + ]); |
| 133 | + }); |
| 134 | + |
| 135 | + it('reads the suffixed cookie before falling back to the unsuffixed cookie', () => { |
| 136 | + const cookieHandler = createDevBrowserCookie(cookieSuffix, defaultOptions); |
| 137 | + |
| 138 | + cookieStore.set(unsuffixedCookieName, 'unsuffixed-value'); |
| 139 | + cookieStore.set(suffixedCookieName, 'suffixed-value'); |
| 140 | + |
| 141 | + expect(cookieHandler.get()).toBe('suffixed-value'); |
| 142 | + |
| 143 | + cookieStore.delete(suffixedCookieName); |
| 144 | + |
| 145 | + expect(cookieHandler.get()).toBe('unsuffixed-value'); |
| 146 | + }); |
| 147 | +}); |
0 commit comments