forked from cpatni/aasm-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate_transition.spec.coffee
103 lines (89 loc) · 3.15 KB
/
state_transition.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
StateTransition = require '../lib/state_transition'
describe 'StateTransition', ->
it 'should set from, to, and opts attr readers', ->
opts = {from: 'foo', to: 'bar', guard: 'g'}
st = new StateTransition(opts)
expect(st.from).toEqual(opts.from)
expect(st.to).toEqual(opts.to)
expect(st.opts).toEqual(opts)
it 'should pass equality check if from and to are the same', ->
opts = {from: 'foo', to: 'bar', guard: 'g'}
st = new StateTransition(opts)
obj = {from: opts.from, to: opts.to}
expect(st.equals(obj)).toBeTruthy()
it 'should fail equality check if from are not the same', ->
opts = {from: 'foo', to: 'bar', guard: 'g'}
st = new StateTransition(opts)
obj = {from: 'blah', to: opts.to}
expect(st.equals(obj)).toBeFalsy()
expect(st).toNotEqual(obj)
describe '- when performing guard checks', ->
it 'should return true of there is no guard', ->
opts = {from: 'foo', to: 'bar'}
st = new StateTransition(opts)
expect(st.perform(null)).toBeTruthy()
it 'should call the method on the object if guard is a string', ->
opts = {from: 'foo', to: 'bar', guard: 'test'}
st = new StateTransition(opts)
object = test: ->
spyOn(object, 'test')
st.perform(object)
expect(object.test).toHaveBeenCalled()
it 'should call the proc passing the object if the guard is a function', ->
opts = {from: 'foo', to: 'bar', guard: () -> @test() }
st = new StateTransition(opts)
obj = test: ->
spyOn(obj, 'test')
st.perform(obj)
expect(obj.test).toHaveBeenCalled()
describe 'Error handling', ->
it 'should execute each method is listed in onTransition',->
opts = {from: 'foo', to: 'bar', onTransition: ["test","a"] }
st = new StateTransition(opts)
obj =
test: ->
a: ->
spyOn(obj, 'test')
spyOn(obj, 'a')
st.execute(obj)
expect(obj.test).toHaveBeenCalled()
expect(obj.a).toHaveBeenCalled()
it 'should throw error if no handler is specified',->
opts = {from: 'foo', to: 'bar', onTransition: ["test","a"] }
st = new StateTransition(opts)
obj =
test: ->
a: ->
spyOn(obj, 'test').andThrow("some")
spyOn(obj, 'a')
expect(-> st.execute(obj)).toThrow("some")
expect(obj.test).toHaveBeenCalled()
expect(obj.a).not.toHaveBeenCalled()
it 'should call errorHandler if it is specified as string',->
opts = {from: 'foo', to: 'bar', onTransition: ["test","a"], onError:'error' }
st = new StateTransition(opts)
obj =
test: ->
a: ->
error: ->
spyOn(obj, 'test').andThrow("some")
spyOn(obj, 'a')
spyOn(obj, 'error')
expect(-> st.execute(obj)).not.toThrow("some")
expect(obj.test).toHaveBeenCalled()
expect(obj.a).not.toHaveBeenCalled()
expect(obj.error).toHaveBeenCalled()
it 'should call errorHandler if it is specified as method',->
opts = {from: 'foo', to: 'bar', onTransition: ["test","a"],onError:()-> @error() }
st = new StateTransition(opts)
obj =
test: ->
a: ->
error: ->
spyOn(obj, 'test').andThrow("some")
spyOn(obj, 'a')
spyOn(obj, 'error')
expect(-> st.execute(obj)).not.toThrow("some")
expect(obj.test).toHaveBeenCalled()
expect(obj.a).not.toHaveBeenCalled()
expect(obj.error).toHaveBeenCalled()