1
+ const shuffle = require ( '../../../../src/common/utils/shuffle' ) ;
2
+
3
+ describe ( 'shuffle' , ( ) => {
4
+ test ( 'Should return a shuffle array (same size, different order)' , async ( ) => {
5
+ // arrange
6
+ const tArray = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ;
7
+ // act
8
+ const result = shuffle ( tArray ) ;
9
+ // assert
10
+ expect ( result ) . toHaveLength ( tArray . length ) ;
11
+ expect ( result ) . not . toEqual ( tArray ) ;
12
+ } ) ;
13
+
14
+ test ( 'Should return a shuffle array for array of size 1' , async ( ) => {
15
+ // arrange
16
+ const tArray = [ 1 ] ;
17
+ // act
18
+ const result = shuffle ( tArray ) ;
19
+ // assert
20
+ expect ( result ) . toHaveLength ( tArray . length ) ;
21
+ expect ( result ) . toEqual ( tArray ) ;
22
+ } ) ;
23
+
24
+ test ( 'Should return a shuffle array for array of size 0' , async ( ) => {
25
+ // arrange
26
+ const tArray = [ ] ;
27
+ // act
28
+ const result = shuffle ( tArray ) ;
29
+ // assert
30
+ expect ( result ) . toHaveLength ( tArray . length ) ;
31
+ expect ( result ) . toEqual ( tArray ) ;
32
+ } ) ;
33
+
34
+ test ( 'Should return a shuffle only from a starting point' , async ( ) => {
35
+ // arrange
36
+ const tArray = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ;
37
+ // act
38
+ const result = shuffle ( tArray , 2 ) ;
39
+ // assert
40
+ expect ( result ) . toHaveLength ( tArray . length ) ;
41
+ expect ( result ) . not . toEqual ( tArray ) ;
42
+ const fixedOrigin = [ ...tArray ] . splice ( 0 , 2 ) ;
43
+ const shuffledOrigin = [ ...tArray ] . splice ( 2 ) ;
44
+ const fixedResult = [ ...result ] . splice ( 0 , 2 ) ;
45
+ const shuffledResult = [ ...result ] . splice ( 2 ) ;
46
+ expect ( fixedResult ) . toHaveLength ( fixedOrigin . length ) ;
47
+ expect ( fixedResult ) . toEqual ( fixedOrigin ) ;
48
+ expect ( shuffledResult ) . toHaveLength ( shuffledOrigin . length ) ;
49
+ expect ( shuffledResult ) . not . toEqual ( shuffledOrigin ) ;
50
+ } ) ;
51
+
52
+ test ( 'Should return a shuffle only from a starting point to and end point' , async ( ) => {
53
+ // arrange
54
+ const tArray = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ;
55
+ // act
56
+ const result = shuffle ( tArray , 2 , 4 ) ;
57
+ // assert
58
+ expect ( result ) . toHaveLength ( tArray . length ) ;
59
+ expect ( result ) . not . toEqual ( tArray ) ;
60
+ const fixedStartOrigin = [ ...tArray ] . splice ( 0 , 2 ) ;
61
+ const fixedEndOrigin = [ ...tArray ] . splice ( 4 ) ;
62
+ const shuffledOrigin = [ ...tArray ] . splice ( 2 , 4 ) ;
63
+ const fixedStartResult = [ ...result ] . splice ( 0 , 2 ) ;
64
+ const fixedEndResult = [ ...tArray ] . splice ( 4 ) ;
65
+ const shuffledResult = [ ...result ] . splice ( 2 , 4 ) ;
66
+ expect ( fixedStartResult ) . toHaveLength ( fixedStartOrigin . length ) ;
67
+ expect ( fixedStartResult ) . toEqual ( fixedStartOrigin ) ;
68
+
69
+ expect ( fixedEndResult ) . toHaveLength ( fixedEndOrigin . length ) ;
70
+ expect ( fixedEndResult ) . toEqual ( fixedEndOrigin ) ;
71
+
72
+ expect ( shuffledResult ) . toHaveLength ( shuffledOrigin . length ) ;
73
+ expect ( shuffledResult ) . not . toEqual ( shuffledOrigin ) ;
74
+ } ) ;
75
+ } ) ;
0 commit comments