-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(workspace): add hook to scope local and session storage by org a…
…nd app (#13937)
- Loading branch information
1 parent
d76a686
commit 7e13f6b
Showing
6 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
frontend/libs/studio-hooks/src/hooks/useOrgAppScopedStorage.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { type UseOrgAppScopedStorage, useOrgAppScopedStorage } from './useOrgAppScopedStorage'; | ||
import { useParams } from 'react-router-dom'; | ||
|
||
jest.mock('react-router-dom', () => ({ | ||
useParams: jest.fn(), | ||
})); | ||
|
||
const mockedOrg: string = 'testOrg'; | ||
const mockedApp: string = 'testApp'; | ||
const scopedStorageKey: string = 'testOrg-testApp'; | ||
const storagesToTest: Array<UseOrgAppScopedStorage['storage']> = ['localStorage', 'sessionStorage']; | ||
|
||
describe('useOrgAppScopedStorage', () => { | ||
afterEach(() => { | ||
window.localStorage.clear(); | ||
window.sessionStorage.clear(); | ||
}); | ||
|
||
it.each(storagesToTest)( | ||
'initializes ScopedStorageImpl with correct storage scope, %s', | ||
(storage) => { | ||
const { result } = renderUseOrgAppScopedStorage({ storage }); | ||
|
||
result.current.setItem('key', 'value'); | ||
|
||
expect(result.current.setItem).toBeDefined(); | ||
expect(result.current.getItem).toBeDefined(); | ||
expect(result.current.removeItem).toBeDefined(); | ||
expect(window[storage].getItem(scopedStorageKey)).toBe('{"key":"value"}'); | ||
}, | ||
); | ||
|
||
it.each(storagesToTest)('should retrieve parsed objects from %s', (storage) => { | ||
const { result } = renderUseOrgAppScopedStorage({ storage }); | ||
|
||
result.current.setItem('person', { name: 'John', age: 18 }); | ||
|
||
expect(result.current.getItem('person')).toEqual({ | ||
name: 'John', | ||
age: 18, | ||
}); | ||
}); | ||
|
||
it.each(storagesToTest)('should be possible to remove item from %s', (storage) => { | ||
const { result } = renderUseOrgAppScopedStorage({ storage }); | ||
|
||
result.current.setItem('key', 'value'); | ||
result.current.removeItem('key'); | ||
expect(result.current.getItem('key')).toBeUndefined(); | ||
}); | ||
|
||
it('should use localStorage as default storage', () => { | ||
const { result } = renderUseOrgAppScopedStorage({}); | ||
result.current.setItem('key', 'value'); | ||
|
||
expect(window.localStorage.getItem(scopedStorageKey)).toBe('{"key":"value"}'); | ||
}); | ||
}); | ||
|
||
const renderUseOrgAppScopedStorage = ({ storage }: UseOrgAppScopedStorage) => { | ||
(useParams as jest.Mock).mockReturnValue({ org: mockedOrg, app: mockedApp }); | ||
const { result } = renderHook(() => | ||
useOrgAppScopedStorage({ | ||
storage, | ||
}), | ||
); | ||
return { result }; | ||
}; |
36 changes: 36 additions & 0 deletions
36
frontend/libs/studio-hooks/src/hooks/useOrgAppScopedStorage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useParams } from 'react-router-dom'; | ||
import { | ||
type ScopedStorage, | ||
type ScopedStorageResult, | ||
ScopedStorageImpl, | ||
} from '@studio/pure-functions'; | ||
|
||
type OrgAppParams = { | ||
org: string; | ||
app: string; | ||
}; | ||
|
||
const supportedStorageMap: Record<UseOrgAppScopedStorage['storage'], ScopedStorage> = { | ||
localStorage: window.localStorage, | ||
sessionStorage: window.sessionStorage, | ||
}; | ||
|
||
export type UseOrgAppScopedStorage = { | ||
storage?: 'localStorage' | 'sessionStorage'; | ||
}; | ||
|
||
type UseOrgAppScopedStorageResult = ScopedStorageResult; | ||
export const useOrgAppScopedStorage = ({ | ||
storage = 'localStorage', | ||
}: UseOrgAppScopedStorage): UseOrgAppScopedStorageResult => { | ||
const { org, app } = useParams<OrgAppParams>(); | ||
|
||
const storageKey: string = `${org}-${app}`; | ||
const scopedStorage = new ScopedStorageImpl(supportedStorageMap[storage], storageKey); | ||
|
||
return { | ||
setItem: scopedStorage.setItem, | ||
getItem: scopedStorage.getItem, | ||
removeItem: scopedStorage.removeItem, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { ScopedStorageImpl, type ScopedStorage } from './ScopedStorage'; | ||
export { ScopedStorageImpl, type ScopedStorage, type ScopedStorageResult } from './ScopedStorage'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters