UNPKG

overmind

Version:
318 lines • 9.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const _1 = require("./"); describe('OPERATORS', () => { test('branch - passes input as output', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { expect.assertions(1); const state = { foo: 'bar', }; const actions = { test: (0, _1.pipe)((0, _1.branch)((_, value) => value.toUpperCase()), ({ state }, value) => { state.foo = value; }), }; const config = { state, actions, }; const overmind = new _1.Overmind(config); yield overmind.actions.test('foo'); expect(overmind.state.foo).toBe('foo'); })); test('action - return value', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { expect.assertions(1); const state = { foo: 'bar', }; const actions = { test: (0, _1.pipe)((_, value) => value.toUpperCase(), ({ state }, value) => { state.foo = value; }), }; const config = { state, actions, }; const overmind = new _1.Overmind(config); yield overmind.actions.test('foo'); expect(overmind.state.foo).toBe('FOO'); })); test('action - return value async', () => { expect.assertions(1); const test = (0, _1.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 _1.Overmind(config); return overmind.actions.test('foo').then(() => { expect(overmind.state.foo).toBe('FOO'); }); }); test('parallel', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { expect.assertions(1); const operatorA = () => { return Promise.resolve('A'); }; const operatorB = () => { return Promise.resolve('B'); }; const actions = { test: (0, _1.parallel)(operatorA, operatorB), }; const config = { actions, }; const overmind = new _1.Overmind(config); const result = yield overmind.actions.test(); expect(result).toEqual(['A', 'B']); })); test('filter - truthy', () => { expect.assertions(1); const test = (0, _1.pipe)((0, _1.filter)((_, value) => value === 'foo'), ({ state }, value) => (state.foo = value.toUpperCase())); const state = { foo: 'bar', }; const actions = { test, }; const config = { state, actions, }; const overmind = new _1.Overmind(config); return overmind.actions.test('foo').then(() => { expect(overmind.state.foo).toBe('FOO'); }); }); test('filter - falsy', () => { const test = (0, _1.pipe)((0, _1.filter)((_, value) => value === 'bar'), ({ state }, value) => (state.foo = value.toUpperCase())); const state = { foo: 'bar', }; const actions = { test, }; const config = { state, actions, }; const overmind = new _1.Overmind(config); return overmind.actions.test('foo').then(() => { expect(overmind.state.foo).toBe('bar'); }); }); test('fork', () => { expect.assertions(1); const test = (0, _1.pipe)(() => ({ type: 'foo' }), (0, _1.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 _1.Overmind(config); return overmind.actions.test().then(() => { expect(overmind.state.foo).toBe('FOO'); }); }); test('when', () => { expect.assertions(1); const test = (0, _1.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 _1.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 = (0, _1.wait)(500); const actions = { test }; const config = { actions, }; const overmind = new _1.Overmind(config); return overmind.actions.test().then(() => { expect(Date.now() - runTime).toBeGreaterThanOrEqual(500); }); }); test('debounce', () => { expect.assertions(1); const test = (0, _1.pipe)((0, _1.debounce)(100), ({ state }) => state.runCount++); const state = { runCount: 0, }; const actions = { test, }; const config = { state, actions, }; const overmind = new _1.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 = (0, _1.pipe)((0, _1.throttle)(0), ({ state }) => state.runCount++); const state = { runCount: 0, }; const actions = { test, }; const config = { state, actions, }; const overmind = new _1.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 = (0, _1.pipe)((() => { throw new Error('wut?!?'); }), ({ state }, payload) => { state.runCount++; return payload; }, (0, _1.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 _1.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 = (0, _1.tryCatch)({ try: ({ state }, value) => { state.foo = value; }, catch: () => { }, }); const state = { foo: 'bar', }; const actions = { test, }; const config = { state, actions, }; const overmind = new _1.Overmind(config); return overmind.actions.test('baz').then(() => { expect(overmind.state.foo).toBe('baz'); }); }); test('tryCatch - fails', () => { expect.assertions(1); const test = (0, _1.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 _1.Overmind(config); return overmind.actions.test('baz').then(() => { expect(overmind.state.foo).toBe('ehm'); }); }); test('waitUntil', () => { expect.assertions(1); const increaseCount = (0, _1.pipe)(({ state }) => state.runCount++); const test = (0, _1.pipe)((0, _1.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 _1.Overmind(config); setTimeout(() => { overmind.actions.increaseCount(); }, 0); return overmind.actions.test().then(() => { expect(overmind.state.runCount).toBe(1); }); }); }); //# sourceMappingURL=operators.test.js.map