-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
106 lines (91 loc) · 2.54 KB
/
index.test.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
import fc from "fast-check";
import runWithLog from "./index";
describe("test runWithLog", () => {
test("wrapped function should work same as original function", () => {
const none = () => {};
const mockFn = jest.fn(
(
a: string,
b: number,
c: bigint,
d: boolean,
e: undefined,
f: symbol,
g: null
) => [a, b, c, d, e, f, g]
);
fc.assert(
fc.property(
fc.string(),
fc.integer(),
fc.bigInt(),
fc.boolean(),
fc.constant(undefined),
fc.constant(Symbol()),
fc.constant(null),
(a, b, c, d, e, f, g) => {
const fn = runWithLog(none, mockFn);
const result = fn(a, b, c, d, e, f, g);
expect(mockFn).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledWith(a, b, c, d, e, f, g);
expect(result).toStrictEqual([a, b, c, d, e, f, g]);
mockFn.mockClear();
}
)
);
});
test("should print hello world after wrapped function be called", () => {
jest.spyOn(console, "log");
const mockFn = jest.fn();
const fn = runWithLog(() => "hello world", mockFn);
expect(console.log).toHaveBeenCalledTimes(0);
fn();
expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith("hello world");
});
test("logger should get arguments and result", () => {
const loggerFn = jest.fn(console.log);
const mockFn = jest.fn(
(
a: string,
b: number,
c: bigint,
d: boolean,
e: undefined,
f: symbol,
g: null
) => [a, b, c, d, e, f, g]
);
fc.assert(
fc.property(
fc.string(),
fc.integer(),
fc.bigInt(),
fc.boolean(),
fc.constant(undefined),
fc.constant(Symbol()),
fc.constant(null),
(a, b, c, d, e, f, g) => {
const fn = runWithLog(loggerFn, mockFn);
const result = fn(a, b, c, d, e, f, g);
expect(loggerFn)
//
.toHaveBeenCalledWith([a, b, c, d, e, f, g], result);
loggerFn.mockClear();
mockFn.mockClear();
}
)
);
});
test("usecase: function add with log", () => {
jest.spyOn(console, "log");
const add = (a: number, b: number) => a + b;
const addWithLog = runWithLog(
([x, y], result) => `${x} + ${y} = ${result}`,
add
);
const result = addWithLog(1, 1);
expect(result).toBe(2);
expect(console.log).toHaveBeenCalledWith("1 + 1 = 2");
});
});