1+ import {
2+ calculateStepDuration ,
3+ linear ,
4+ calculateMovementTimesteps , EasingFunction
5+ } from "./mouse-movement.function" ;
6+
7+ describe ( "MovementType" , ( ) => {
8+ describe ( "baseStepDuration" , ( ) => {
9+ it ( "should calculate the base step duration in nanoseconds" , ( ) => {
10+ // GIVEN
11+ const speedInPixelsPerSecond = 1000 ;
12+ const expectedBaseStepDuration = 1_000_000 ;
13+
14+ // WHEN
15+ const result = calculateStepDuration ( speedInPixelsPerSecond ) ;
16+
17+ // THEN
18+ expect ( result ) . toBe ( expectedBaseStepDuration ) ;
19+ } ) ;
20+ } ) ;
21+
22+ describe ( "stepDuration" , ( ) => {
23+ it ( "should call easing function progress to calculate current step duration" , ( ) => {
24+ // GIVEN
25+ const amountOfSteps = 100 ;
26+ const speedInPixelsPerSecond = 1000 ;
27+ const easingFunction = jest . fn ( ( ) => 0 ) ;
28+
29+ // WHEN
30+ calculateMovementTimesteps ( amountOfSteps , speedInPixelsPerSecond , easingFunction ) ;
31+
32+ // THEN
33+ expect ( easingFunction ) . toBeCalledTimes ( amountOfSteps ) ;
34+ } )
35+ } ) ;
36+
37+ describe ( 'linear' , ( ) => {
38+ it ( "should return a set of linear timesteps, 1000000 nanosecond per step." , ( ) => {
39+ // GIVEN
40+ const expected = [ 1000000 , 1000000 , 1000000 , 1000000 , 1000000 , 1000000 ] ;
41+
42+ // WHEN
43+ const result = calculateMovementTimesteps ( 6 , 1000 , linear ) ;
44+
45+ // THEN
46+ expect ( result ) . toEqual ( expected ) ;
47+ } ) ;
48+
49+ it ( "should should return a set of linear timesteps, 2000000 nanoseconds per step." , ( ) => {
50+ // GIVEN
51+ const expected = [ 2000000 , 2000000 , 2000000 , 2000000 , 2000000 , 2000000 ] ;
52+
53+ // WHEN
54+ const result = calculateMovementTimesteps ( 6 , 500 , linear ) ;
55+
56+ // THEN
57+ expect ( result ) . toEqual ( expected ) ;
58+ } ) ;
59+ } ) ;
60+
61+ describe ( 'non-linear' , ( ) => {
62+ it ( "should return progress slowly in the first half, 2000000 nanoseconds per step, then continue with normal speed, 1000000 nanoseconds per step" , ( ) => {
63+ // GIVEN
64+ const mouseSpeed = 1000 ;
65+ const easingFunction : EasingFunction = ( p : number ) => {
66+ if ( p < 0.5 ) {
67+ return - 0.5 * mouseSpeed ;
68+ }
69+ return 0 ;
70+ } ;
71+ const expected = [ 2000000 , 2000000 , 2000000 , 1000000 , 1000000 , 1000000 ] ;
72+
73+ // WHEN
74+ const result = calculateMovementTimesteps ( 6 , mouseSpeed , easingFunction ) ;
75+
76+ // THEN
77+ expect ( result ) . toEqual ( expected ) ;
78+ } ) ;
79+ } ) ;
80+ } ) ;
0 commit comments