From 6f0d6e8abc004040497abd608e6d193af09991e8 Mon Sep 17 00:00:00 2001 From: Phan An Date: Sat, 14 May 2022 17:51:47 +0200 Subject: [PATCH] feat(test): add helper util tests --- resources/assets/js/utils/helpers.spec.ts | 35 +++++++++++++++++++++++ resources/assets/js/utils/helpers.ts | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 resources/assets/js/utils/helpers.spec.ts diff --git a/resources/assets/js/utils/helpers.spec.ts b/resources/assets/js/utils/helpers.spec.ts new file mode 100644 index 00000000..ea9f76df --- /dev/null +++ b/resources/assets/js/utils/helpers.spec.ts @@ -0,0 +1,35 @@ +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']] + ])('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, []] + ])('takes %d elements from %d position', (count, position, result) => { + expect(limitBy(['a', 'b', 'c', 'd'], count, position)).toEqual(result) + }) + } +} diff --git a/resources/assets/js/utils/helpers.ts b/resources/assets/js/utils/helpers.ts index 3319e848..bd268004 100644 --- a/resources/assets/js/utils/helpers.ts +++ b/resources/assets/js/utils/helpers.ts @@ -12,4 +12,4 @@ export const arrayify = (maybeArray: T | Array) => ([] as Array).conca export const noop = () => { } -export const limitBy = (arr: T[], n: number, offset: number = 0): T[] => arr.slice(offset, offset + n) +export const limitBy = (arr: T[], count: number, offset: number = 0): T[] => arr.slice(offset, offset + count)