8
8
} = require ( '../../src/utils' ) ;
9
9
const { expect } = require ( 'chai' ) ;
10
10
const sinon = require ( 'sinon' ) ;
11
- const { MongoInvalidArgumentError } = require ( '../../src/error' ) ;
11
+ const { MongoRuntimeError } = require ( '../../src/error' ) ;
12
12
13
13
describe ( 'utils' , function ( ) {
14
14
context ( 'eachAsync' , function ( ) {
@@ -466,13 +466,34 @@ describe('utils', function () {
466
466
const output = shuffle ( input ) ;
467
467
expect ( Array . isArray ( output ) ) . to . be . true ;
468
468
} ) ;
469
+
470
+ it ( 'should not mutate the original input' , function ( ) {
471
+ const input = Object . freeze ( [ 'a' , 'b' , 'c' , 'd' , 'e' ] ) ;
472
+ const output = shuffle ( input ) ; // This will throw if shuffle tries to edit the input
473
+ expect ( output ) . to . not . deep . equal ( input ) ;
474
+ expect ( output ) . to . have . lengthOf ( input . length ) ;
475
+ } ) ;
476
+
469
477
it ( 'should give a random subset if limit is less than the input length' , ( ) => {
470
478
const input = [ 'a' , 'b' , 'c' , 'd' , 'e' ] ;
471
479
const output = shuffle ( input , 3 ) ;
472
480
expect ( output ) . to . have . lengthOf ( 3 ) ;
473
481
} ) ;
474
482
475
- it ( 'should give a random shuffling of the input' , ( ) => {
483
+ const input = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' ] ;
484
+ for ( let n = 1 ; n <= input . length ; n ++ ) {
485
+ it ( `should give a random subset of length ${ n } ` , function ( ) {
486
+ const output = shuffle ( input , n ) ;
487
+ if ( n > 2 ) {
488
+ // This expectation fails more at n values below 3
489
+ // sparing us the flake
490
+ expect ( output ) . to . not . deep . equal ( input . slice ( 0 , n ) ) ;
491
+ }
492
+ expect ( output ) . to . have . lengthOf ( n ) ;
493
+ } ) ;
494
+ }
495
+
496
+ it ( 'should give a random shuffling of the entire input when no limit provided' , ( ) => {
476
497
const input = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' ] ;
477
498
const output = shuffle ( input ) ;
478
499
// Of course it is possible a shuffle returns exactly the same as the input
@@ -481,8 +502,22 @@ describe('utils', function () {
481
502
expect ( output ) . to . have . lengthOf ( input . length ) ;
482
503
} ) ;
483
504
505
+ it ( 'should handle empty array' , function ( ) {
506
+ expect ( shuffle ( [ ] ) ) . to . deep . equal ( [ ] ) ;
507
+ expect ( shuffle ( [ ] , 0 ) ) . to . deep . equal ( [ ] ) ;
508
+ } ) ;
509
+
510
+ it ( 'should handle limit set to 0' , function ( ) {
511
+ expect ( shuffle ( [ 'a' , 'b' ] ) ) . to . have . lengthOf ( 2 ) ;
512
+ expect ( shuffle ( [ 'a' , 'b' ] , 0 ) ) . to . have . lengthOf ( 2 ) ;
513
+ } ) ;
514
+
515
+ it ( 'should throw if limit is greater than zero and empty array' , function ( ) {
516
+ expect ( ( ) => shuffle ( [ ] , 2 ) ) . to . throw ( MongoRuntimeError ) ;
517
+ } ) ;
518
+
484
519
it ( 'should throw if limit is larger than input size' , ( ) => {
485
- expect ( ( ) => shuffle ( [ ] , 100 ) ) . to . throw ( MongoInvalidArgumentError ) ;
520
+ expect ( ( ) => shuffle ( [ 'a' , 'b' ] , 3 ) ) . to . throw ( MongoRuntimeError ) ;
486
521
} ) ;
487
522
} ) ;
488
523
} ) ;
0 commit comments