Last active
April 14, 2017 10:06
-
-
Save mori-dev/1763a8a23c36086482881d254f94af7a to your computer and use it in GitHub Desktop.
redux アプリで定期実行処理を書くには / saga のテスト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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