Skip to content

Commit 40aa513

Browse files
grappigegovertRDIL
andauthored
Support actions with constants (#16)
* Add support for actions that reference constants * Add test with an action that references a constant --------- Signed-off-by: Reece Dunham <me@rdil.rocks> Co-authored-by: Reece Dunham <me@rdil.rocks>
1 parent 30a8eef commit 40aa513

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/handleEvent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ export function handleEvent<Context = unknown, Event = unknown>(
156156
},
157157
{
158158
...newContext,
159+
...(definition.Constants || {}),
159160
...(options.contractId && {
160161
ContractId: options.contractId,
161162
}),
162-
...(definition.Constants || {}),
163163
Value: event,
164164
},
165165
{

tests/handleEvent.data.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,29 @@
117117
}
118118
}
119119
},
120+
"actionWithConstants": {
121+
"Definition": {
122+
"Context": {
123+
"Variable": 0
124+
},
125+
"Constants": {
126+
"ValueToAdd": 500
127+
},
128+
"States": {
129+
"Start": {
130+
"Event": {
131+
"Actions": {
132+
"$inc": ["Variable", "$.ValueToAdd"]
133+
}
134+
}
135+
}
136+
}
137+
},
138+
"Input": {
139+
"Name": "Event",
140+
"Value": {}
141+
}
142+
},
120143
"pushUniqueAsCondition": {
121144
"Definition": {
122145
"Context": {

tests/handleEvent.spec.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe("handleEvent api", () => {
9696
assert.strictEqual(result.state, "Success", "Transition did not happen")
9797
})
9898

99-
it("works with constants", () => {
99+
it("supports tests with constants", () => {
100100
const { Definition, Input } = suites.testWithConstants
101101

102102
let state = "Start"
@@ -115,6 +115,25 @@ describe("handleEvent api", () => {
115115
assert.strictEqual(result.state, "Success", "Transition did not happen")
116116
})
117117

118+
it("supports actions with constants", () => {
119+
const { Definition, Input } = suites.actionWithConstants
120+
121+
let state = "Start"
122+
123+
const result = handleEvent(
124+
Definition,
125+
Definition.Context,
126+
Input.Value,
127+
{
128+
currentState: state,
129+
eventName: Input.Name,
130+
timestamp: 0,
131+
}
132+
)
133+
134+
assert.strictEqual(result.context["Variable"], 500)
135+
})
136+
118137
it("supports pushunique", () => {
119138
const { Definition, Input } = suites.pushUniqueAsCondition
120139
let context: Required<typeof Definition.Context> = Definition.Context

0 commit comments

Comments
 (0)