UNPKG

chromogen-zustand

Version:

simple, interaction-driven Jest test generator for Recoil and React Hooks apps

42 lines (41 loc) 1.67 kB
import { ledger } from '../utils/ledger'; import { dummyParam } from '../utils/utils'; import { recordingState } from '../utils/store'; import { debouncedAddToTransactions } from './core-utils'; const { transactions, selectorFamilies, initialRenderFamilies, setTransactions } = ledger; export const wrapFamilyGetter = (key, configGet) => { let returnedPromise = false; return (params) => (utils) => { const { get } = utils; const value = configGet(params)(utils); if (get(recordingState)) { if (transactions.length === 0) { if (typeof value === 'object' && value !== null && Object.prototype.toString.call(value) === '[object Promise]') { delete selectorFamilies[key]; returnedPromise = true; } else { initialRenderFamilies.push({ key, params, value }); } } else if (!returnedPromise) { if (!selectorFamilies[key].prevParams.has(params)) { selectorFamilies[key].prevParams.add(params); } if (params !== dummyParam) debouncedAddToTransactions(key, value, params); } } return value; }; }; export const wrapFamilySetter = (key, set) => (params) => (utils, newValue) => { if (utils.get(recordingState) && setTransactions.length > 0) { setTimeout(() => { setTransactions[setTransactions.length - 1].setter = { key, params, newValue }; }, 0); } return set(params)(utils, newValue); };