bdn-pocket
Version:
pocket tools for managing redux and redux-saga
103 lines (87 loc) • 2.25 kB
JavaScript
import test from 'ava'
import Action, { Types } from '../src/action'
import ActionType from '../src/action_type'
const { string } = Types
test('Action definition', t => {
const a = Action.def('my action')
t.is(
a.CONST,
'my-app/MY_ACTION',
'generate an action creator with a constant defined by def function'
)
// console.log(a({ propA: 'a' }))
})
test('Action redefinition', t => {
const myActionType = ActionType({
prefix: 'coucou',
transformer: x => x,
})
const MyActionDef = Action.conf({ actionType: myActionType })
const b = MyActionDef.def('my action')
t.is(
b.CONST,
'coucou/my action',
'you can override default Action behaviour by defining a new ActionDefiniton (ActionType)'
)
const MyOtherActionDef = Action.prefix('hello')
const c = MyOtherActionDef.def('my other action')
t.is(
c.CONST,
'hello/MY_OTHER_ACTION',
'you can override default Action behaviour by changing prefix'
)
const v = Action.def('initial action')
t.is(
v.CONST,
'my-app/INITIAL_ACTION',
'initial action creator keep always the same configuration'
)
})
test('Action#create action', t => {
t.throws(
() => Action({ a: 'a' }),
Error,
'You must define a name of an action creator'
)
const myAction = Action.def('action name')
const payload = {a: 'a', b: 'b' }
const a = myAction(payload)
t.is(
a.type,
myAction.CONST,
'create an action with type equals to CONST value'
)
t.deepEqual(
a.payload,
payload,
'create an action with payload equals to props send as first argument'
)
})
test('Action#propTypes', t => {
const myAction = Action
.def('action name')
.propTypes('a', 'b')
const myAction2 = Action
.def('action name 2')
.propTypes({
a: string,
b: string,
})
const actions = [myAction, myAction2]
actions.forEach(a => {
t.notThrows(
() => a({ a: 'a', b: 'b' }),
'you can ensure props validation - props ok'
)
t.throws(
() => a({ a: 'a' }),
Error,
'you can ensure props validation - missing prop'
)
})
t.throws(
() => myAction2({ a: 'a', b: 10 }),
Error,
'you can ensure props validation - bad type prop'
)
})