-
Notifications
You must be signed in to change notification settings - Fork 19
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
af0c245
commit 9f33569
Showing
590 changed files
with
5,811 additions
and
38,014 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ LICENSE | |
*.png | ||
*.hbs | ||
*.jpg | ||
*.ico | ||
|
||
**/build | ||
**/dist | ||
|
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,23 +1,62 @@ | ||
/** | ||
* From: https://docs.pmnd.rs/zustand/guides/testing | ||
* | ||
* Modified to also export useStore | ||
*/ | ||
|
||
import * as zustand from 'zustand'; | ||
import { vi, afterEach } from 'vitest'; | ||
|
||
const actualZustand = await vi.importActual<typeof import('zustand')>('zustand'); | ||
import { act } from '@testing-library/react'; | ||
|
||
const actualCreate = actualZustand.create; | ||
const { | ||
create: actualCreate, | ||
createStore: actualCreateStore, | ||
useStore: actualUseStore | ||
} = await vi.importActual<typeof zustand>('zustand'); | ||
|
||
const stores = new Set<() => unknown>(); | ||
// a variable to hold reset functions for all stores declared in the app | ||
export const storeResetFns = new Set<() => void>(); | ||
|
||
const create = ((createState: any) => { | ||
const store = actualCreate(createState); | ||
const createUncurried = <T>(stateCreator: zustand.StateCreator<T>) => { | ||
const store = actualCreate(stateCreator); | ||
const initialState = store.getState(); | ||
stores.add(() => store.setState(initialState, true)); | ||
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 | ||
export const create = (<T>(stateCreator: zustand.StateCreator<T>) => | ||
// to support curried version of create | ||
typeof stateCreator === 'function' | ||
? createUncurried(stateCreator) | ||
: createUncurried) as typeof zustand.create; | ||
|
||
const createStoreUncurried = <T>(stateCreator: zustand.StateCreator<T>) => { | ||
const store = actualCreateStore(stateCreator); | ||
const initialState = store.getState(); | ||
storeResetFns.add(() => { | ||
store.setState(initialState, true); | ||
}); | ||
return store; | ||
}) as typeof actualCreate; | ||
}; | ||
|
||
afterEach(() => { | ||
stores.forEach(resetFn => resetFn()); | ||
}); | ||
// 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 | ||
typeof stateCreator === 'function' | ||
? createStoreUncurried(stateCreator) | ||
: createStoreUncurried) as typeof zustand.createStore; | ||
|
||
const { createStore, useStore } = actualZustand; | ||
export { actualUseStore as useStore }; | ||
|
||
export { create, createStore, useStore }; | ||
// reset all stores after each test run | ||
afterEach(() => { | ||
act(() => { | ||
storeResetFns.forEach(resetFn => { | ||
resetFn(); | ||
}); | ||
}); | ||
}); |
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
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
Oops, something went wrong.