11/* eslint-disable @typescript-eslint/no-unused-vars */
2+ /*
3+ * Copyright (c) 2025 Ping Identity Corporation. All rights reserved.
4+ *
5+ * This software may be modified and distributed under the terms
6+ * of the MIT license. See the LICENSE file for details.
7+ */
28import { describe , it , expect , vi , beforeEach , type Mock } from 'vitest' ;
39import { createStorage , type StorageConfig } from './storage.effects.js' ;
4- import type { TokenStoreObject } from '@forgerock/sdk-types' ;
10+ import type { CustomStorageObject } from '@forgerock/sdk-types' ;
511
612const localStorageMock = ( ( ) => {
713 let store : Record < string , string > = { } ;
@@ -13,7 +19,7 @@ const localStorageMock = (() => {
1319 }
1420 return Promise . resolve ( value ) ;
1521 } ) ,
16- setItem : vi . fn ( ( key : string , value : string | Record < any , any > | any [ ] ) => {
22+ setItem : vi . fn ( ( key : string , value : any ) => {
1723 const valueIsString = typeof value === 'string' ;
1824 store [ key ] = valueIsString ? value : JSON . stringify ( value ) ;
1925 return Promise . resolve ( ) ;
@@ -64,7 +70,7 @@ const sessionStorageMock = (() => {
6470
6571Object . defineProperty ( global , 'sessionStorage' , { value : sessionStorageMock } ) ;
6672
67- const mockCustomStore : TokenStoreObject = {
73+ const mockCustomStore : CustomStorageObject = {
6874 get : vi . fn ( ( key : string ) => Promise . resolve < string | null > ( null ) ) ,
6975 set : vi . fn ( ( key : string , value : unknown ) => Promise . resolve ( ) ) ,
7076 remove : vi . fn ( ( key : string ) => Promise . resolve ( ) ) ,
@@ -73,6 +79,7 @@ const mockCustomStore: TokenStoreObject = {
7379describe ( 'storage Effect' , ( ) => {
7480 const storageName = 'MyStorage' ;
7581 const baseConfig : Omit < StorageConfig , 'tokenStore' > = {
82+ storeType : 'localStorage' ,
7683 prefix : 'testPrefix' ,
7784 } ;
7885 const expectedKey = `${ baseConfig . prefix } -${ storageName } ` ;
@@ -91,14 +98,14 @@ describe('storage Effect', () => {
9198 describe ( 'with localStorage' , ( ) => {
9299 const config : StorageConfig = {
93100 ...baseConfig ,
94- tokenStore : 'localStorage' ,
101+ storeType : 'localStorage' ,
95102 } ;
96103
97104 const storageInstance = createStorage ( config , storageName ) ;
98105
99106 it ( 'should call localStorage.getItem with the correct key and return value' , async ( ) => {
100- localStorageMock . setItem ( expectedKey , testValue ) ;
101- const result = await storageInstance . get < string > ( ) ;
107+ localStorageMock . setItem ( expectedKey , JSON . stringify ( testValue ) ) ;
108+ const result = await storageInstance . get ( ) ;
102109 expect ( localStorageMock . getItem ) . toHaveBeenCalledTimes ( 1 ) ;
103110 expect ( localStorageMock . getItem ) . toHaveBeenCalledWith ( expectedKey ) ;
104111 expect ( result ) . toBe ( testValue ) ;
@@ -107,7 +114,7 @@ describe('storage Effect', () => {
107114 } ) ;
108115
109116 it ( 'should return null if localStorage.getItem finds no value' , async ( ) => {
110- const result = await storageInstance . get < null > ( ) ;
117+ const result = await storageInstance . get ( ) ;
111118 expect ( localStorageMock . getItem ) . toHaveBeenCalledTimes ( 1 ) ;
112119 expect ( localStorageMock . getItem ) . toHaveBeenCalledWith ( expectedKey ) ;
113120 expect ( result ) . toBeNull ( ) ;
@@ -116,8 +123,8 @@ describe('storage Effect', () => {
116123 it ( 'should call localStorage.setItem with the correct key and value' , async ( ) => {
117124 await storageInstance . set ( testValue ) ;
118125 expect ( localStorageMock . setItem ) . toHaveBeenCalledTimes ( 1 ) ;
119- expect ( localStorageMock . setItem ) . toHaveBeenCalledWith ( expectedKey , testValue ) ;
120- expect ( await localStorageMock . getItem ( expectedKey ) ) . toBe ( testValue ) ;
126+ expect ( localStorageMock . setItem ) . toHaveBeenCalledWith ( expectedKey , JSON . stringify ( testValue ) ) ;
127+ expect ( await localStorageMock . getItem ( expectedKey ) ) . toBe ( JSON . stringify ( testValue ) ) ;
121128 expect ( sessionStorageMock . setItem ) . not . toHaveBeenCalled ( ) ;
122129 expect ( mockCustomStore . set ) . not . toHaveBeenCalled ( ) ;
123130 } ) ;
@@ -145,7 +152,7 @@ describe('storage Effect', () => {
145152 const testObject = { a : 1 , b : 'test' } ;
146153 storageInstance . set ( testObject ) ;
147154
148- const result = await storageInstance . get < typeof testObject > ( ) ;
155+ const result = await storageInstance . get ( ) ;
149156 console . log ( result ) ;
150157
151158 expect ( localStorageMock . getItem ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -158,13 +165,13 @@ describe('storage Effect', () => {
158165 describe ( 'with sessionStorage' , ( ) => {
159166 const config : StorageConfig = {
160167 ...baseConfig ,
161- tokenStore : 'sessionStorage' ,
168+ storeType : 'sessionStorage' ,
162169 } ;
163170 const storageName = 'MyStorage' ;
164171 const storageInstance = createStorage ( config , storageName ) ;
165172
166173 it ( 'should call sessionStorage.getItem with the correct key and return value' , async ( ) => {
167- sessionStorageMock . setItem ( expectedKey , testValue ) ;
174+ sessionStorageMock . setItem ( expectedKey , JSON . stringify ( testValue ) ) ;
168175 const result = await storageInstance . get ( ) ;
169176 expect ( sessionStorageMock . getItem ) . toHaveBeenCalledTimes ( 1 ) ;
170177 expect ( sessionStorageMock . getItem ) . toHaveBeenCalledWith ( expectedKey ) ;
@@ -182,7 +189,7 @@ describe('storage Effect', () => {
182189 it ( 'should return parsed value if sessionStorage.getItem returns object or array' , async ( ) => {
183190 const obj = { tokens : 123 } ;
184191 await storageInstance . set ( obj ) ;
185- const result = await storageInstance . get < typeof obj > ( ) ;
192+ const result = await storageInstance . get ( ) ;
186193 if ( ! result ) {
187194 // we should not hit this expect
188195 expect ( false ) . toBe ( true ) ;
@@ -221,13 +228,13 @@ describe('storage Effect', () => {
221228 describe ( 'with custom TokenStoreObject' , ( ) => {
222229 const config : StorageConfig = {
223230 ...baseConfig ,
224- tokenStore : 'localStorage' ,
231+ storeType : 'localStorage' ,
225232 } ;
226233 const myStorage = 'MyStorage' ;
227234 const storageInstance = createStorage ( config , myStorage , mockCustomStore ) ;
228235
229236 it ( 'should call customStore.get with the correct key and return its value' , async ( ) => {
230- ( mockCustomStore . get as Mock ) . mockResolvedValueOnce ( testValue ) ;
237+ ( mockCustomStore . get as Mock ) . mockResolvedValueOnce ( JSON . stringify ( testValue ) ) ;
231238 const result = await storageInstance . get ( ) ;
232239 expect ( mockCustomStore . get ) . toHaveBeenCalledTimes ( 1 ) ;
233240 expect ( mockCustomStore . get ) . toHaveBeenCalledWith ( expectedKey ) ;
@@ -248,7 +255,7 @@ describe('storage Effect', () => {
248255 const jsonString = JSON . stringify ( testObject ) ;
249256 ( mockCustomStore . get as Mock ) . mockResolvedValueOnce ( jsonString ) ; // Mock returns JSON string
250257
251- const result = await storageInstance . get < typeof testObject > ( ) ;
258+ const result = await storageInstance . get ( ) ;
252259
253260 expect ( mockCustomStore . get ) . toHaveBeenCalledTimes ( 1 ) ;
254261 expect ( mockCustomStore . get ) . toHaveBeenCalledWith ( expectedKey ) ;
@@ -258,7 +265,7 @@ describe('storage Effect', () => {
258265 it ( 'should call customStore.set with the correct key and value' , async ( ) => {
259266 await storageInstance . set ( testValue ) ;
260267 expect ( mockCustomStore . set ) . toHaveBeenCalledTimes ( 1 ) ;
261- expect ( mockCustomStore . set ) . toHaveBeenCalledWith ( expectedKey , testValue ) ;
268+ expect ( mockCustomStore . set ) . toHaveBeenCalledWith ( expectedKey , JSON . stringify ( testValue ) ) ;
262269 expect ( localStorageMock . setItem ) . not . toHaveBeenCalled ( ) ;
263270 expect ( sessionStorageMock . setItem ) . not . toHaveBeenCalled ( ) ;
264271 } ) ;
@@ -282,7 +289,7 @@ describe('storage Effect', () => {
282289 } ) ;
283290
284291 it ( 'should return a function that returns the storage interface' , ( ) => {
285- const config : StorageConfig = { ...baseConfig , tokenStore : 'localStorage' } ;
292+ const config : StorageConfig = { ...baseConfig , storeType : 'localStorage' } ;
286293 const myStorage = 'MyStorage' ;
287294 const storageInterface = createStorage ( config , myStorage ) ;
288295 expect ( storageInterface ) . toHaveProperty ( 'get' ) ;
0 commit comments