1+ import { execCmd , runSingleOfPromiseCmd } from "../src/Testing" ;
2+ import { createCmd } from "../src/Cmd" ;
3+
4+ type Message =
5+ | { name : "Msg1" }
6+ | { name : "Msg2" }
7+ | { name : "Error" }
8+ ;
9+
10+ const cmd = createCmd < Message > ( ) ;
11+
12+ describe ( "Testing" , ( ) => {
13+ describe ( "execCmd" , ( ) => {
14+ it ( "executes all message commands" , async ( ) => {
15+ // arrange
16+ const cmds = cmd . batch ( cmd . ofMsg ( { name : "Msg1" } ) , cmd . ofMsg ( { name : "Msg2" } ) ) ;
17+
18+ // act
19+ const messages = await execCmd ( cmds ) ;
20+
21+ // assert
22+ expect ( messages ) . toEqual ( [ { name : "Msg1" } , { name : "Msg2" } ] ) ;
23+ } ) ;
24+
25+ it ( "executes all ofFunc commands" , async ( ) => {
26+ // arrange
27+ const func = ( ) : void => {
28+ return ;
29+ } ;
30+
31+ const cmds = cmd . batch ( cmd . ofFunc . either ( func , ( ) => ( { name : "Msg1" } ) , ( ) => ( { name : "Error" } ) ) , cmd . ofMsg ( { name : "Msg2" } ) ) ;
32+
33+ // act
34+ const messages = await execCmd ( cmds ) ;
35+
36+ // assert
37+ expect ( messages ) . toEqual ( [ { name : "Msg1" } , { name : "Msg2" } ] ) ;
38+ } ) ;
39+
40+ it ( "executes all ofFunc commands, fail" , async ( ) => {
41+ // arrange
42+ const func = ( ) : void => {
43+ throw Error ( ) ;
44+ } ;
45+
46+ const cmds = cmd . batch ( cmd . ofFunc . either ( func , ( ) => ( { name : "Msg1" } ) , ( ) => ( { name : "Error" } ) ) , cmd . ofMsg ( { name : "Msg2" } ) ) ;
47+
48+ // act
49+ const messages = await execCmd ( cmds ) ;
50+
51+ // assert
52+ expect ( messages ) . toEqual ( [ { name : "Error" } , { name : "Msg2" } ] ) ;
53+ } ) ;
54+
55+ it ( "executes all ofPromise commands" , async ( ) => {
56+ // arrange
57+ const asyncFunc = ( ) : Promise < void > => {
58+ return Promise . resolve ( ) ;
59+ } ;
60+
61+ const cmds = cmd . batch ( cmd . ofPromise . either ( asyncFunc , ( ) => ( { name : "Msg1" } ) , ( ) => ( { name : "Error" } ) ) , cmd . ofMsg ( { name : "Msg2" } ) ) ;
62+
63+ // act
64+ const messages = await execCmd ( cmds ) ;
65+
66+ // assert
67+ expect ( messages ) . toEqual ( [ { name : "Msg1" } , { name : "Msg2" } ] ) ;
68+ } ) ;
69+
70+ it ( "executes all ofPromise commands, fail" , async ( ) => {
71+ // arrange
72+ const asyncFunc = ( ) : Promise < void > => {
73+ return Promise . reject ( ) ;
74+ } ;
75+
76+ const cmds = cmd . batch ( cmd . ofPromise . either ( asyncFunc , ( ) => ( { name : "Msg1" } ) , ( ) => ( { name : "Error" } ) ) , cmd . ofMsg ( { name : "Msg2" } ) ) ;
77+
78+ // act
79+ const messages = await execCmd ( cmds ) ;
80+
81+ // assert
82+ expect ( messages ) . toEqual ( [ { name : "Error" } , { name : "Msg2" } ] ) ;
83+ } ) ;
84+ } ) ;
85+ } ) ;
0 commit comments