From 25bca132659896dc1779fd55300d7725f3786fc0 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Fri, 20 Aug 2021 11:01:53 +0300 Subject: [PATCH] chore: add test that reused one builder --- fsm.test.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/fsm.test.ts b/fsm.test.ts index 7ecfa60..6e2a2ef 100644 --- a/fsm.test.ts +++ b/fsm.test.ts @@ -160,3 +160,27 @@ Deno.test("should throw error if beforeExit action returns false", () => { `cannot change state from "${ProjectStatus.Pending}" to "${ProjectStatus.Active}"`, ); }); + +Deno.test("should reuse one builder for many entities", () => { + const statuses = Object.values(ProjectStatus); + const transitions: Array<[string, Array]> = [ + [ProjectStatus.Pending, [ProjectStatus.Active, ProjectStatus.Archived]], + [ProjectStatus.Active, [ProjectStatus.Completed]], + ]; + + const smb = new fsm.StateMachineBuilder() + .withStates(statuses) + .withTransitions(transitions); + + function expectedAllowedStates(status: ProjectStatus) { + return transitions.find(([s]) => s === status)?.[1] || []; + } + + for (const status of statuses) { + const sm = smb.build(status); + assertEquals( + sm.allowedTransitionStates().map(String), + expectedAllowedStates(status), + ); + } +});