Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docs): support currying for testing mock #2137

Merged
merged 4 commits into from
Oct 22, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 48 additions & 24 deletions docs/guides/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <T>(stateCreator: zustand.StateCreator<T>) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming createInternalFn and createStoreInternalFn
to createTestStore, createStoreUncurried, or similar.

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 = (<T>() => {
console.log('zustand create mock')

return (stateCreator: zustand.StateCreator<T>) => {
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 = (<T>(stateCreator: zustand.StateCreator<T>) => {
console.log('zustand createStore mock')

const createStoreInternalFn = <T>(stateCreator: zustand.StateCreator<T>) => {
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 = (<T>(stateCreator: zustand.StateCreator<T>) => {
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
Expand Down Expand Up @@ -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 = <T>(stateCreator: zustand.StateCreator<T>) => {
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 = (<T>() => {
console.log('zustand create mock')

return (stateCreator: zustand.StateCreator<T>) => {
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 = (<T>(stateCreator: zustand.StateCreator<T>) => {
console.log('zustand createStore mock')

const createStoreInternalFn = <T>(stateCreator: zustand.StateCreator<T>) => {
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 = (<T>(stateCreator: zustand.StateCreator<T>) => {
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
Expand Down
Loading