@@ -10,21 +10,42 @@ describe('randomShipRegistryNumber',() => {
1010 expect ( randomShipRegistryNumber ( ) ) . not . toEqual ( randomShipRegistryNumber ( ) )
1111 } ) ;
1212} ) ;
13+ function loadDie ( ...values ) {
14+ const originalRandom = Math . random ( )
15+
16+ Math . random = function loadedDie {
17+ if ( values . length === 0 ) {
18+ return originalRandom ( ) ;
19+ }
20+ return values . shift ( ) ;
21+ }
22+ return ( ) => {
23+ Math . random = originalRandom ;
24+ }
25+ }
1326describe ( 'randomStardate' , ( ) => {
14- test ( 'stardates are valid' , ( ) => {
15- for ( let i = 0 ; i < 4 ; i ++ ) {
16- expect ( randomStardate ( ) >= 41000 && randomStardate ( ) < 42000 ) . toBe ( true ) ;
27+ test ( 'stardate is between 41000 and 42000' , ( ) => {
28+ const restore = loadDie (
29+ 0 , 0 , 0 , 0 , 0 , 0 ,
30+ 1 , 1 , 1 , 1 , 1 , 1 ,
31+ 0.5 , 0.5 , 0.5 , 0.5 , 0.5 , 0.5
32+ ) ;
33+
34+ // 6 * 0, 6 * 1, 6 * 0.5 and everything else random
35+
36+ for ( let i = 0 ; i < 10_000 ; i ++ ) {
37+ const starDate = randomStardate ( )
38+ expect ( starDate ) . toBeGreaterThanOrEqual ( 41_000 ) ;
39+ expect ( starDate ) . toBeLessThan ( 42_000 ) ;
1740 }
18- } ) ;
19- test ( 'returns a random stardate' , ( ) => {
20- expect ( randomStardate ( ) ) . not . toEqual ( randomStardate ( ) )
21- } ) ;
22- } ) ;
41+ restore ( ) ;
42+ }
43+ }
2344describe ( 'randomPlanetClass' , ( ) => {
2445 test ( 'planet classes are valid' , ( ) => {
2546 const planetClasses = [ 'D' , 'H' , 'J' , 'K' , 'L' , 'M' , 'N' , 'R' , 'T' , 'Y' ] ;
2647 for ( let i = 0 ; i < 4 ; i ++ ) {
27- expect ( planetClasses . includes ( randomPlanetClass ( ) ) ) . toBe ( true )
48+ expect ( planetClasses ) . toContain ( randomPlanetClass ( ) )
2849 }
2950 } ) ;
3051 test ( 'returns a random planet class' , ( ) => {
0 commit comments