6
6
* of the MIT license. See the LICENSE file for details.
7
7
*
8
8
*/
9
- import { describe , it , expect , beforeEach , afterEach , vi } from 'vitest' ;
9
+ import { describe , it , expect , beforeEach , afterEach , vi , Mock } from 'vitest' ;
10
10
import {
11
11
getLocalStorageTokens ,
12
12
setLocalStorageTokens ,
13
13
removeTokensFromLocalStorage ,
14
14
tokenFactory ,
15
15
} 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' ;
35
17
36
18
describe ( 'Token Storage Functions' , ( ) => {
37
- let localStorageMock : ReturnType < typeof createMockWindow > ;
38
-
39
19
// Mock config
40
20
const mockConfig : ConfigOptions = {
41
21
clientId : 'test-client' ,
@@ -51,14 +31,20 @@ describe('Token Storage Functions', () => {
51
31
} ;
52
32
53
33
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
61
34
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 ) ;
62
48
} ) ;
63
49
64
50
afterEach ( ( ) => {
@@ -70,34 +56,30 @@ describe('Token Storage Functions', () => {
70
56
it ( 'should return undefined when no tokens exist' , ( ) => {
71
57
const tokens = getLocalStorageTokens ( mockConfig ) ;
72
58
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
+ } ) ;
75
63
} ) ;
76
64
77
65
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' ) ;
84
68
} ) ;
85
69
86
70
it ( 'should return error object when tokens exist but cannot be parsed' , ( ) => {
87
- localStorageMock . getItem . mockReturnValueOnce ( 'invalid-json' ) ;
88
-
89
71
const result = getLocalStorageTokens ( mockConfig ) ;
90
72
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 } ) ;
93
75
} ) ;
94
76
} ) ;
95
77
96
78
describe ( 'setTokens' , ( ) => {
97
79
it ( 'should stringify and store tokens in localStorage' , ( ) => {
98
80
setLocalStorageTokens ( mockConfig , sampleTokens ) ;
99
81
100
- expect ( localStorageMock . setItem ) . toHaveBeenCalledWith (
82
+ expect ( localStorage . setItem ) . toHaveBeenCalledWith (
101
83
'test-prefix-test-client' ,
102
84
JSON . stringify ( sampleTokens ) ,
103
85
) ;
@@ -108,7 +90,7 @@ describe('Token Storage Functions', () => {
108
90
it ( 'should remove tokens from localStorage' , ( ) => {
109
91
removeTokensFromLocalStorage ( mockConfig ) ;
110
92
111
- expect ( localStorageMock . removeItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
93
+ expect ( localStorage . removeItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
112
94
} ) ;
113
95
} ) ;
114
96
@@ -125,20 +107,20 @@ describe('Token Storage Functions', () => {
125
107
} ) ;
126
108
127
109
it ( 'get method should retrieve tokens' , ( ) => {
128
- localStorageMock . getItem . mockReturnValueOnce ( JSON . stringify ( sampleTokens ) ) ;
110
+ ( localStorage . getItem as Mock ) . mockReturnValueOnce ( JSON . stringify ( sampleTokens ) ) ;
129
111
130
112
const tokenManager = tokenFactory ( mockConfig ) ;
131
113
const tokens = tokenManager . get ( ) ;
132
114
133
- expect ( localStorageMock . getItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
115
+ expect ( localStorage . getItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
134
116
expect ( tokens ) . toEqual ( sampleTokens ) ;
135
117
} ) ;
136
118
137
119
it ( 'set method should store tokens' , ( ) => {
138
120
const tokenManager = tokenFactory ( mockConfig ) ;
139
121
tokenManager . set ( sampleTokens ) ;
140
122
141
- expect ( localStorageMock . setItem ) . toHaveBeenCalledWith (
123
+ expect ( localStorage . setItem ) . toHaveBeenCalledWith (
142
124
'test-prefix-test-client' ,
143
125
JSON . stringify ( sampleTokens ) ,
144
126
) ;
@@ -148,7 +130,7 @@ describe('Token Storage Functions', () => {
148
130
const tokenManager = tokenFactory ( mockConfig ) ;
149
131
tokenManager . remove ( ) ;
150
132
151
- expect ( localStorageMock . removeItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
133
+ expect ( localStorage . removeItem ) . toHaveBeenCalledWith ( 'test-prefix-test-client' ) ;
152
134
} ) ;
153
135
} ) ;
154
136
} ) ;
0 commit comments