diff --git a/docs/guides/testing.md b/docs/guides/testing.md index e005639baa..7d10e8330f 100644 --- a/docs/guides/testing.md +++ b/docs/guides/testing.md @@ -77,30 +77,42 @@ const { create: actualCreate, createStore: actualCreateStore } = // a variable to hold reset functions for all stores declared in the app export const storeResetFns = new Set<() => void>() +const createUncurried = (stateCreator: zustand.StateCreator) => { + const store = actualCreate(stateCreator) + const initialState = store.getState() + 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 = (() => { console.log('zustand create mock') - return (stateCreator: zustand.StateCreator) => { - const store = actualCreate(stateCreator) - const initialState = store.getState() - storeResetFns.add(() => { - store.setState(initialState, true) - }) - return store - } + // to support curried version of create + return typeof stateCreator === 'function' + ? createUncurried(stateCreator) + : createUncurried }) as typeof zustand.create -// when creating a store, we get its initial state, create a reset function and add it in the set -export const createStore = ((stateCreator: zustand.StateCreator) => { - console.log('zustand createStore mock') - +const createStoreUncurried = (stateCreator: zustand.StateCreator) => { const store = actualCreateStore(stateCreator) const initialState = store.getState() 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 = ((stateCreator: zustand.StateCreator) => { + console.log('zustand createStore mock') + + // to support curried version of createStore + return typeof stateCreator === 'function' + ? createStoreUncurried(stateCreator) + : createStoreUncurried }) as typeof zustand.createStore // reset all stores after each test run @@ -154,30 +166,42 @@ const { create: actualCreate, createStore: actualCreateStore } = // a variable to hold reset functions for all stores declared in the app export const storeResetFns = new Set<() => void>() +const createUncurried = (stateCreator: zustand.StateCreator) => { + const store = actualCreate(stateCreator) + const initialState = store.getState() + 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 = (() => { console.log('zustand create mock') - return (stateCreator: zustand.StateCreator) => { - const store = actualCreate(stateCreator) - const initialState = store.getState() - storeResetFns.add(() => { - store.setState(initialState, true) - }) - return store - } + // to support curried version of create + return typeof stateCreator === 'function' + ? createUncurried(stateCreator) + : createUncurried }) as typeof zustand.create -// when creating a store, we get its initial state, create a reset function and add it in the set -export const createStore = ((stateCreator: zustand.StateCreator) => { - console.log('zustand createStore mock') - +const createStoreUncurried = (stateCreator: zustand.StateCreator) => { const store = actualCreateStore(stateCreator) const initialState = store.getState() 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 = ((stateCreator: zustand.StateCreator) => { + console.log('zustand createStore mock') + + // to support curried version of createStore + return typeof stateCreator === 'function' + ? createStoreUncurried(stateCreator) + : createStoreUncurried }) as typeof zustand.createStore // reset all stores after each test run