Skip to content

Commit 8ce2878

Browse files
committed
[@dhealthdapps/backend] test: update unit tests
1 parent f3fd718 commit 8ce2878

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

runtime/backend/tests/unit/payout/services/MathService.spec.ts

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,30 @@ describe("payout/MathService", () => {
1616

1717
beforeEach(() => {
1818
mathService = new MathService();
19+
});
20+
21+
afterEach(() => {
22+
jest.clearAllMocks()
1923
})
2024

2125
describe("skewNormal()", () => {
2226
it("should return correct result if skewness is not defined", () => {
23-
// prepare
24-
const [mean, deviation] = [1, 2];
25-
const getRandomVariatesCall = jest
26-
.spyOn((mathService as any), "getRandomVariates")
27-
.mockReturnValue([1,1]);
27+
const expectedResullts = [3, -4];
28+
[1, -2.5].forEach((u0: number, index: number) => {
29+
// prepare
30+
jest.clearAllMocks();
31+
const [mean, deviation] = [1, 2];
32+
const getRandomVariatesCall = jest
33+
.spyOn((mathService as any), "getRandomVariates")
34+
.mockReturnValue([u0, 1]);
2835

29-
// act
30-
const result = mathService.skewNormal(mean, deviation);
36+
// act
37+
const result = mathService.skewNormal(mean, deviation);
3138

32-
// assert
33-
expect(getRandomVariatesCall).toHaveBeenCalledTimes(1);
34-
expect(result).toBe(3);
39+
// assert
40+
expect(getRandomVariatesCall).toHaveBeenCalledTimes(1);
41+
expect(result).toBe(expectedResullts[index]);
42+
});
3543
});
3644

3745
it("should return correct result if skewness is defined", () => {
@@ -54,5 +62,30 @@ describe("payout/MathService", () => {
5462
expect(mathSqrtCall).toHaveBeenNthCalledWith(2, 0);
5563
expect(result).toBe(7);
5664
});
65+
66+
it("should return correct result if first random variate number is negative", () => {
67+
const expectedResullts = [0, 3];
68+
[-1, -2].forEach((u0: number, index: number) => {
69+
// prepare
70+
jest.clearAllMocks();
71+
const [mean, deviation, skewness] = [1, 2, 3];
72+
const getRandomVariatesCall = jest
73+
.spyOn((mathService as any), "getRandomVariates")
74+
.mockReturnValue([u0, 1]);
75+
const mathSqrtCall = jest
76+
.spyOn(Math, "sqrt")
77+
.mockReturnValue(2);
78+
79+
// act
80+
const result = mathService.skewNormal(mean, deviation, skewness);
81+
82+
// assert
83+
expect(getRandomVariatesCall).toHaveBeenCalledTimes(1);
84+
expect(mathSqrtCall).toHaveBeenCalledTimes(2);
85+
expect(mathSqrtCall).toHaveBeenNthCalledWith(1, 1 + skewness * skewness);
86+
expect(mathSqrtCall).toHaveBeenNthCalledWith(2, 1 - (skewness / 2) * (skewness/ 2));
87+
expect(result).toBe(expectedResullts[index]);
88+
});
89+
});
5790
});
5891
});

0 commit comments

Comments
 (0)