From c45537f6d4f3a28d8be1ebbe03567004c04af145 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sun, 25 Feb 2024 22:05:57 +0100 Subject: feat(helpers)!: use const generics where possible (#2685) --- test/modules/helpers.spec-d.ts | 127 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 test/modules/helpers.spec-d.ts (limited to 'test/modules') diff --git a/test/modules/helpers.spec-d.ts b/test/modules/helpers.spec-d.ts new file mode 100644 index 00000000..11ce8c88 --- /dev/null +++ b/test/modules/helpers.spec-d.ts @@ -0,0 +1,127 @@ +import { describe, expectTypeOf, it } from 'vitest'; +import { faker } from '../../src'; + +describe('helpers', () => { + describe('shuffle', () => { + describe('inplace: true', () => { + it('const generic single element', () => { + const actual = faker.helpers.shuffle([1], { inplace: true }); + expectTypeOf(actual).toEqualTypeOf>(); + }); + + it('const generic multiple elements', () => { + const actual = faker.helpers.shuffle([1, 'a', false], { + inplace: true, + }); + expectTypeOf(actual).toEqualTypeOf>(); + }); + }); + + describe('inplace: false', () => { + it('const generic single element', () => { + const actual = faker.helpers.shuffle([1], { inplace: false }); + expectTypeOf(actual).toEqualTypeOf>(); + }); + + it('const generic multiple elements', () => { + const actual = faker.helpers.shuffle([1, 'a', false], { + inplace: false, + }); + expectTypeOf(actual).toEqualTypeOf>(); + }); + }); + }); + + describe('uniqueArray', () => { + it('const generic single element', () => { + const actual = faker.helpers.uniqueArray([1], 1); + expectTypeOf(actual).toEqualTypeOf>(); + }); + + it('const generic multiple elements', () => { + const actual = faker.helpers.uniqueArray([1, 'a', false], 3); + expectTypeOf(actual).toEqualTypeOf>(); + }); + }); + + describe('maybe', () => { + it('const generic single element', () => { + // TODO @ST-DDT 2024-02-25: Check why this is detected as `number` instead of `1` + const actual = faker.helpers.maybe(() => 1); + expectTypeOf(actual).toEqualTypeOf(); + }); + }); + + describe('objectKey', () => { + it('const generic single element', () => { + const actual = faker.helpers.objectKey({ a: 1 }); + expectTypeOf(actual).toEqualTypeOf<'a'>(); + }); + + it('const generic multiple elements', () => { + const actual = faker.helpers.objectKey({ a: 1, b: 'a', c: false }); + expectTypeOf(actual).toEqualTypeOf<'a' | 'b' | 'c'>(); + }); + }); + + describe('objectValue', () => { + it('const generic single element', () => { + const actual = faker.helpers.objectValue({ a: 1 }); + expectTypeOf(actual).toEqualTypeOf<1>(); + }); + + it('const generic multiple elements', () => { + const actual = faker.helpers.objectValue({ a: 1, b: 'a', c: false }); + expectTypeOf(actual).toEqualTypeOf<1 | 'a' | false>(); + }); + }); + + describe('objectEntry', () => { + it('const generic single element', () => { + const actual = faker.helpers.objectEntry({ a: 1 }); + expectTypeOf(actual).toEqualTypeOf<['a', 1]>(); + }); + + it('const generic multiple elements', () => { + const actual = faker.helpers.objectEntry({ a: 1, b: 'a', c: false }); + // TODO @ST-DDT 2024-02-25: Check whether we can infer the return type any better + expectTypeOf(actual).toEqualTypeOf<['a' | 'b' | 'c', false | 1 | 'a']>(); + }); + }); + + describe('arrayElement', () => { + it('const generic single element', () => { + const actual = faker.helpers.arrayElement([1]); + expectTypeOf(actual).toEqualTypeOf<1>(); + }); + + it('const generic multiple elements', () => { + const actual = faker.helpers.arrayElement([1, 'a', false]); + expectTypeOf(actual).toEqualTypeOf<1 | 'a' | false>(); + }); + }); + + describe('arrayElements', () => { + it('const generic single element', () => { + const actual = faker.helpers.arrayElements([1], 1); + expectTypeOf(actual).toEqualTypeOf>(); + }); + + it('const generic multiple elements', () => { + const actual = faker.helpers.arrayElements([1, 'a', false], 3); + expectTypeOf(actual).toEqualTypeOf>(); + }); + }); + + describe('multiple', () => { + it('const generic single element', () => { + const actual = faker.helpers.multiple(() => 1); + expectTypeOf(actual).toEqualTypeOf(); + }); + + it('const generic multiple elements', () => { + const actual = faker.helpers.multiple(() => 1, { count: 3 }); + expectTypeOf(actual).toEqualTypeOf(); + }); + }); +}); -- cgit v1.2.3