UNPKG

@brightlayer-ui/react-auth-workflow

Version:

Re-usable workflow components for Authentication and Registration within Eaton applications.

46 lines (45 loc) 1.09 kB
import { subMilliseconds, differenceInMilliseconds } from 'date-fns'; let timeTravelOffset = 0; let frozenTime; const internalNow = () => { if (frozenTime) { return frozenTime; } return subMilliseconds(new Date(), timeTravelOffset); }; const now = () => internalNow(); const assertInTest = () => { if (process.env.NODE_ENV !== 'test') { throw new Error('Time travel is only allowed in tests!'); } }; const timeTravel = (time) => { assertInTest(); timeTravelOffset = differenceInMilliseconds(new Date(), typeof time === 'string' ? new Date(time) : time); frozenTime = undefined; }; const restoreTime = () => { assertInTest(); timeTravelOffset = 0; frozenTime = undefined; }; const freezeTime = (time) => { assertInTest(); if (time) { timeTravel(time); } frozenTime = internalNow(); }; const unfreezeTime = () => { assertInTest(); frozenTime = undefined; }; export const Clock = { now, Testing: { timeTravel, restoreTime, freezeTime, unfreezeTime, }, };