-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6189b52
commit 144ddd8
Showing
4 changed files
with
145 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,121 +1,126 @@ | ||
const noop = require('noop2') | ||
const test = require('tape') | ||
const fsm = require('./') | ||
|
||
noop() | ||
test('should validate input values', function (t) { | ||
t.plan(4) | ||
|
||
test('should validate input states', function (t) { | ||
t.plan(2) | ||
t.throws(fsm.bind(null, 123, 123), /string/) | ||
t.throws(fsm.bind(null, 'UP', 123), /object/) | ||
|
||
const state1 = { | ||
UP: {down: 'DOWN'}, | ||
DOWN: {up: 'UP'} | ||
} | ||
|
||
t.doesNotThrow(fsm.bind(null, 'UP', state1)) | ||
|
||
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') | ||
t.throws(fsm.bind(null, 'UP', state2), /state/) | ||
}) | ||
|
||
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') | ||
}) | ||
// test('m.on() should attach events', function (t) { | ||
// t.plan(2) | ||
|
||
// const m = fsm('UP', { | ||
// UP: {down: 'DOWN'}, | ||
// DOWN: {up: 'UP'} | ||
// }) | ||
|
||
// m.on('UP', function () { | ||
// t.pass('cb called') | ||
// }) | ||
|
||
// t.equal(typeof m._emitter._events.UP, 'function') | ||
// }) | ||
|
||
// test('m.emit() should catch invalid state transitions', function (t) { | ||
// t.plan(3) | ||
|
||
// const m = fsm('UP', { | ||
// UP: {down: 'DOWN'}, | ||
// DOWN: {up: 'UP'} | ||
// }) | ||
|
||
// t.equal(m._state, 'UP') | ||
// m('down') | ||
// t.equal(m._state, 'DOWN') | ||
// t.throws(m.bind(null, 'END')) | ||
// }) | ||
|
||
// test('m.emit() should set the state', function (t) { | ||
// t.plan(4) | ||
|
||
// const m = fsm('UP', { | ||
// UP: {down: 'DOWN'}, | ||
// DOWN: {up: 'UP'} | ||
// }) | ||
|
||
// m.on('DOWN', function () { | ||
// t.pass('cb called') | ||
// }) | ||
|
||
// t.equal(m._state, 'UP') | ||
// m('down') | ||
// t.equal(m._state, 'DOWN') | ||
// }) | ||
|
||
// test('m.emit() should emit enter events', function (t) { | ||
// t.plan(2) | ||
|
||
// const m = fsm('UP', { | ||
// 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', { | ||
// 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') | ||
// }) |