bdn-pocket
Version:
pocket tools for managing redux and redux-saga
119 lines (105 loc) • 2.13 kB
JavaScript
import test from 'ava'
import R from 'ramda'
import { Messenger } from '../src/messenger'
import Action from '../src/action'
import { checkMessages } from './lib'
test('Messenger - a link between action and reducer', t => {
const actionKey = 'actionKey'
const myAction = Action.def('hello')
const reducer = (state, action) => 'reducer'
const msgr = Messenger
.add({
key: actionKey,
action: myAction,
reducer,
})
.create()
t.true(
R.has(actionKey, msgr),
'Messenger has actionKey in its props'
)
t.true(
msgr[actionKey] === myAction,
'Messenger has action definition'
)
t.true(
R.has('reducers', msgr),
'Messenger has reducers props'
)
t.true(
R.has(myAction.CONST, msgr.reducers),
'You can retreive reducer linked to an action definition'
)
t.true(
msgr.reducers[myAction.CONST]() === reducer(),
'function linked to action definition is the defined reducer'
)
})
test('Messenger#add', t => {
const reducer = (state, action) => state
const msgr = Messenger
.add({
key: 'a',
action: Action.def('a'),
reducer,
})
.add({
key: 'b',
action: Action.def('b'),
reducer,
})
.add({
key: 'c',
action: Action.def('c'),
reducer,
})
.create()
checkMessages(msgr, ['a', 'b', 'c'], t)
})
test('Messenger#add checks', t => {
const reducer = (state, action) => state
const msgr = Messenger
.add({
key: 'a',
action: Action.def('a'),
reducer,
})
t.throws(
() => (
msgr.add({
})
),
Error,
'key is mandatory'
)
t.throws(
() => (
msgr.add({
key: 'hello'
})
),
Error,
'action must be defined and have a CONST prop'
)
t.throws(
() => (
msgr.add({
key: 'hello',
action: Action.def('b')
})
),
Error,
'reducer must be a function'
)
t.throws(
() => (
msgr.add({
key: 'a',
action: Action.def('b'),
reducer
})
),
Error,
'you cannot add duplicate keys'
)
})