-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port DownloadStore/DownloadModel to zustand
- Loading branch information
Johnathan Bell
authored and
Johnathan Bell
committed
Dec 10, 2024
1 parent
31c6cda
commit d7d441a
Showing
16 changed files
with
436 additions
and
149 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
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,65 @@ | ||
// __mocks__/zustand.ts | ||
import { act } from '@testing-library/react' | ||
import type * as ZustandExportedTypes from 'zustand' | ||
export * from 'zustand' | ||
|
||
const { create: actualCreate, createStore: actualCreateStore } = | ||
jest.requireActual<typeof ZustandExportedTypes>('zustand') | ||
|
||
// a variable to hold reset functions for all stores declared in the app | ||
export const storeResetFns = new Set<() => void>() | ||
|
||
const createUncurried = <T>( | ||
stateCreator: ZustandExportedTypes.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 | ||
export const create = (<T>( | ||
stateCreator: ZustandExportedTypes.StateCreator<T>, | ||
) => { | ||
console.log('zustand create mock') | ||
|
||
// to support curried version of create | ||
return typeof stateCreator === 'function' | ||
? createUncurried(stateCreator) | ||
: createUncurried | ||
}) as typeof ZustandExportedTypes.create | ||
|
||
const createStoreUncurried = <T>( | ||
stateCreator: ZustandExportedTypes.StateCreator<T>, | ||
) => { | ||
const store = actualCreateStore(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 | ||
export const createStore = (<T>( | ||
stateCreator: ZustandExportedTypes.StateCreator<T>, | ||
) => { | ||
console.log('zustand createStore mock') | ||
|
||
// to support curried version of createStore | ||
return typeof stateCreator === 'function' | ||
? createStoreUncurried(stateCreator) | ||
: createStoreUncurried | ||
}) as typeof ZustandExportedTypes.createStore | ||
|
||
// 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.