UNPKG

chromogen-zustand

Version:

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

38 lines (37 loc) 1.54 kB
import { debounce } from '../utils/utils'; import { ledger } from '../utils/ledger'; import { recordingState } from '../utils/store'; const { transactions, initialRender, selectors, setTransactions } = ledger; const DEBOUNCE_MS = 250; export const debouncedAddToTransactions = debounce((key, value, params) => params !== undefined ? transactions[transactions.length - 1].familyUpdates.push({ key, value, params }) : transactions[transactions.length - 1].updates.push({ key, value }), DEBOUNCE_MS); export const wrapGetter = (key, get) => { let returnedPromise = false; return (utils) => { const value = get(utils); if (utils.get(recordingState)) { if (transactions.length === 0) { if (typeof value === 'object' && value !== null && value.constructor.name === 'Promise') { ledger.selectors = selectors.filter((current) => current !== key); returnedPromise = true; } else { initialRender.push({ key, value }); } } else if (!returnedPromise) { debouncedAddToTransactions(key, value); } } return value; }; }; export const wrapSetter = (key, set) => (utils, newValue) => { if (utils.get(recordingState) && setTransactions.length > 0) { setTimeout(() => { setTransactions[setTransactions.length - 1].setter = { key, newValue }; }, 0); } return set(utils, newValue); };