mirror of
https://github.com/inaturalist/iNaturalistReactNative.git
synced 2025-12-23 14:08:56 -05:00
* MOB-1008 add lint config for consistent ts imports * auto-fix consistent imports * auto-fix consistent imports * reapply autofix from latest main * Extend Props interface from PropsWithChildren --------- Co-authored-by: Johannes Klein <johannes.t.klein@gmail.com>
48 lines
1.8 KiB
TypeScript
48 lines
1.8 KiB
TypeScript
// Mostly from https://github.com/pmndrs/zustand/blob/main/docs/guides/testing.md#jest
|
|
|
|
import { cloneDeep } from "lodash";
|
|
import type * as zustand from "zustand";
|
|
|
|
const { create: actualCreate, createStore: actualCreateStore }
|
|
= jest.requireActual<typeof zustand>( "zustand" );
|
|
|
|
// Reset functions for all stores declared in the app. These will get run in
|
|
// tests/jest.post-setup.js in an afterEach callback
|
|
export const storeResetFns = new Set<() => void>();
|
|
|
|
const createUncurried = <T>( stateCreator: zustand.StateCreator<T> ) => {
|
|
const store = actualCreate( stateCreator );
|
|
// cloneDeep may not be totally necessary, but it assuages some paranoia
|
|
// about this object getting mutated
|
|
const initialState = cloneDeep( 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>( stateCreator: zustand.StateCreator<T> ) => (
|
|
// to support curried version of create
|
|
typeof stateCreator === "function"
|
|
? createUncurried( stateCreator )
|
|
: createUncurried
|
|
) ) as typeof zustand.create;
|
|
|
|
const createStoreUncurried = <T>( stateCreator: zustand.StateCreator<T> ) => {
|
|
const store = actualCreateStore( stateCreator );
|
|
const initialState = cloneDeep( 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> ) => (
|
|
// to support curried version of createStore
|
|
typeof stateCreator === "function"
|
|
? createStoreUncurried( stateCreator )
|
|
: createStoreUncurried
|
|
) ) as typeof zustand.createStore;
|