From 0772db5ba5e9fa2dda874cbca125598212e328b2 Mon Sep 17 00:00:00 2001 From: plrs9816 <61955474+plrs9816@users.noreply.github.com> Date: Sun, 22 Oct 2023 08:15:37 +0900 Subject: [PATCH 1/4] fix(docs): support currying for testing mock --- 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..2d2223125e 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 createInternalFn = (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" + ? createInternalFn(stateCreator) + : createInternalFn }) 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 createStoreInternalFn = (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" + ? createStoreInternalFn(stateCreator) + : createStoreInternalFn }) 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 createInternalFn = (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" + ? createInternalFn(stateCreator) + : createInternalFn }) 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 createStoreInternalFn = (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" + ? createStoreInternalFn(stateCreator) + : createStoreInternalFn }) as typeof zustand.createStore // reset all stores after each test run From 30736f832b40fea6f1225fc8d4216c15fbce0ffe Mon Sep 17 00:00:00 2001 From: plrs9816 <61955474+plrs9816@users.noreply.github.com> Date: Sun, 22 Oct 2023 19:45:31 +0900 Subject: [PATCH 2/4] change function name --- docs/guides/testing.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/guides/testing.md b/docs/guides/testing.md index 2d2223125e..9cc8048aad 100644 --- a/docs/guides/testing.md +++ b/docs/guides/testing.md @@ -77,7 +77,7 @@ 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 createInternalFn = (stateCreator: zustand.StateCreator) => { +const createUncurried = (stateCreator: zustand.StateCreator) => { const store = actualCreate(stateCreator) const initialState = store.getState() storeResetFns.add(() => { @@ -92,11 +92,11 @@ export const create = (() => { // to support curried version of create return typeof stateCreator === "function" - ? createInternalFn(stateCreator) - : createInternalFn + ? createUncurried(stateCreator) + : createUncurried }) as typeof zustand.create -const createStoreInternalFn = (stateCreator: zustand.StateCreator) => { +const createStoreUncurried = (stateCreator: zustand.StateCreator) => { const store = actualCreateStore(stateCreator) const initialState = store.getState() storeResetFns.add(() => { @@ -111,8 +111,8 @@ export const createStore = ((stateCreator: zustand.StateCreator) => { // to support curried version of createStore return typeof stateCreator === "function" - ? createStoreInternalFn(stateCreator) - : createStoreInternalFn + ? createStoreUncurried(stateCreator) + : createStoreUncurried }) as typeof zustand.createStore // reset all stores after each test run @@ -166,7 +166,7 @@ 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 createInternalFn = (stateCreator: zustand.StateCreator) => { +const createUncurried = (stateCreator: zustand.StateCreator) => { const store = actualCreate(stateCreator) const initialState = store.getState() storeResetFns.add(() => { @@ -181,11 +181,11 @@ export const create = (() => { // to support curried version of create return typeof stateCreator === "function" - ? createInternalFn(stateCreator) - : createInternalFn + ? createUncurried(stateCreator) + : createUncurried }) as typeof zustand.create -const createStoreInternalFn = (stateCreator: zustand.StateCreator) => { +const createStoreUncurried = (stateCreator: zustand.StateCreator) => { const store = actualCreateStore(stateCreator) const initialState = store.getState() storeResetFns.add(() => { @@ -200,8 +200,8 @@ export const createStore = ((stateCreator: zustand.StateCreator) => { // to support curried version of createStore return typeof stateCreator === "function" - ? createStoreInternalFn(stateCreator) - : createStoreInternalFn + ? createStoreUncurried(stateCreator) + : createStoreUncurried }) as typeof zustand.createStore // reset all stores after each test run From 4fc422b6cd5086d76f7452db2594f611e96a9263 Mon Sep 17 00:00:00 2001 From: plrs9816 <61955474+plrs9816@users.noreply.github.com> Date: Sun, 22 Oct 2023 21:12:09 +0900 Subject: [PATCH 3/4] fix lint error --- docs/guides/testing.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/guides/testing.md b/docs/guides/testing.md index 9cc8048aad..7d10e8330f 100644 --- a/docs/guides/testing.md +++ b/docs/guides/testing.md @@ -91,7 +91,7 @@ export const create = (() => { console.log('zustand create mock') // to support curried version of create - return typeof stateCreator === "function" + return typeof stateCreator === 'function' ? createUncurried(stateCreator) : createUncurried }) as typeof zustand.create @@ -110,7 +110,7 @@ export const createStore = ((stateCreator: zustand.StateCreator) => { console.log('zustand createStore mock') // to support curried version of createStore - return typeof stateCreator === "function" + return typeof stateCreator === 'function' ? createStoreUncurried(stateCreator) : createStoreUncurried }) as typeof zustand.createStore @@ -180,7 +180,7 @@ export const create = (() => { console.log('zustand create mock') // to support curried version of create - return typeof stateCreator === "function" + return typeof stateCreator === 'function' ? createUncurried(stateCreator) : createUncurried }) as typeof zustand.create @@ -199,7 +199,7 @@ export const createStore = ((stateCreator: zustand.StateCreator) => { console.log('zustand createStore mock') // to support curried version of createStore - return typeof stateCreator === "function" + return typeof stateCreator === 'function' ? createStoreUncurried(stateCreator) : createStoreUncurried }) as typeof zustand.createStore From 543591d06ca89d6fcf424b44a943c8f7b54e0f82 Mon Sep 17 00:00:00 2001 From: plrs9816 <61955474+plrs9816@users.noreply.github.com> Date: Sun, 22 Oct 2023 21:25:38 +0900 Subject: [PATCH 4/4] empty commit to trigger checks