UNPKG

overmind

Version:
315 lines • 9.27 kB
import { Overmind, pipe, branch, filter, fork, when, wait, debounce, parallel, catchError, tryCatch, throttle, waitUntil, } from './'; describe('OPERATORS', () => { test('branch - passes input as output', async () => { expect.assertions(1); const state = { foo: 'bar', }; const actions = { test: pipe(branch((_, value) => value.toUpperCase()), ({ state }, value) => { state.foo = value; }), }; const config = { state, actions, }; const overmind = new Overmind(config); await overmind.actions.test('foo'); expect(overmind.state.foo).toBe('foo'); }); test('action - return value', async () => { expect.assertions(1); const state = { foo: 'bar', }; const actions = { test: pipe((_, value) => value.toUpperCase(), ({ state }, value) => { state.foo = value; }), }; const config = { state, actions, }; const overmind = new Overmind(config); await overmind.actions.test('foo'); expect(overmind.state.foo).toBe('FOO'); }); test('action - return value async', () => { expect.assertions(1); const test = pipe((_, value) => Promise.resolve(value.toUpperCase()), ({ state }, value) => { state.foo = value; }); const state = { foo: 'bar', }; const actions = { test }; const config = { state, actions, }; const overmind = new Overmind(config); return overmind.actions.test('foo').then(() => { expect(overmind.state.foo).toBe('FOO'); }); }); test('parallel', async () => { expect.assertions(1); const operatorA = () => { return Promise.resolve('A'); }; const operatorB = () => { return Promise.resolve('B'); }; const actions = { test: parallel(operatorA, operatorB), }; const config = { actions, }; const overmind = new Overmind(config); const result = await overmind.actions.test(); expect(result).toEqual(['A', 'B']); }); test('filter - truthy', () => { expect.assertions(1); const test = pipe(filter((_, value) => value === 'foo'), ({ state }, value) => (state.foo = value.toUpperCase())); const state = { foo: 'bar', }; const actions = { test, }; const config = { state, actions, }; const overmind = new Overmind(config); return overmind.actions.test('foo').then(() => { expect(overmind.state.foo).toBe('FOO'); }); }); test('filter - falsy', () => { const test = pipe(filter((_, value) => value === 'bar'), ({ state }, value) => (state.foo = value.toUpperCase())); const state = { foo: 'bar', }; const actions = { test, }; const config = { state, actions, }; const overmind = new Overmind(config); return overmind.actions.test('foo').then(() => { expect(overmind.state.foo).toBe('bar'); }); }); test('fork', () => { expect.assertions(1); const test = pipe(() => ({ type: 'foo' }), fork('type', { foo: () => { return 'FOO'; }, bar: () => { return 'BAR'; }, }), ({ state }, value) => { state.foo = value; }); const state = { foo: 'bar', }; const actions = { test, }; const config = { state, actions, }; const overmind = new Overmind(config); return overmind.actions.test().then(() => { expect(overmind.state.foo).toBe('FOO'); }); }); test('when', () => { expect.assertions(1); const test = when(() => true, { true: ({ state }, value) => { state.foo = value.toUpperCase(); }, false: ({ state }, value) => (state.number = Number(value)), }); const state = { foo: 'bar', number: 0, }; const actions = { test, }; const config = { state, actions, }; const overmind = new Overmind(config); return overmind.actions.test('foo').then(() => { expect(overmind.state.foo).toBe('FOO'); }); }); test('wait', () => { expect.assertions(1); const runTime = Date.now(); const test = wait(500); const actions = { test }; const config = { actions, }; const overmind = new Overmind(config); return overmind.actions.test().then(() => { expect(Date.now() - runTime).toBeGreaterThanOrEqual(500); }); }); test('debounce', () => { expect.assertions(1); const test = pipe(debounce(100), ({ state }) => state.runCount++); const state = { runCount: 0, }; const actions = { test, }; const config = { state, actions, }; const overmind = new Overmind(config); return Promise.all([overmind.actions.test(), overmind.actions.test()]).then(() => { expect(overmind.state.runCount).toBe(1); }); }); test('throttle', () => { expect.assertions(1); const test = pipe(throttle(0), ({ state }) => state.runCount++); const state = { runCount: 0, }; const actions = { test, }; const config = { state, actions, }; const overmind = new Overmind(config); return Promise.all([overmind.actions.test(), overmind.actions.test()]).then(() => { expect(overmind.state.runCount).toBe(1); }); }); test('catchError', () => { expect.assertions(3); const test = pipe((() => { throw new Error('wut?!?'); }), ({ state }, payload) => { state.runCount++; return payload; }, catchError(({ state }, error) => { state.error = error.message; return 'hm'; }), ({ state }, value) => { state.foo = value; }); const state = { runCount: 0, foo: 'bar', error: '', }; const actions = { test, }; const config = { state, actions, }; const overmind = new Overmind(config); return overmind.actions.test('baz').then(() => { expect(overmind.state.runCount).toBe(0); expect(overmind.state.error).toBe('wut?!?'); expect(overmind.state.foo).toBe('hm'); }); }); test('tryCatch - resolves', () => { expect.assertions(1); const test = tryCatch({ try: ({ state }, value) => { state.foo = value; }, catch: () => { }, }); const state = { foo: 'bar', }; const actions = { test, }; const config = { state, actions, }; const overmind = new Overmind(config); return overmind.actions.test('baz').then(() => { expect(overmind.state.foo).toBe('baz'); }); }); test('tryCatch - fails', () => { expect.assertions(1); const test = tryCatch({ try: (() => { throw new Error('ehm'); }), catch: ({ state }, value) => { state.foo = value.message; }, }); const state = { foo: 'bar', }; const actions = { test, }; const config = { state, actions, }; const overmind = new Overmind(config); return overmind.actions.test('baz').then(() => { expect(overmind.state.foo).toBe('ehm'); }); }); test('waitUntil', () => { expect.assertions(1); const increaseCount = pipe(({ state }) => state.runCount++); const test = pipe(waitUntil((state) => state.runCount === 1), ({ state }) => (state.hasRun = true)); const state = { runCount: 0, hasRun: false, }; const actions = { increaseCount, test, }; const config = { state, actions, }; const overmind = new Overmind(config); setTimeout(() => { overmind.actions.increaseCount(); }, 0); return overmind.actions.test().then(() => { expect(overmind.state.runCount).toBe(1); }); }); }); //# sourceMappingURL=operators.test.js.map