Skip to content

Commit

Permalink
Add type checking to some actions (#29)
Browse files Browse the repository at this point in the history
* rearrange statements for consistency

* Add tests for actions trying to modify undefined variables

* Fix running actions with wrong types to do nothing instead of breaking

* fix-fix: actually check the type of the right variable in $remove

* Also add tests for $inc/$dec undefined with 1 operand

Gotta reach those coverage targets :^)
  • Loading branch information
grappigegovert authored Jun 23, 2024
1 parent 093944d commit bbf3684
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/handleEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ export function handleEvent<Context = unknown, Event = unknown>(
`Running pushUniqueAction on ${reference} with ${item}`
)

if (!referenceArray) {
throw new Error(
`Could not find ${reference} in context`
)
if (!Array.isArray(referenceArray)) {
return false
}

if (referenceArray.includes(item)) {
Expand Down
22 changes: 21 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,13 @@ export function handleActions<Context>(

const addOrDec = (op: string) => {
if (typeof input[op] === "string") {
let reference = input[op]

const variableValue = findNamedChild(input[op], context, true)

let reference = input[op]
if (typeof variableValue !== "number") {
return
}

set(
context,
Expand All @@ -325,6 +329,10 @@ export function handleActions<Context>(
const variableValue = findNamedChild(reference, context, true)
const incrementBy = findNamedChild(input[op][1], context, false)

if (typeof variableValue !== "number") {
return
}

set(
context,
reference,
Expand All @@ -348,6 +356,10 @@ export function handleActions<Context>(
// clone the thing
const array = deepClone(findNamedChild(reference, context, true))

if (!Array.isArray(array)) {
return
}

if (unique) {
if (array.indexOf(value) === -1) {
array.push(value)
Expand Down Expand Up @@ -388,6 +400,10 @@ export function handleActions<Context>(
false
)

if (typeof variableValue1 !== "number" || typeof variableValue2 !== "number") {
break
}

set(context, reference, variableValue1 * variableValue2)
break
}
Expand Down Expand Up @@ -421,6 +437,10 @@ export function handleActions<Context>(
findNamedChild(reference, context, true)
)

if (!Array.isArray(array)) {
break
}

array = array.filter((item) => item !== value)

set(context, reference, array)
Expand Down
109 changes: 109 additions & 0 deletions tests/edge-cases.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { findNamedChild, set } from "../src/utils"
import { handleActions, handleEvent } from "../src"
import * as assert from "assert"

describe("edge cases", () => {
Expand Down Expand Up @@ -43,4 +44,112 @@ describe("edge cases", () => {
)
})
})

describe("trying to modify non-existant context vars does not error", () => {
it("using the $inc action", () => {
const action1 = {
$inc: ["MyCoolArray", 1],
}
const action2 = {
$inc: "MyCoolArray"
}
const context = {}

let newContext = handleActions(action1, context)
newContext = handleActions(action2, newContext)

assert.deepStrictEqual(newContext, {})
})

it("using the $dec action", () => {
const action1 = {
$dec: ["MyCoolArray", 2],
}
const action2 = {
$dec: "MyCoolArray",
}
const context = {}

let newContext = handleActions(action1, context)
newContext = handleActions(action2, newContext)

assert.deepStrictEqual(newContext, {})
})

it("using the $mul action", () => {
const action = {
$mul: ["MyCoolArray", 3],
}
const context = {}

const newContext = handleActions(action, context)

assert.deepStrictEqual(newContext, {})
})

it("using the $push action", () => {
const action = {
$push: ["MyCoolArray", 4],
}
const context = {}

const newContext = handleActions(action, context)

assert.deepStrictEqual(newContext, {})
})

it("using the $pushunique action", () => {
const action = {
$pushunique: ["MyCoolArray", 5],
}
const context = {}

const newContext = handleActions(action, context)

assert.deepStrictEqual(newContext, {})
})

it("using the $remove action", () => {
const action = {
$remove: ["MyCoolArray", 6],
}
const context = {}

const newContext = handleActions(action, context)

assert.deepStrictEqual(newContext, {})
})

it("using the $pushunique condition", () => {
const Definition = {
Context: {},
States: {
Start: {
EventName: {
Condition: {
$pushunique: ["MyOtherCoolArray", "bofa"],
},
},
},
},
}
const Input = {
Name: "EventName",
Value: {},
}

const result = handleEvent(
Definition,
Definition.Context,
Input.Value,
{
currentState: "Start",
eventName: Input.Name,
timestamp: 0,
},
)

assert.deepStrictEqual(result.context, {})
})
})
})

0 comments on commit bbf3684

Please sign in to comment.