-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use latest zustand testing docs (#188)
- Loading branch information
Showing
8 changed files
with
57 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,54 @@ | ||
import { type StateCreator, create as actualCreate } from 'zustand'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { afterEach } from 'vitest'; | ||
// https://github.com/pmndrs/zustand/blob/main/docs/guides/testing.md | ||
import * as zustand from 'zustand'; | ||
import { act } from '@testing-library/react'; | ||
|
||
const { create: actualCreate, createStore: actualCreateStore } = | ||
await vi.importActual<typeof zustand>('zustand'); | ||
|
||
// a variable to hold reset functions for all stores declared in the app | ||
const storeResetFns = new Set<() => void>(); | ||
export const storeResetFns = new Set<() => void>(); | ||
|
||
const createUncurried = <T>(stateCreator: zustand.StateCreator<T>) => { | ||
const store = actualCreate(stateCreator); | ||
const initialState = store.getInitialState(); | ||
storeResetFns.add(() => { | ||
store.setState(initialState, true); | ||
}); | ||
return store; | ||
}; | ||
|
||
// when creating a store, we get its initial state, create a reset function and add it in the set | ||
const createStore = (createState: StateCreator<unknown>) => { | ||
if (!createState) return createStore; | ||
const store = actualCreate(createState); | ||
const initialState = store.getState(); | ||
storeResetFns.add(() => store.setState(initialState, true)); | ||
export const create = (<T>(stateCreator: zustand.StateCreator<T>) => { | ||
// to support curried version of create | ||
return typeof stateCreator === 'function' | ||
? createUncurried(stateCreator) | ||
: createUncurried; | ||
}) as typeof zustand.create; | ||
|
||
const createStoreUncurried = <T>(stateCreator: zustand.StateCreator<T>) => { | ||
const store = actualCreateStore(stateCreator); | ||
const initialState = store.getInitialState(); | ||
storeResetFns.add(() => { | ||
store.setState(initialState, true); | ||
}); | ||
return store; | ||
}; | ||
|
||
// Reset all stores after each test run | ||
// when creating a store, we get its initial state, create a reset function and add it in the set | ||
export const createStore = (<T>(stateCreator: zustand.StateCreator<T>) => { | ||
// to support curried version of createStore | ||
return typeof stateCreator === 'function' | ||
? createStoreUncurried(stateCreator) | ||
: createStoreUncurried; | ||
}) as typeof zustand.createStore; | ||
|
||
export const { useStore } = zustand; | ||
|
||
// reset all stores after each test run | ||
afterEach(() => { | ||
act(() => storeResetFns.forEach((resetFn) => resetFn())); | ||
act(() => { | ||
storeResetFns.forEach((resetFn) => { | ||
resetFn(); | ||
}); | ||
}); | ||
}); | ||
|
||
export { createStore }; |
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
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
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
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,8 +1,4 @@ | ||
import { afterEach } from 'vitest'; | ||
import { cleanup } from '@testing-library/react'; | ||
import '@testing-library/jest-dom/vitest'; | ||
// https://github.com/pmndrs/zustand/blob/main/docs/guides/testing.md | ||
import '@testing-library/jest-dom'; | ||
|
||
// runs a cleanup after each test case (e.g. clearing happy-dom) | ||
afterEach(() => { | ||
cleanup(); | ||
}); | ||
vi.mock('zustand'); // to make it work like Jest (auto-mocking) |