-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from balena-io-modules/run-task
Add test utility to run a task
- Loading branch information
Showing
5 changed files
with
95 additions
and
19 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,57 @@ | ||
import { expect } from '~/test-utils'; | ||
import { Task } from '../task'; | ||
import { runTask } from './run-task'; | ||
|
||
describe('testing/run-task', () => { | ||
const plusOne = Task.of<number>().from({ | ||
// This means the task can only be triggered | ||
// if the system state is below the target | ||
condition: (state, { target }) => state < target, | ||
// The effect of the action is increasing the system | ||
// counter by 1 | ||
effect: (state) => ++state._, | ||
// An optional description. Useful for testing | ||
description: '+1', | ||
}); | ||
|
||
const plusTwo = Task.of<number>().from({ | ||
condition: (state, { target }) => target - state > 1, | ||
method: (_, { target }) => [plusOne({ target }), plusOne({ target })], | ||
description: '+2', | ||
}); | ||
|
||
const plusThree = Task.of<number>().from({ | ||
condition: (state, { target }) => target - state > 2, | ||
method: (_, { target }) => [plusTwo({ target }), plusOne({ target })], | ||
description: '+3', | ||
}); | ||
|
||
const buggedPlusThree = Task.of<number>().from({ | ||
method: (_, { target }) => [plusTwo({ target }), plusOne({ target })], | ||
description: '+3', | ||
}); | ||
|
||
it('runs an action task if the condition is met', async () => { | ||
expect(await runTask(plusOne, 1, { target: 2 })).to.equal(2); | ||
expect(await runTask(plusOne, 0, { target: 2 })).to.equal(1); | ||
}); | ||
|
||
it('throws if the condition of an action task is not met', async () => { | ||
await expect(runTask(plusOne, 2, { target: 2 })).to.be.rejected; | ||
await expect(runTask(plusOne, 3, { target: 2 })).to.be.rejected; | ||
}); | ||
|
||
it('runs a method task by expanding its actions', async () => { | ||
expect(await runTask(plusTwo, 0, { target: 2 })).to.equal(2); | ||
expect(await runTask(plusTwo, 1, { target: 4 })).to.equal(3); | ||
expect(await runTask(plusThree, 1, { target: 4 })).to.equal(4); | ||
}); | ||
|
||
it('throws if a condition of a method task is not met', async () => { | ||
await expect(runTask(plusTwo, 2, { target: 2 })).to.be.rejected; | ||
await expect(runTask(plusTwo, 3, { target: 4 })).to.be.rejected; | ||
await expect(runTask(plusThree, 2, { target: 4 })).to.be.rejected; | ||
// the condition for the plusTwo call should fail here | ||
await expect(runTask(buggedPlusThree, 3, { target: 4 })).to.be.rejected; | ||
}); | ||
}); |
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,23 @@ | ||
import { zip } from './zip'; | ||
|
||
import type { Task, TaskOp, TaskArgs } from '../task'; | ||
import type { Root, PathType } from '../path'; | ||
|
||
/** | ||
* Run the task on a given state and context | ||
* | ||
* If the given task is a Method task, it expands the task first in | ||
* a sequential fashion and runs all the returned actions | ||
*/ | ||
export async function runTask< | ||
TState = unknown, | ||
TPath extends PathType = Root, | ||
TOp extends TaskOp = 'update', | ||
>( | ||
task: Task<TState, TPath, TOp>, | ||
state: TState, | ||
args: TaskArgs<TState, TPath, TOp>, | ||
): Promise<TState> { | ||
const doTask = zip(task(args)); | ||
return doTask(state); | ||
} |
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