forked from marco-ippolito/fiume
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
3 changed files
with
72 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import assert from "node:assert"; | ||
import test from "node:test"; | ||
import { StateMachine } from "../dist/index.js"; | ||
|
||
test("#171 - event is available on 'subscribe' callback", async () => { | ||
const subscriptionEvent = []; | ||
|
||
const states = [ | ||
{ | ||
id: "OFF", | ||
initial: true, | ||
transitionTo: () => "ON", | ||
}, | ||
{ | ||
id: "ON", | ||
transitionTo: () => "OFF", | ||
}, | ||
]; | ||
|
||
const context = {}; | ||
const sharedData = {}; | ||
const machine = StateMachine.from(states, { | ||
context, | ||
sharedData, | ||
}); | ||
|
||
machine.subscribe((event) => { | ||
subscriptionEvent.push(event); | ||
}); | ||
|
||
// Start the state machine | ||
await machine.start(); | ||
|
||
const event1 = { name: "foo", value: 1 }; | ||
await machine.send(event1); | ||
const event2 = { name: "foo", value: 2 }; | ||
await machine.send(event2); | ||
|
||
assert.deepStrictEqual(subscriptionEvent, [ | ||
{ | ||
context, | ||
currentStateId: "OFF", | ||
event: undefined, | ||
sharedData, | ||
}, | ||
{ | ||
context, | ||
currentStateId: "ON", | ||
event: event1, | ||
sharedData, | ||
}, | ||
{ | ||
context, | ||
currentStateId: "OFF", | ||
event: event2, | ||
sharedData, | ||
}, | ||
]); | ||
}); |