@@ -76,7 +76,11 @@ describe('helpers', () => {
7676 t . describe ( 'arrayElements' , ( t ) => {
7777 t . it ( 'noArgs' )
7878 . it ( 'with array' , 'Hello World!' . split ( '' ) )
79- . it ( 'with array and count' , 'Hello World!' . split ( '' ) , 3 ) ;
79+ . it ( 'with array and count' , 'Hello World!' . split ( '' ) , 3 )
80+ . it ( 'with array and count range' , 'Hello World!' . split ( '' ) , {
81+ min : 1 ,
82+ max : 5 ,
83+ } ) ;
8084 } ) ;
8185
8286 t . describe ( 'shuffle' , ( t ) => {
@@ -281,6 +285,46 @@ describe('helpers', () => {
281285 expect ( subset ) . toHaveLength ( new Set ( subset ) . size ) ;
282286 } ) ;
283287
288+ it ( 'should return a subset with random elements in the array for a length range' , ( ) => {
289+ const testArray = [ 'hello' , 'to' , 'you' , 'my' , 'friend' ] ;
290+ const subset = faker . helpers . arrayElements ( testArray , {
291+ min : 2 ,
292+ max : 4 ,
293+ } ) ;
294+
295+ // Check length
296+ expect ( subset . length ) . toBeGreaterThanOrEqual ( 2 ) ;
297+ expect ( subset . length ) . toBeLessThanOrEqual ( 4 ) ;
298+
299+ // Check elements
300+ subset . forEach ( ( element ) => {
301+ expect ( testArray ) . toContain ( element ) ;
302+ } ) ;
303+
304+ // Check uniqueness
305+ expect ( subset ) . not . toContainDuplicates ( ) ;
306+ } ) ;
307+
308+ it ( 'should return an array with all elements when count > array length' , ( ) => {
309+ const testArray = [ 'hello' , 'to' , 'you' , 'my' , 'friend' ] ;
310+ const subset = faker . helpers . arrayElements ( testArray , 6 ) ;
311+
312+ // Check length
313+ expect ( subset . length ) . toEqual ( 5 ) ;
314+
315+ // Check elements
316+ subset . forEach ( ( element ) => {
317+ expect ( testArray ) . toContain ( element ) ;
318+ } ) ;
319+ } ) ;
320+
321+ it ( 'should return an empty array when array length > 0 and count = 0' , ( ) => {
322+ const testArray = [ 'hello' , 'to' , 'you' , 'my' , 'friend' ] ;
323+ const result = faker . helpers . arrayElements ( testArray , 0 ) ;
324+
325+ expect ( result ) . toHaveLength ( 0 ) ;
326+ } ) ;
327+
284328 it ( 'should return an empty array when receiving an empty array' , ( ) => {
285329 const result = faker . helpers . arrayElements ( [ ] ) ;
286330
0 commit comments