-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ec0964
commit 9016720
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
packages/lib-classifier/src/hooks/useHydratedStore.spec.js
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,103 @@ | ||
import { applySnapshot, getSnapshot } from 'mobx-state-tree' | ||
import React from 'react' | ||
import { renderHook } from '@testing-library/react-hooks/pure' | ||
|
||
import mockStore from '@test/mockStore' | ||
import RootStore from '@store/RootStore' | ||
import { cleanStore } from './useStore' | ||
import { useHydratedStore } from '.' | ||
|
||
describe('Hooks > useHydratedStore', function () { | ||
describe('without an existing store', function () { | ||
let store | ||
|
||
beforeEach(function () { | ||
const { authClient, client } = mockStore() | ||
const { result } = renderHook(() => useHydratedStore({ authClient, client }, false, 'test-key')) | ||
store = result.current | ||
}) | ||
|
||
afterEach(function () { | ||
cleanStore() | ||
}) | ||
|
||
it('should create a new store', function () { | ||
const mockStore = getSnapshot(RootStore.create({})) | ||
const snapshot = getSnapshot(store) | ||
expect(snapshot).to.deep.equal(mockStore) | ||
}) | ||
}) | ||
|
||
describe('with an existing store', function () { | ||
let store | ||
let newStore | ||
|
||
beforeEach(function () { | ||
const { authClient, client } = mockStore() | ||
const { result: firstRun } = renderHook(() => useHydratedStore({ authClient, client }, false, 'test-key')) | ||
store = firstRun.current | ||
const { result: secondRun } = renderHook(() => useHydratedStore({ authClient, client }, false, 'test-key')) | ||
newStore = secondRun.current | ||
}) | ||
|
||
afterEach(function () { | ||
cleanStore() | ||
}) | ||
|
||
it('should use the existing store', function () { | ||
expect(newStore).to.deep.equal(store) | ||
}) | ||
}) | ||
|
||
describe('with session storage enabled', function () { | ||
let store | ||
|
||
beforeEach(async function () { | ||
const expectedStore = mockStore() | ||
const { authClient, client } = expectedStore | ||
const { result, waitForNextUpdate } = renderHook(() => useHydratedStore({ authClient, client }, true, 'test-key')) | ||
await waitForNextUpdate() | ||
store = result.current | ||
const mockSnapshot = getSnapshot(expectedStore) | ||
applySnapshot(store.projects, mockSnapshot.projects) | ||
}) | ||
|
||
afterEach(function () { | ||
cleanStore() | ||
}) | ||
|
||
it('should save snapshots in session storage', function () { | ||
const mockSnapshot = window.sessionStorage.getItem('test-key') | ||
const snapshot = getSnapshot(store) | ||
expect(JSON.stringify(snapshot)).to.equal(mockSnapshot) | ||
}) | ||
}) | ||
|
||
describe('with a saved snapshot', function () { | ||
let store | ||
let mockSnapshot | ||
|
||
beforeEach(async function () { | ||
const expectedStore = mockStore() | ||
mockSnapshot = getSnapshot(expectedStore) | ||
window.sessionStorage.setItem('test-key', JSON.stringify(mockSnapshot)) | ||
|
||
const { authClient, client } = expectedStore | ||
const { result, waitFor } = renderHook(() => useHydratedStore({ authClient, client }, true, 'test-key')) | ||
await waitFor(() => { | ||
store = result.current | ||
return !!store?.projects | ||
}) | ||
const snapshot = getSnapshot(store) | ||
}) | ||
|
||
afterEach(function () { | ||
cleanStore() | ||
}) | ||
|
||
it('should load the snapshot into the store', function () { | ||
const snapshot = getSnapshot(store) | ||
expect(snapshot.projects).to.deep.equal(mockSnapshot.projects) | ||
}) | ||
}) | ||
}) |
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