-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrandomArrayElement.tests.js
More file actions
32 lines (29 loc) · 1.05 KB
/
randomArrayElement.tests.js
File metadata and controls
32 lines (29 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { randomArrayElement } from '../../../lib/utils/array/randomArrayElement'
jest.retryTimes(1)
describe(randomArrayElement.name, function () {
it('returns the first element in 0 or 1 length arrays', () => {
expect(randomArrayElement([])).toBe(undefined)
expect(randomArrayElement([0])).toBe(0)
})
it('throws if given value is no arary', () => {
[{}, () => {}, new Date(), 1, '1', false, undefined, null]
.forEach(value => {
expect(() => randomArrayElement(value))
.toThrow(`Expected array, got ${value}`)
})
})
it('returns a random element from an array', () => {
const input = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const covered = new Set()
for (let i = 0; i < 1000; i++) {
const element = randomArrayElement(input)
covered.add(element)
const index = input.indexOf(element)
expect(index).toBeGreaterThan(-1)
}
// there might be the chance of failing,
// but it's super low!
// still we use retryTimes to cover this
expect(covered.size).toBe(input.length)
})
})