Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
redux アプリで定期実行処理を書くには / saga のテスト
import assert from 'power-assert';
import { createMockTask } from 'redux-saga/lib/utils';
import { take, fork, cancel, put, call } from 'redux-saga/effects';
import { delay } from 'redux-saga';
import * as actions from '../../../src/actions/periodic_inspections/some_check';
import * as ActionTypes from '../../../src/actions/action_types';
import {
watchSampleTrigger,
worker,
intervalMs
} from '../../../src/sagas/periodic_inspections/location_share_progress_check';
describe('watchSampleTrigger', () => {
const generator = watchSampleTrigger();
it('CHECK_SAMPLE_TRIGGER を待ち受けること', () => {
const expectedYield = take(ActionTypes.CHECK_SAMPLE_TRIGGER);
assert.deepStrictEqual(generator.next().value, expectedYield);
});
it('ワーカーをフォークすること', () => {
const mockTask = createMockTask();
const expectedYield = fork(worker);
assert.deepStrictEqual(generator.next(mockTask).value, expectedYield);
});
it('CHECK_SAMPLE_STOP と LOGOUT_OK を待ち受けて、キャンセルすること', () => {
const mockTask = createMockTask();
const expectedTakeYield = take([ActionTypes.CHECK_SAMPLE_STOP, ActionTypes.LOGOUT_OK]);
assert.deepStrictEqual(generator.next(mockTask).value, expectedTakeYield);
const expectedCancelYield = cancel(mockTask);
assert.deepStrictEqual(generator.next().value, expectedCancelYield);
});
});
describe('worker', () => {
const generator = worker();
it('actions.exec が実行されること', () => {
const expectedYield = put(actions.exec());
assert.deepStrictEqual(generator.next().value, expectedYield);
});
it('指定した時間待機されること', () => {
const expectedYield = call(delay, intervalMs);
assert.deepStrictEqual(generator.next().value, expectedYield);
});
it('ふたたび actions.exec が実行されること', () => {
const expectedYield = put(actions.exec());
assert.deepStrictEqual(generator.next().value, expectedYield);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment