Skip to content

Commit

Permalink
Utils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bgonp committed Apr 7, 2021
1 parent da61660 commit ce2ca46
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
65 changes: 65 additions & 0 deletions __tests__/utils/randomNumbers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { getRandomNumbersFactory } from 'utils/randomNumbers'

describe('getRandomNumbersFactory', () => {
test('should return a numbers factory', () => {
const getNumbers = getRandomNumbersFactory()
expect(typeof getNumbers).toBe('function')
})

test('should return an array of one number', () => {
const getNumbers = getRandomNumbersFactory()
const numbers = getNumbers()
expect(Array.isArray(numbers)).toBe(true)
})

test('should return a single random number', () => {
const getNumbers = getRandomNumbersFactory()
const numbers = getNumbers()
expect(numbers.length).toBe(1)
expect(numbers[0]).toBeGreaterThanOrEqual(0)
expect(numbers[0]).toBeLessThanOrEqual(1000)
})

test('should return array with exact length', () => {
const getNumbers = getRandomNumbersFactory(10, 10)
const numbers = getNumbers()
expect(numbers).toHaveLength(10)
})

test('should return array of different numbers', () => {
const getNumbers = getRandomNumbersFactory(100, 100)
const numbers = getNumbers()
const withoutRepeated = numbers.reduce(
(acc, number) => acc.includes(number) ? acc : acc.concat(number),
[]
)
expect(numbers.length).toBeGreaterThanOrEqual(withoutRepeated.length)
})

test('should return numbers between bounds', () => {
const getNumbers = getRandomNumbersFactory(1000, 1000, [[10, 100]])
const numbers = getNumbers()
expect(numbers.filter(number => number < 10 || number > 100)).toHaveLength(0)
})

test('should return numbers between several bounds', () => {
const getNumbers = getRandomNumbersFactory(1000, 1000, [[10, 10], [100, 100]])
const numbers = getNumbers()
expect(numbers.includes(10)).toBe(true)
expect(numbers.includes(100)).toBe(true)
})

test('should return different numbers each time', () => {
const getNumbers = getRandomNumbersFactory(1, 1, [[1, 1000]])
const numbers1 = getNumbers()
const numbers2 = getNumbers()
expect(numbers1[0]).not.toBe(numbers2[0])
})

test('should return arrays with different length each time', () => {
const getNumbers = getRandomNumbersFactory(1, 1000, [[10, 100]])
const numbers1 = getNumbers()
const numbers2 = getNumbers()
expect(numbers1.length).not.toBe(numbers2.length)
})
})
15 changes: 15 additions & 0 deletions __tests__/utils/twoDigits.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { twoDigits } from 'utils/twoDigits'

describe('twoDigits', () => {
test('should return same number as string', () => {
expect(twoDigits(10)).toBe('10')
})

test('should return number with left 0', () => {
expect(twoDigits(5)).toBe('05')
})

test('should return last two digits', () => {
expect(twoDigits(123)).toBe('23')
})
})

0 comments on commit ce2ca46

Please sign in to comment.