Skip to content

Commit 83b86a7

Browse files
authored
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
1 parent 93c8ca5 commit 83b86a7

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

docs/guides/testing.md

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -77,30 +77,42 @@ const { create: actualCreate, createStore: actualCreateStore } =
7777
// a variable to hold reset functions for all stores declared in the app
7878
export const storeResetFns = new Set<() => void>()
7979

80+
const createUncurried = <T>(stateCreator: zustand.StateCreator<T>) => {
81+
const store = actualCreate(stateCreator)
82+
const initialState = store.getState()
83+
storeResetFns.add(() => {
84+
store.setState(initialState, true)
85+
})
86+
return store
87+
}
88+
8089
// when creating a store, we get its initial state, create a reset function and add it in the set
8190
export const create = (<T>() => {
8291
console.log('zustand create mock')
8392

84-
return (stateCreator: zustand.StateCreator<T>) => {
85-
const store = actualCreate(stateCreator)
86-
const initialState = store.getState()
87-
storeResetFns.add(() => {
88-
store.setState(initialState, true)
89-
})
90-
return store
91-
}
93+
// to support curried version of create
94+
return typeof stateCreator === 'function'
95+
? createUncurried(stateCreator)
96+
: createUncurried
9297
}) as typeof zustand.create
9398

94-
// when creating a store, we get its initial state, create a reset function and add it in the set
95-
export const createStore = (<T>(stateCreator: zustand.StateCreator<T>) => {
96-
console.log('zustand createStore mock')
97-
99+
const createStoreUncurried = <T>(stateCreator: zustand.StateCreator<T>) => {
98100
const store = actualCreateStore(stateCreator)
99101
const initialState = store.getState()
100102
storeResetFns.add(() => {
101103
store.setState(initialState, true)
102104
})
103105
return store
106+
}
107+
108+
// when creating a store, we get its initial state, create a reset function and add it in the set
109+
export const createStore = (<T>(stateCreator: zustand.StateCreator<T>) => {
110+
console.log('zustand createStore mock')
111+
112+
// to support curried version of createStore
113+
return typeof stateCreator === 'function'
114+
? createStoreUncurried(stateCreator)
115+
: createStoreUncurried
104116
}) as typeof zustand.createStore
105117

106118
// reset all stores after each test run
@@ -154,30 +166,42 @@ const { create: actualCreate, createStore: actualCreateStore } =
154166
// a variable to hold reset functions for all stores declared in the app
155167
export const storeResetFns = new Set<() => void>()
156168

169+
const createUncurried = <T>(stateCreator: zustand.StateCreator<T>) => {
170+
const store = actualCreate(stateCreator)
171+
const initialState = store.getState()
172+
storeResetFns.add(() => {
173+
store.setState(initialState, true)
174+
})
175+
return store
176+
}
177+
157178
// when creating a store, we get its initial state, create a reset function and add it in the set
158179
export const create = (<T>() => {
159180
console.log('zustand create mock')
160181

161-
return (stateCreator: zustand.StateCreator<T>) => {
162-
const store = actualCreate(stateCreator)
163-
const initialState = store.getState()
164-
storeResetFns.add(() => {
165-
store.setState(initialState, true)
166-
})
167-
return store
168-
}
182+
// to support curried version of create
183+
return typeof stateCreator === 'function'
184+
? createUncurried(stateCreator)
185+
: createUncurried
169186
}) as typeof zustand.create
170187

171-
// when creating a store, we get its initial state, create a reset function and add it in the set
172-
export const createStore = (<T>(stateCreator: zustand.StateCreator<T>) => {
173-
console.log('zustand createStore mock')
174-
188+
const createStoreUncurried = <T>(stateCreator: zustand.StateCreator<T>) => {
175189
const store = actualCreateStore(stateCreator)
176190
const initialState = store.getState()
177191
storeResetFns.add(() => {
178192
store.setState(initialState, true)
179193
})
180194
return store
195+
}
196+
197+
// when creating a store, we get its initial state, create a reset function and add it in the set
198+
export const createStore = (<T>(stateCreator: zustand.StateCreator<T>) => {
199+
console.log('zustand createStore mock')
200+
201+
// to support curried version of createStore
202+
return typeof stateCreator === 'function'
203+
? createStoreUncurried(stateCreator)
204+
: createStoreUncurried
181205
}) as typeof zustand.createStore
182206

183207
// reset all stores after each test run

0 commit comments

Comments
 (0)