|
| 1 | +import { describe, expectTypeOf, it } from 'vitest'; |
| 2 | +import { CustomStorageObject, Tokens } from '@forgerock/sdk-types'; |
| 3 | +import { GenericError, StorageConfig } from '@forgerock/storage'; |
| 4 | +import { tokenStore } from './token-store.js'; |
| 5 | + |
| 6 | +describe('tokenStore type tests', () => { |
| 7 | + const mockTokens: Tokens = { |
| 8 | + accessToken: 'test_access_token', |
| 9 | + idToken: 'test_id_token', |
| 10 | + refreshToken: 'test_refresh', |
| 11 | + tokenExpiry: 3600, |
| 12 | + }; |
| 13 | + it('should return a storage object with Tokens type when customStore is not provided', () => { |
| 14 | + const storageConfig: StorageConfig = { |
| 15 | + storeType: 'sessionStorage', |
| 16 | + }; |
| 17 | + const store = tokenStore(storageConfig); |
| 18 | + expectTypeOf<Promise<void | { code: string; message: string; type: string }>>( |
| 19 | + store.set(mockTokens), |
| 20 | + ); |
| 21 | + |
| 22 | + expectTypeOf<Promise<void>>(store.remove()); |
| 23 | + expectTypeOf<Promise<Tokens | GenericError | null>>(store.get()); |
| 24 | + const tokens = store.get(); |
| 25 | + if ('accessToken' in tokens) { |
| 26 | + expectTypeOf<Promise<Tokens>>(); |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + it('should return a storage object with Tokens type when customStore is provided', () => { |
| 31 | + const storageConfig: StorageConfig = { |
| 32 | + storeType: 'sessionStorage', |
| 33 | + }; |
| 34 | + const customStore: CustomStorageObject = { |
| 35 | + get: async (key: string) => { |
| 36 | + return key; |
| 37 | + }, |
| 38 | + set: async (key: string, value: any) => { |
| 39 | + console.log(key, value); |
| 40 | + }, |
| 41 | + remove: async (key: string) => { |
| 42 | + console.log(key); |
| 43 | + }, |
| 44 | + }; |
| 45 | + const store = tokenStore(storageConfig, customStore); |
| 46 | + expectTypeOf<Promise<Tokens | GenericError | null>>(store.get()); |
| 47 | + expectTypeOf<Promise<void | { code: string; message: string; type: string }>>( |
| 48 | + store.set(mockTokens), |
| 49 | + ); |
| 50 | + expectTypeOf<Promise<void>>(store.remove()); |
| 51 | + }); |
| 52 | +}); |
0 commit comments