@@ -22,6 +22,51 @@ const {
22
22
jsCodeWithCommentedCall,
23
23
} = jsTestValues ;
24
24
25
+ describe ( "RandomMocker" , ( ) => {
26
+ let random : ( ) => number ;
27
+
28
+ beforeEach ( ( ) => {
29
+ random = Math . random ;
30
+ } ) ;
31
+
32
+ afterEach ( ( ) => {
33
+ Math . random = random ;
34
+ } ) ;
35
+
36
+ describe ( "mock" , ( ) => {
37
+ it ( 'should replace "Math.random" with a mock function' , ( ) => {
38
+ const mocker = new helper . RandomMocker ( ) ;
39
+ mocker . mock ( ) ;
40
+ expect ( Math . random ) . not . toBe ( random ) ;
41
+ } ) ;
42
+
43
+ it ( 'should mock "Math.random" with a pseudorandom function' , ( ) => {
44
+ const mocker = new helper . RandomMocker ( ) ;
45
+ mocker . mock ( ) ;
46
+ // Predictable random values:
47
+ expect ( Math . random ( ) ) . toBe ( 0.2523451747838408 ) ;
48
+ expect ( Math . random ( ) ) . toBe ( 0.08812504541128874 ) ;
49
+ } ) ;
50
+
51
+ it ( "should reset the pseudorandom function when called multiple times" , ( ) => {
52
+ const mocker = new helper . RandomMocker ( ) ;
53
+ mocker . mock ( ) ;
54
+ expect ( Math . random ( ) ) . toBe ( 0.2523451747838408 ) ;
55
+ mocker . mock ( ) ;
56
+ expect ( Math . random ( ) ) . toBe ( 0.2523451747838408 ) ;
57
+ } ) ;
58
+ } ) ;
59
+
60
+ describe ( "restore" , ( ) => {
61
+ it ( 'should restore "Math.random" to its original function' , ( ) => {
62
+ const mocker = new helper . RandomMocker ( ) ;
63
+ mocker . mock ( ) ;
64
+ mocker . restore ( ) ;
65
+ expect ( Math . random ) . toBe ( random ) ;
66
+ } ) ;
67
+ } ) ;
68
+ } ) ;
69
+
25
70
describe ( "removeWhiteSpace" , ( ) => {
26
71
const { removeWhiteSpace } = helper ;
27
72
it ( "returns a string" , ( ) => {
0 commit comments