aboutsummaryrefslogtreecommitdiff
path: root/test/modules/datatype.spec.ts
blob: 0dc59fe4d99b0d62c952444ecaee9064832795b8 (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
import { describe, expect, it } from 'vitest';
import { faker } from '../../src';
import { seededTests } from '../support/seeded-runs';
import { times } from './../support/times';

const NON_SEEDED_BASED_RUN = 25;

describe('datatype', () => {
  seededTests(faker, 'datatype', (t) => {
    t.describe('boolean', (t) => {
      t.itRepeated('noArgs', 5)
        .it('with probability', 0.42)
        .it('with probability option', { probability: 0.13 });
    });
  });

  describe.each(times(NON_SEEDED_BASED_RUN).map(() => faker.seed()))(
    'random seeded tests for seed %i',
    () => {
      describe('boolean', () => {
        it('generates a boolean value', () => {
          const bool = faker.datatype.boolean();
          expect(bool).toBeTypeOf('boolean');
        });

        it('generates false for probability = 0', () => {
          const bool = faker.datatype.boolean(0);
          expect(bool).toBe(false);
        });

        it('generates true for probability = 1', () => {
          const bool = faker.datatype.boolean(1);
          expect(bool).toBe(true);
        });

        it.each([-5, 0.42, 5])(
          'generates a boolean value with given probability',
          (probability) => {
            const bool = faker.datatype.boolean(probability);
            expect(bool).toBeTypeOf('boolean');
          }
        );

        it('generates a boolean value for empty options', () => {
          const bool = faker.datatype.boolean({});
          expect(bool).toBeTypeOf('boolean');
        });

        it('generates false for { probability: 0 }', () => {
          const bool = faker.datatype.boolean({ probability: 0 });
          expect(bool).toBe(false);
        });

        it('generates true for { probability: 1 }', () => {
          const bool = faker.datatype.boolean({ probability: 1 });
          expect(bool).toBe(true);
        });

        it.each([-5, 0.42, 5])(
          'generates a boolean value with given probability option',
          (probability) => {
            const bool = faker.datatype.boolean({ probability });
            expect(bool).toBeTypeOf('boolean');
          }
        );

        it('should not mutate the input object', () => {
          const filledOptions: { probability?: number } = Object.freeze({
            probability: 1,
          });
          expect(() => faker.datatype.boolean(filledOptions)).not.toThrow();

          const emptyOptions: { probability?: number } = Object.freeze({});
          expect(() => faker.datatype.boolean(emptyOptions)).not.toThrow();
        });
      });
    }
  );
});