forked from fraktalio/fmodel-deno-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_specification.ts
120 lines (115 loc) · 3.23 KB
/
test_specification.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { assert, assertEquals } from "std/assert/mod.ts";
import type { IDecider, IView } from "fmodel";
export type DeciderSpecification<C, E> = {
given: (events: E[]) => {
when: (command: C) => {
then: (expectedEvents: E[]) => void;
thenThrows: (assertion: (error: Error) => boolean) => void;
};
};
};
/**
* A test specification for deciders.
*
* @param decider - The decider of type `IDecider<C, S, E>` to test.
*
* @typeParam C - The type of the command.
* @typeParam S - The type of the state.
* @typeParam E - The type of the event.
*
* @example
* ```ts
* DeciderSpecification.for(restaurantDecider)
* .given([restaurantCreatedEvent])
* .when(changeRestaurantMenuCommand)
* .then([restaurantMenuChangedEvent]);
* ```
*/
export const DeciderSpecification = {
for: <C, S, E>(
decider: IDecider<C, S, E>,
): DeciderSpecification<C, E> => {
return {
given: (events: E[]) => {
return {
when: (command: C) => {
const handle = () => {
const currentState = events.reduce<S>(
decider.evolve,
decider.initialState,
);
return decider.decide(command, currentState);
};
return {
// biome-ignore lint/suspicious/noThenProperty: <explanation>
then: (expectedEvents: E[]) => {
const resultEvents = handle();
assertEquals(resultEvents, expectedEvents);
},
thenThrows: (check?: (error: Error) => boolean) => {
try {
handle();
throw new Error("Handler did not fail as expected");
} catch (error) {
if (check) assert(check(error as Error) === true);
}
},
};
},
};
},
};
},
};
export type ViewSpecification<S, E> = {
given: (events: E[]) => {
then: (expectedState: S) => void;
thenThrows: (assertion: (error: Error) => boolean) => void;
};
};
/**
* A test specification for views.
*
* @param view - The view of type `IView`<S, E>` to test.
*
* @typeParam S - The type of the state.
* @typeParam E - The type of the event.
*
* @example
* ```ts
* ViewSpecification.for(restaurantView)
* .given([restaurantCreatedEvent, restaurantMenuChangedEvent])
* .then(restaurantState);
* ```
*/
export const ViewSpecification = {
for: <S, E>(
view: IView<S, E>,
): ViewSpecification<S, E> => {
return {
given: (events: E[]) => {
const handle = () => {
return events.reduce<S>(
view.evolve,
view.initialState,
);
};
return {
// biome-ignore lint/suspicious/noThenProperty: <explanation>
then: (expectedState: S) => {
const resultState = handle();
assertEquals(resultState, expectedState);
},
thenThrows: (check?: (error: Error) => boolean) => {
try {
handle();
throw new Error("Handler did not fail as expected");
} catch (error) {
if (check) assert(check(error as Error) === true);
}
},
};
},
};
},
};