Skip to content

Commit b26fb64

Browse files
committed
feat: add execCmd testing function
mark runSingleOfPromiseCmd as deprecated
1 parent 3d2856c commit b26fb64

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

src/Testing/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Cmd } from "../Cmd";
33
/**
44
* Executes a single command created by one of the ofPromise functions.
55
* @param cmd The command to process.
6+
* @deprecated Use execCmd instead.
67
*/
78
export const runSingleOfPromiseCmd = async <TMsg>(cmd: Cmd<TMsg>): Promise<void> => {
89
return new Promise<void>((resolve) => {
@@ -25,4 +26,19 @@ export const getOfMsgParams = <TMsg>(cmd?: Cmd<TMsg>): TMsg [] => {
2526
cmd?.map(c => c(dispatch));
2627

2728
return msgNames;
29+
};
30+
31+
/**
32+
* Executes all commands and resolves the messages.
33+
* @param cmd The command to process.
34+
* @returns The array of processed messages.
35+
*/
36+
export const execCmd = <TMsg>(cmd: Cmd<TMsg>): Promise<TMsg []> => {
37+
const callers = cmd.map(c => new Promise<TMsg>((resolve) => {
38+
const dispatch = (msg: TMsg) => resolve(msg);
39+
40+
c(dispatch);
41+
}));
42+
43+
return Promise.all(callers);
2844
};

tests/Testing.spec.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)