-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathmouse-movement.function.spec.ts
80 lines (65 loc) · 2.58 KB
/
mouse-movement.function.spec.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
import {
calculateStepDuration,
linear,
calculateMovementTimesteps, EasingFunction
} from "./mouse-movement.function";
describe("MovementType", () => {
describe("baseStepDuration", () => {
it("should calculate the base step duration in nanoseconds", () => {
// GIVEN
const speedInPixelsPerSecond = 1000;
const expectedBaseStepDuration = 1_000_000;
// WHEN
const result = calculateStepDuration(speedInPixelsPerSecond);
// THEN
expect(result).toBe(expectedBaseStepDuration);
});
});
describe("stepDuration", () => {
it("should call easing function progress to calculate current step duration", () => {
// GIVEN
const amountOfSteps = 100;
const speedInPixelsPerSecond = 1000;
const easingFunction = jest.fn(() => 0);
// WHEN
calculateMovementTimesteps(amountOfSteps, speedInPixelsPerSecond, easingFunction);
// THEN
expect(easingFunction).toBeCalledTimes(amountOfSteps);
})
});
describe('linear', () => {
it("should return a set of linear timesteps, 1000000 nanosecond per step.", () => {
// GIVEN
const expected = [1000000, 1000000, 1000000, 1000000, 1000000, 1000000];
// WHEN
const result = calculateMovementTimesteps(6, 1000, linear);
// THEN
expect(result).toEqual(expected);
});
it("should should return a set of linear timesteps, 2000000 nanoseconds per step.", () => {
// GIVEN
const expected = [2000000, 2000000, 2000000, 2000000, 2000000, 2000000];
// WHEN
const result = calculateMovementTimesteps(6, 500, linear);
// THEN
expect(result).toEqual(expected);
});
});
describe('non-linear', () => {
it("should return progress slowly in the first half, 2000000 nanoseconds per step, then continue with normal speed, 1000000 nanoseconds per step", () => {
// GIVEN
const mouseSpeed = 1000;
const easingFunction: EasingFunction = (p: number) => {
if (p < 0.5) {
return -0.5;
}
return 0;
};
const expected = [2000000, 2000000, 2000000, 1000000, 1000000, 1000000];
// WHEN
const result = calculateMovementTimesteps(6, mouseSpeed, easingFunction);
// THEN
expect(result).toEqual(expected);
});
});
});