-
Notifications
You must be signed in to change notification settings - Fork 0
/
redux_action_match.spec.coffee
59 lines (50 loc) · 1.7 KB
/
redux_action_match.spec.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
chai = require 'chai'
matchJS = require '../lib/match-js'
match = matchJS.match
otherwise = matchJS.otherwise
chai.expect()
expect = chai.expect
describe 'Matching on redux-like Action class:', ->
class Action
constructor:(type) ->
@type = type
@payload = undefined
unapply:(action) ->
if(action.type == @type)
return { payload: action.payload }
action:(payload)->
return {
type: this.type
payload: payload
}
ChangeUsername = new Action('ChangeUsername')
ChangeAddress = new Action('ChangeAddress')
StrangeAction = new Action('UJRBJLWYTU')
it 'ChangeUsername should match ChangeUsername', ->
action = ChangeUsername.action('myusername')
m = match(action)(
[ChangeUsername, ({payload}) -> 'name: '+payload],
[ChangeAddress, ({payload}) -> 'postcode: '+payload.postcode],
[otherwise, (_) -> 'unknown action: '+action]
)
expect(m).to.be.equal('name: myusername')
it 'ChangeAddress should match ChangeAddress', ->
action = ChangeAddress.action({
name: 'HOME ROAD',
postcode: '1234',
})
m = match(action)(
[ChangeUsername, ({payload}) -> 'name: '+payload],
[ChangeAddress, ({payload}) -> 'postcode: '+payload.postcode],
[otherwise, (_) -> 'unknown action: '+action]
)
expect(m).to.be.equal('postcode: 1234')
it 'StrangeAction should match otherwise', ->
action = StrangeAction.action('FSCRKCAWCN')
m = match(action)(
[ChangeUsername, ({payload}) -> 'name: '+payload],
[ChangeAddress, ({payload}) -> 'postcode: '+payload.postcode],
[otherwise, (_) -> 'unknown action: '+action]
)
expect(m).to.be.equal('unknown action: '+action)
return