koel/resources/assets/js/utils/helpers.spec.ts

36 lines
1 KiB
TypeScript
Raw Normal View History

2022-05-14 15:51:47 +00:00
import UnitTestCase from '@/__tests__/UnitTestCase'
import { expect, it, vi } from 'vitest'
import { arrayify, limitBy, use } from './helpers'
new class extends UnitTestCase {
protected test () {
it('use() triggers a closure with a defined value', () => {
const mock = vi.fn()
use('foo', mock)
expect(mock).toHaveBeenCalledWith('foo')
})
it('use() does not trigger a closure with an undefined value', () => {
const mock = vi.fn()
use(undefined, mock)
expect(mock).not.toHaveBeenCalled()
})
it.each([
['foo', ['foo']],
[['foo', 'bar'], ['foo', 'bar']],
2022-05-14 15:51:47 +00:00
])('turns the parameter into an array', (input, output) => expect(arrayify(input)).toEqual(output))
it.each([
[2, 0, ['a', 'b']],
[2, 1, ['b', 'c']],
[1, 0, ['a']],
[1, 2, ['c']],
[0, 0, []],
[0, 1, []],
2022-05-14 15:51:47 +00:00
])('takes %d elements from %d position', (count, position, result) => {
expect(limitBy(['a', 'b', 'c', 'd'], count, position)).toEqual(result)
})
}
}