diff --git a/package.json b/package.json index f5f1670..f1ab40b 100644 --- a/package.json +++ b/package.json @@ -6,11 +6,13 @@ "license": "MIT", "main": "lib/index.js", "scripts": { + "test": "babel-tape-runner test/*.js", "prepublish": "rm -rf lib && babel src --out-dir lib", "postpublish": "rm -rf lib" }, "dependencies": {}, "devDependencies": { + "babel-cli": "^6.6.5", "babel-preset-es2015": "^6.3.13", "babel-tape-runner": "^2.0.0", "tape": "^4.2.0" diff --git a/src/index.js b/src/index.js index 071c785..f6138d9 100644 --- a/src/index.js +++ b/src/index.js @@ -8,29 +8,18 @@ const RANDOM = 'EFFECT_RANDOM' * Random number generator */ -function random (rng=Math.random.bind(Math)) { - return () => next => effect => - effect.type === RANDOM +function randomMiddleware (rng = () => Math.random()) { + return () => next => action => + action.type === RANDOM ? Promise.resolve(rng()) - : next(effect) + : next(action) } -/** - * Action creator - */ - - function random () { - return { - type: RANDOM - } - } - - /** * Exports */ -export default random +export default randomMiddleware export { - random + RANDOM } diff --git a/test/index.js b/test/index.js index 37c44fc..b764aee 100644 --- a/test/index.js +++ b/test/index.js @@ -3,20 +3,40 @@ */ import test from 'tape' -import random from '../src' +import random, { RANDOM } from '../src' /** * Tests */ -test('should work', ({equal, end}) => { - const mw = random(rfc1149)({})(() => {}) +test(`should resolve number when using custom random function`, ({equal, end}) => { + const next = (action) => action + const mw = random(() => 4)()(next) + + mw({ type: RANDOM }).then((value) => { + equal(value, 4) + }) + + end() +}) + +test(`should resolve number when using default random function`, ({equal, end}) => { + const next = (action) => action + const mw = random()()(next) + + mw({ type: RANDOM }).then((value) => { + equal(typeof value, 'number') + }) - equal(mw({type: 'RANDOM'}), 4) end() }) -function rfc1149 () { - // Chosen by fair dice roll - return 4 -} + +test('should return next action when action type is ANY', ({equal, end}) => { + const next = (action) => action + const mw = random()()(next) + + equal(mw({ type: 'ANY' }).type, 'ANY') + + end() +})