forked from yoshuawuyts/fsm-event
/
test.js
121 lines (93 loc) · 1.84 KB
/
test.js
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
110
111
112
113
114
115
116
117
118
119
120
121
const noop = require('noop2')
const test = require('tape')
const fsm = require('./')
noop()
test('should validate input states', function (t) {
t.plan(2)
const state1 = {
UP: {down: 'DOWN'},
DOWN: {up: 'UP'}
}
const state2 = {
UP: {down: 'DOWN'},
DOWN: {up: 'END'}
}
t.doesNotThrow(fsm.bind(null, state1))
t.throws(fsm.bind(null, state2), /state/)
})
test('m.on() should attach events', function (t) {
t.plan(1)
const m = fsm({
UP: {down: 'DOWN'},
DOWN: {up: 'UP'}
})
m.on('UP', noop)
t.equal(typeof m._emitter._events.UP, 'function')
})
test('m.emit() should catch invalid state transitions', function (t) {
t.plan(2)
const m = fsm({
UP: {down: 'DOWN'},
DOWN: {up: 'UP'}
})
m('UP')
t.equal(m._state, 'UP')
t.throws(m.bind(null, 'END'))
})
test('m.emit() should set the state', function (t) {
t.plan(3)
const m = fsm({
UP: {down: 'DOWN'},
DOWN: {up: 'UP'}
})
m.on('UP', function () {
t.pass('cb called')
})
t.equal(m._state, null)
m('UP')
t.equal(m._state, 'UP')
})
test('m.emit() should emit enter events', function (t) {
t.plan(2)
const m = fsm({
UP: {down: 'DOWN'},
DOWN: {up: 'UP'}
})
m.on('UP:enter', function (cb) {
t.pass('enter')
cb()
})
m.on('UP', function () {
t.pass('UP')
})
m.emit('UP')
m.emit('DOWN')
})
test('m.emit() should emit events in sequence', function (t) {
t.plan(5)
var i = 0
const m = fsm({
UP: {down: 'DOWN'},
DOWN: {up: 'UP'}
})
m.on('UP:enter', function (cb) {
t.equal(++i, 1)
cb()
})
m.on('UP', function () {
t.equal(++i, 2)
})
m.on('UP:leave', function (cb) {
t.equal(++i, 3)
cb()
})
m.on('DOWN:enter', function (cb) {
t.equal(++i, 4)
cb()
})
m.on('DOWN', function () {
t.equal(++i, 5)
})
m.emit('UP')
m.emit('DOWN')
})