Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 127 additions & 1 deletion packages/cli-kit/src/public/common/array.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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'])
})
})
Loading