aboutsummaryrefslogtreecommitdiff
path: root/test/modules/helpers.spec-d.ts
blob: 7e1729d807812ce09872a8b1ff94bf60a1db8516 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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<Array<1>>();
      });

      it('const generic multiple elements', () => {
        const actual = faker.helpers.shuffle([1, 'a', false], {
          inplace: true,
        });
        expectTypeOf(actual).toEqualTypeOf<Array<1 | 'a' | false>>();
      });
    });

    describe('inplace: false', () => {
      it('const generic single element', () => {
        const actual = faker.helpers.shuffle([1], { inplace: false });
        expectTypeOf(actual).toEqualTypeOf<Array<1>>();
      });

      it('const generic multiple elements', () => {
        const actual = faker.helpers.shuffle([1, 'a', false], {
          inplace: false,
        });
        expectTypeOf(actual).toEqualTypeOf<Array<1 | 'a' | false>>();
      });
    });
  });

  describe('uniqueArray', () => {
    it('const generic single element', () => {
      const actual = faker.helpers.uniqueArray([1], 1);
      expectTypeOf(actual).toEqualTypeOf<Array<1>>();
    });

    it('const generic multiple elements', () => {
      const actual = faker.helpers.uniqueArray([1, 'a', false], 3);
      expectTypeOf(actual).toEqualTypeOf<Array<1 | 'a' | false>>();
    });
  });

  describe('maybe', () => {
    it('generic single element', () => {
      const actual = faker.helpers.maybe(() => 1);
      expectTypeOf(actual).toEqualTypeOf<number | undefined>();
    });

    it('const generic single element', () => {
      const actual = faker.helpers.maybe(() => 1 as const);
      expectTypeOf(actual).toEqualTypeOf<1 | undefined>();
    });
  });

  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<Array<1>>();
    });

    it('const generic multiple elements', () => {
      const actual = faker.helpers.arrayElements([1, 'a', false], 3);
      expectTypeOf(actual).toEqualTypeOf<Array<1 | 'a' | false>>();
    });
  });

  describe('multiple', () => {
    it('const generic single element', () => {
      const actual = faker.helpers.multiple(() => 1);
      expectTypeOf(actual).toEqualTypeOf<number[]>();
    });

    it('const generic multiple elements', () => {
      const actual = faker.helpers.multiple(() => 1, { count: 3 });
      expectTypeOf(actual).toEqualTypeOf<number[]>();
    });
  });
});