@@ -292,6 +292,45 @@ describe('helpers', () => {
292292
293293 expect ( result ) . toHaveLength ( 0 ) ;
294294 } ) ;
295+
296+ it ( 'should return the only element in the array when there is only 1' , ( ) => {
297+ const testArray = [ 'hello' ] ;
298+ const actual = faker . helpers . arrayElements ( testArray ) ;
299+ expect ( actual ) . toEqual ( testArray ) ;
300+ } ) ;
301+
302+ it ( 'should return each element with a somewhat equal distribution with 2 elements' , ( ) => {
303+ const input = Array . from ( { length : 2 } , ( _ , i ) => i ) ;
304+ const occurrences = Array . from ( { length : 2 } , ( ) => 0 ) ;
305+
306+ for ( let i = 0 ; i < 1000 ; i ++ ) {
307+ const [ result ] = faker . helpers . arrayElements ( input , 1 ) ;
308+ occurrences [ result ] ++ ;
309+ }
310+
311+ for ( const occurrence of occurrences ) {
312+ expect ( occurrence ) . toBeGreaterThanOrEqual ( 400 ) ;
313+ expect ( occurrence ) . toBeLessThanOrEqual ( 600 ) ;
314+ }
315+ } ) ;
316+
317+ it . each ( [ 10 , 100 , 1000 ] ) (
318+ 'should return each element with a somewhat equal distribution with %s elements' ,
319+ ( length ) => {
320+ const input = Array . from ( { length } , ( _ , i ) => i % 10 ) ;
321+ const occurrences = Array . from ( { length : 10 } , ( ) => 0 ) ;
322+
323+ for ( let i = 0 ; i < 1000 ; i ++ ) {
324+ const [ result ] = faker . helpers . arrayElements ( input , 1 ) ;
325+ occurrences [ result ] ++ ;
326+ }
327+
328+ for ( const occurrence of occurrences ) {
329+ expect ( occurrence ) . toBeGreaterThanOrEqual ( 70 ) ;
330+ expect ( occurrence ) . toBeLessThanOrEqual ( 130 ) ;
331+ }
332+ }
333+ ) ;
295334 } ) ;
296335
297336 describe ( 'slugify()' , ( ) => {
0 commit comments