From 83b86a716c66253a5ab95781a17e08fc090cea69 Mon Sep 17 00:00:00 2001 From: plrs9816 <61955474+plrs9816@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:05:43 +0900 Subject: [PATCH] fix(docs): support currying for testing mock (#2137) * fix(docs): support currying for testing mock * change function name * fix lint error * empty commit to trigger checks --- docs/guides/testing.md | 72 ++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 24 deletions(-) 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