forked from cpatni/aasm-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.spec.coffee
109 lines (98 loc) · 3.47 KB
/
state.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
State = require '../lib/state'
describe 'State', ->
beforeEach ->
@name = 'astate'
@options = { crazyCustomKey: 'key' }
@state = new State(@name, @options)
it 'should set the name', ->
expect(@state.name).toEqual(@name)
it 'should set the options and expose them as options', ->
expect(@state.options).toEqual(@options)
it 'should be equal to a State of the same name', ->
expect(@state.name).toEqual(new State("astate").name)
it 'should send a message to the record for an action if the action is present as a string', ->
state = new State("astate", { entering: 'foo' })
record = foo: ->
spyOn(record, 'foo')
state.callAction('entering', record)
expect(record.foo).toHaveBeenCalled()
it 'should send a message to the record for each action', ->
state = new State('astate', {entering: ['a', 'b', 'c', () -> @foobar()]})
record =
a: ->
b: ->
c: ->
foobar: ->
spyOn(record, 'a')
spyOn(record, 'b')
spyOn(record, 'c')
spyOn(record, 'foobar')
state.callAction('entering', record)
expect(record.a).toHaveBeenCalled()
expect(record.b).toHaveBeenCalled()
expect(record.c).toHaveBeenCalled()
expect(record.foobar).toHaveBeenCalled()
it "should stop calling actions if one of them raises :halt_aasm_chain", ->
state = new State('astate', {entering: ['a', 'b', 'c']})
record =
a: ->
b: ->
c: ->
spyOn(record, 'a')
spyOn(record, 'b').andThrow('halt_aasm_chain')
spyOn(record, 'c')
state.callAction('entering', record)
expect(record.a).toHaveBeenCalled()
expect(record.b).toHaveBeenCalled()
expect(record.c).not.toHaveBeenCalled()
it 'should call a function, passing in the record for an action if the action is present', ->
state = new State('astate', {entering: () -> @foobar()})
record = foobar: ->
spyOn(record, 'foobar')
state.callAction('entering', record)
expect(record.foobar).toHaveBeenCalled()
describe 'ErrorHandling', ->
it 'should handleError for chain of methods (excepts:halt_aasm_chain), if method provided as string', ->
state = new State('astate', {entering: ['a', 'b', 'c'],onError:"error"})
record =
a: ->
b: ->
c: ->
error: ->
spyOn(record, 'a')
spyOn(record, 'b').andThrow('some other Error')
spyOn(record, 'c')
spyOn(record, 'error')
state.callAction('entering', record)
expect(record.a).toHaveBeenCalled()
expect(record.b).toHaveBeenCalled()
expect(record.c).not.toHaveBeenCalled()
expect(record.error).toHaveBeenCalled()
it 'should handleError for chain of methods (excepts:halt_aasm_chain), if method provided as method', ->
@some = (error)-> @error(error)
state = new State('astate', {entering: ['a', 'b', 'c'],onError:@some})
record =
a: ->
b: ->
c: ->
error: ->
spyOn(record, 'a')
spyOn(record, 'b').andThrow('some other Error')
spyOn(record, 'c')
spyOn(record, 'error')
state.callAction('entering', record)
expect(record.a).toHaveBeenCalled()
expect(record.b).toHaveBeenCalled()
expect(record.c).not.toHaveBeenCalled()
expect(record.error).toHaveBeenCalled()
it 'should handleError for single method call of methods (excepts:halt_aasm_chain), if method provided as method', ->
@some = (rec, error)-> @error(error)
state = new State('astate', {entering: 'a',onError:@some})
record =
a: ->
error: ->
spyOn(record, 'a').andThrow('some other Error')
spyOn(record, 'error')
state.callAction('entering', record)
expect(record.a).toHaveBeenCalled()
expect(record.error).toHaveBeenCalled()