66 * of the MIT license. See the LICENSE file for details.
77 *
88 */
9- import { describe , it , expect , beforeEach , afterEach , vi } from 'vitest' ;
9+ import { describe , it , expect , beforeEach , afterEach , vi , Mock } from 'vitest' ;
1010import {
1111 getLocalStorageTokens ,
1212 setLocalStorageTokens ,
1313 removeTokensFromLocalStorage ,
1414 tokenFactory ,
1515} from './local-storage.js' ;
16- import type { ConfigOptions , Tokens } from '@forgerock/shared-types' ;
17-
18- // Create a mock global object for tests
19- const createMockWindow = ( ) => {
20- const localStorageMock = {
21- getItem : vi . fn ( ( key : string ) => localStorageMock . store [ key ] || null ) ,
22- setItem : vi . fn ( ( key : string , value : string ) => {
23- localStorageMock . store [ key ] = value . toString ( ) ;
24- } ) ,
25- removeItem : vi . fn ( ( key : string ) => {
26- delete localStorageMock . store [ key ] ;
27- } ) ,
28- clear : vi . fn ( ( ) => {
29- localStorageMock . store = { } ;
30- } ) ,
31- store : { } as Record < string , string > ,
32- } ;
33- return localStorageMock ;
34- } ;
16+ import { TOKEN_ERRORS , type ConfigOptions , type Tokens } from '@forgerock/shared-types' ;
3517
3618describe ( 'Token Storage Functions' , ( ) => {
37- let localStorageMock : ReturnType < typeof createMockWindow > ;
38-
3919 // Mock config
4020 const mockConfig : ConfigOptions = {
4121 clientId : 'test-client' ,
@@ -51,14 +31,20 @@ describe('Token Storage Functions', () => {
5131 } ;
5232
5333 beforeEach ( ( ) => {
54- // Setup mock localStorage
55- localStorageMock = createMockWindow ( ) ;
56-
57- // Mock the localStorage globally
58- vi . stubGlobal ( 'localStorage' , localStorageMock ) ;
59-
60- // Clear mock calls before each test
6134 vi . clearAllMocks ( ) ;
35+ const mockLocalStorage = {
36+ getItem : vi . fn ( ) ,
37+ setItem : vi . fn ( ) ,
38+ removeItem : vi . fn ( ) ,
39+ } ;
40+ const mockSessionStorage = {
41+ getItem : vi . fn ( ) ,
42+ setItem : vi . fn ( ) ,
43+ removeItem : vi . fn ( ) ,
44+ } ;
45+
46+ vi . stubGlobal ( 'localStorage' , mockLocalStorage ) ;
47+ vi . stubGlobal ( 'sessionStorage' , mockSessionStorage ) ;
6248 } ) ;
6349
6450 afterEach ( ( ) => {
@@ -70,34 +56,30 @@ describe('Token Storage Functions', () => {
7056 it ( 'should return undefined when no tokens exist' , ( ) => {
7157 const tokens = getLocalStorageTokens ( mockConfig ) ;
7258
73- expect ( localStorageMock . getItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
74- expect ( tokens ) . toBeUndefined ( ) ;
59+ expect ( localStorage . getItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
60+ expect ( tokens ) . toEqual ( {
61+ error : TOKEN_ERRORS . NO_TOKENS_FOUND_LOCAL_STORAGE ,
62+ } ) ;
7563 } ) ;
7664
7765 it ( 'should parse and return tokens when they exist' , ( ) => {
78- localStorageMock . getItem . mockReturnValueOnce ( JSON . stringify ( sampleTokens ) ) ;
79-
80- const tokens = getLocalStorageTokens ( mockConfig ) ;
81-
82- expect ( localStorageMock . getItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
83- expect ( tokens ) . toEqual ( sampleTokens ) ;
66+ getLocalStorageTokens ( mockConfig ) ;
67+ expect ( localStorage . getItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
8468 } ) ;
8569
8670 it ( 'should return error object when tokens exist but cannot be parsed' , ( ) => {
87- localStorageMock . getItem . mockReturnValueOnce ( 'invalid-json' ) ;
88-
8971 const result = getLocalStorageTokens ( mockConfig ) ;
9072
91- expect ( localStorageMock . getItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
92- expect ( result ) . toEqual ( { error : 'Could not parse token from localStorage' } ) ;
73+ expect ( localStorage . getItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
74+ expect ( result ) . toEqual ( { error : TOKEN_ERRORS . NO_TOKENS_FOUND_LOCAL_STORAGE } ) ;
9375 } ) ;
9476 } ) ;
9577
9678 describe ( 'setTokens' , ( ) => {
9779 it ( 'should stringify and store tokens in localStorage' , ( ) => {
9880 setLocalStorageTokens ( mockConfig , sampleTokens ) ;
9981
100- expect ( localStorageMock . setItem ) . toHaveBeenCalledWith (
82+ expect ( localStorage . setItem ) . toHaveBeenCalledWith (
10183 'test-prefix-test-client' ,
10284 JSON . stringify ( sampleTokens ) ,
10385 ) ;
@@ -108,7 +90,7 @@ describe('Token Storage Functions', () => {
10890 it ( 'should remove tokens from localStorage' , ( ) => {
10991 removeTokensFromLocalStorage ( mockConfig ) ;
11092
111- expect ( localStorageMock . removeItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
93+ expect ( localStorage . removeItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
11294 } ) ;
11395 } ) ;
11496
@@ -125,20 +107,20 @@ describe('Token Storage Functions', () => {
125107 } ) ;
126108
127109 it ( 'get method should retrieve tokens' , ( ) => {
128- localStorageMock . getItem . mockReturnValueOnce ( JSON . stringify ( sampleTokens ) ) ;
110+ ( localStorage . getItem as Mock ) . mockReturnValueOnce ( JSON . stringify ( sampleTokens ) ) ;
129111
130112 const tokenManager = tokenFactory ( mockConfig ) ;
131113 const tokens = tokenManager . get ( ) ;
132114
133- expect ( localStorageMock . getItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
115+ expect ( localStorage . getItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
134116 expect ( tokens ) . toEqual ( sampleTokens ) ;
135117 } ) ;
136118
137119 it ( 'set method should store tokens' , ( ) => {
138120 const tokenManager = tokenFactory ( mockConfig ) ;
139121 tokenManager . set ( sampleTokens ) ;
140122
141- expect ( localStorageMock . setItem ) . toHaveBeenCalledWith (
123+ expect ( localStorage . setItem ) . toHaveBeenCalledWith (
142124 'test-prefix-test-client' ,
143125 JSON . stringify ( sampleTokens ) ,
144126 ) ;
@@ -148,7 +130,7 @@ describe('Token Storage Functions', () => {
148130 const tokenManager = tokenFactory ( mockConfig ) ;
149131 tokenManager . remove ( ) ;
150132
151- expect ( localStorageMock . removeItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
133+ expect ( localStorage . removeItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
152134 } ) ;
153135 } ) ;
154136} ) ;
0 commit comments