From ab5a96f988eb1d5151b11a7a3986146d597cc1db Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Mon, 6 Jul 2026 00:39:34 +0000 Subject: [PATCH] [Tests] Cover array utility functions Add unit tests for takeRandomFromArray, getArrayRejectingUndefined, getArrayContainsDuplicates, and asHumanFriendlyArray in packages/cli-kit/src/public/common/array.ts to improve coverage. --- .../cli-kit/src/public/common/array.test.ts | 128 +++++++++++++++++- 1 file changed, 127 insertions(+), 1 deletion(-) diff --git a/packages/cli-kit/src/public/common/array.test.ts b/packages/cli-kit/src/public/common/array.test.ts index 720becbac0b..2863f5dc8a0 100644 --- a/packages/cli-kit/src/public/common/array.test.ts +++ b/packages/cli-kit/src/public/common/array.test.ts @@ -1,4 +1,12 @@ -import {difference, uniq, uniqBy} from './array.js' +import { + asHumanFriendlyArray, + difference, + getArrayContainsDuplicates, + getArrayRejectingUndefined, + takeRandomFromArray, + uniq, + uniqBy, +} from './array.js' import {describe, test, expect} from 'vitest' describe('uniqBy', () => { @@ -62,3 +70,121 @@ describe('difference', () => { expect(got).toEqual([1]) }) }) + +describe('takeRandomFromArray', () => { + test('returns a random element from the array', () => { + // Given + const array = [1, 2, 3, 4, 5] + + // When + const got = takeRandomFromArray(array) + + // Then + expect(array).toContain(got) + }) +}) + +describe('getArrayRejectingUndefined', () => { + test('removes undefined elements', () => { + // Given + const array = [1, undefined, 2, undefined, 3] + + // When + const got = getArrayRejectingUndefined(array) + + // Then + expect(got).toEqual([1, 2, 3]) + }) + + test('keeps other falsy values', () => { + // Given + const array = [0, '', null, false, undefined] as any[] + + // When + const got = getArrayRejectingUndefined(array) + + // Then + expect(got).toEqual([0, '', null, false]) + }) +}) + +describe('getArrayContainsDuplicates', () => { + test('returns true if there are duplicates', () => { + // Given + const array = [1, 2, 2, 3] + + // When + const got = getArrayContainsDuplicates(array) + + // Then + expect(got).toBe(true) + }) + + test('returns false if there are no duplicates', () => { + // Given + const array = [1, 2, 3] + + // When + const got = getArrayContainsDuplicates(array) + + // Then + expect(got).toBe(false) + }) +}) + +describe('asHumanFriendlyArray', () => { + test('returns empty array if input is empty', () => { + // Given + const array: string[] = [] + + // When + const got = asHumanFriendlyArray(array) + + // Then + expect(got).toEqual([]) + }) + + test('returns single item array if input has one item', () => { + // Given + const array = ['apple'] + + // When + const got = asHumanFriendlyArray(array) + + // Then + expect(got).toEqual(['apple']) + }) + + test('returns two items separated by and', () => { + // Given + const array = ['apple', 'banana'] + + // When + const got = asHumanFriendlyArray(array) + + // Then + expect(got).toEqual(['apple', 'and', 'banana']) + }) + + test('returns multiple items separated by commas and and', () => { + // Given + const array = ['apple', 'banana', 'cherry'] + + // When + const got = asHumanFriendlyArray(array) + + // Then + expect(got).toEqual(['apple', ', ', 'banana', 'and', 'cherry']) + }) + + test('handles more than three items', () => { + // Given + const array = ['apple', 'banana', 'cherry', 'date'] + + // When + const got = asHumanFriendlyArray(array) + + // Then + expect(got).toEqual(['apple', ', ', 'banana', ', ', 'cherry', 'and', 'date']) + }) +})