-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(hook): add test for new sessionStorage hook
- Loading branch information
Showing
6 changed files
with
125 additions
and
16 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks' | ||
import { mockButtonPlans } from 'test/fixtures' | ||
import { useSessionStorage } from 'hooks/useSessionStorage' | ||
describe('useSessionStorage', () => { | ||
let mockStorage: Record<string, string> = {} | ||
|
||
beforeAll(() => { | ||
global.Storage.prototype.setItem = jest.fn((key: string, value) => { | ||
mockStorage[key] = value | ||
}) | ||
global.Storage.prototype.getItem = jest.fn((key) => { | ||
return mockStorage[key] | ||
}) | ||
global.Storage.prototype.removeItem = jest.fn((key) => { | ||
delete mockStorage[key] | ||
}) | ||
global.Storage.prototype.clear = jest.fn(() => { | ||
mockStorage = {} | ||
}) | ||
}) | ||
|
||
beforeEach(() => { | ||
// Start with a clean storage | ||
mockStorage = {} | ||
}) | ||
|
||
afterAll(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
it('should call the dedicated sessionStorage functions', () => { | ||
const { result } = renderHook(() => useSessionStorage()) | ||
|
||
// First we check that the storage is empty for a key | ||
expect(result.current.getCache('someKey')).toEqual(null) | ||
// We set values for the specific key | ||
act(() => result.current.setCache('someKey', mockButtonPlans)) | ||
|
||
// We check that storage has been updated | ||
expect(result.current.getCache('someKey')).toStrictEqual(mockButtonPlans) | ||
|
||
// We set another value for the another key | ||
act(() => result.current.setCache('otherKey', [mockButtonPlans[0]])) | ||
|
||
// We delete someKey values | ||
act(() => result.current.deleteCache('someKey')) | ||
// We check that storage is empty again for this key | ||
expect(result.current.getCache('someKey')).toEqual(null) | ||
// But it's still filled for the other key | ||
expect(result.current.getCache('otherKey')).toStrictEqual([mockButtonPlans[0]]) | ||
|
||
// We clean the storage | ||
act(() => result.current.clearCache()) | ||
// We check that storage is empty for the remaining key | ||
expect(result.current.getCache('otherKey')).toStrictEqual(null) | ||
}) | ||
}) |
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