|
| 1 | +import { describe, expectTypeOf, it } from 'vitest'; |
| 2 | +import { faker } from '../../src'; |
| 3 | + |
| 4 | +describe('helpers', () => { |
| 5 | + describe('shuffle', () => { |
| 6 | + describe('inplace: true', () => { |
| 7 | + it('const generic single element', () => { |
| 8 | + const actual = faker.helpers.shuffle([1], { inplace: true }); |
| 9 | + expectTypeOf(actual).toEqualTypeOf<Array<1>>(); |
| 10 | + }); |
| 11 | + |
| 12 | + it('const generic multiple elements', () => { |
| 13 | + const actual = faker.helpers.shuffle([1, 'a', false], { |
| 14 | + inplace: true, |
| 15 | + }); |
| 16 | + expectTypeOf(actual).toEqualTypeOf<Array<1 | 'a' | false>>(); |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('inplace: false', () => { |
| 21 | + it('const generic single element', () => { |
| 22 | + const actual = faker.helpers.shuffle([1], { inplace: false }); |
| 23 | + expectTypeOf(actual).toEqualTypeOf<Array<1>>(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('const generic multiple elements', () => { |
| 27 | + const actual = faker.helpers.shuffle([1, 'a', false], { |
| 28 | + inplace: false, |
| 29 | + }); |
| 30 | + expectTypeOf(actual).toEqualTypeOf<Array<1 | 'a' | false>>(); |
| 31 | + }); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('uniqueArray', () => { |
| 36 | + it('const generic single element', () => { |
| 37 | + const actual = faker.helpers.uniqueArray([1], 1); |
| 38 | + expectTypeOf(actual).toEqualTypeOf<Array<1>>(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('const generic multiple elements', () => { |
| 42 | + const actual = faker.helpers.uniqueArray([1, 'a', false], 3); |
| 43 | + expectTypeOf(actual).toEqualTypeOf<Array<1 | 'a' | false>>(); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('maybe', () => { |
| 48 | + it('const generic single element', () => { |
| 49 | + // TODO @ST-DDT 2024-02-25: Check why this is detected as `number` instead of `1` |
| 50 | + const actual = faker.helpers.maybe(() => 1); |
| 51 | + expectTypeOf(actual).toEqualTypeOf<number | undefined>(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('objectKey', () => { |
| 56 | + it('const generic single element', () => { |
| 57 | + const actual = faker.helpers.objectKey({ a: 1 }); |
| 58 | + expectTypeOf(actual).toEqualTypeOf<'a'>(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('const generic multiple elements', () => { |
| 62 | + const actual = faker.helpers.objectKey({ a: 1, b: 'a', c: false }); |
| 63 | + expectTypeOf(actual).toEqualTypeOf<'a' | 'b' | 'c'>(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('objectValue', () => { |
| 68 | + it('const generic single element', () => { |
| 69 | + const actual = faker.helpers.objectValue({ a: 1 }); |
| 70 | + expectTypeOf(actual).toEqualTypeOf<1>(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('const generic multiple elements', () => { |
| 74 | + const actual = faker.helpers.objectValue({ a: 1, b: 'a', c: false }); |
| 75 | + expectTypeOf(actual).toEqualTypeOf<1 | 'a' | false>(); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('objectEntry', () => { |
| 80 | + it('const generic single element', () => { |
| 81 | + const actual = faker.helpers.objectEntry({ a: 1 }); |
| 82 | + expectTypeOf(actual).toEqualTypeOf<['a', 1]>(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('const generic multiple elements', () => { |
| 86 | + const actual = faker.helpers.objectEntry({ a: 1, b: 'a', c: false }); |
| 87 | + // TODO @ST-DDT 2024-02-25: Check whether we can infer the return type any better |
| 88 | + expectTypeOf(actual).toEqualTypeOf<['a' | 'b' | 'c', false | 1 | 'a']>(); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('arrayElement', () => { |
| 93 | + it('const generic single element', () => { |
| 94 | + const actual = faker.helpers.arrayElement([1]); |
| 95 | + expectTypeOf(actual).toEqualTypeOf<1>(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('const generic multiple elements', () => { |
| 99 | + const actual = faker.helpers.arrayElement([1, 'a', false]); |
| 100 | + expectTypeOf(actual).toEqualTypeOf<1 | 'a' | false>(); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('arrayElements', () => { |
| 105 | + it('const generic single element', () => { |
| 106 | + const actual = faker.helpers.arrayElements([1], 1); |
| 107 | + expectTypeOf(actual).toEqualTypeOf<Array<1>>(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('const generic multiple elements', () => { |
| 111 | + const actual = faker.helpers.arrayElements([1, 'a', false], 3); |
| 112 | + expectTypeOf(actual).toEqualTypeOf<Array<1 | 'a' | false>>(); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('multiple', () => { |
| 117 | + it('const generic single element', () => { |
| 118 | + const actual = faker.helpers.multiple(() => 1); |
| 119 | + expectTypeOf(actual).toEqualTypeOf<number[]>(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('const generic multiple elements', () => { |
| 123 | + const actual = faker.helpers.multiple(() => 1, { count: 3 }); |
| 124 | + expectTypeOf(actual).toEqualTypeOf<number[]>(); |
| 125 | + }); |
| 126 | + }); |
| 127 | +}); |
0 commit comments