aboutsummaryrefslogtreecommitdiff
path: root/test/faker.spec.ts
blob: 207dac4a7a359b2c1302c0f7e12b4574f71ccc09 (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
import type { SpyInstance } from 'vitest';
import { describe, expect, it, vi } from 'vitest';
import { faker, Faker } from '../src';
import { FakerError } from '../src/errors/faker-error';

describe('faker', () => {
  it('should throw error if no locales passed', () => {
    expect(() => new Faker({ locale: [] })).toThrow(
      new FakerError(
        'The locale option must contain at least one locale definition.'
      )
    );
  });

  it('should not log anything on startup', () => {
    const spies: SpyInstance[] = Object.keys(console)
      .filter((key) => typeof console[key] === 'function')
      .map((methodName) =>
        vi.spyOn(console, methodName as keyof typeof console)
      );

    // eslint-disable-next-line @typescript-eslint/no-var-requires
    require('..').faker;

    new Faker({ locale: { metadata: { title: '' } } });

    for (const spy of spies) {
      expect(spy).not.toHaveBeenCalled();
      spy.mockRestore();
    }
  });

  describe('rawDefinitions', () => {
    it('locale rawDefinition accessibility', () => {
      // Metadata
      expect(faker.rawDefinitions.metadata.title).toBeDefined();
      // Standard modules
      expect(faker.rawDefinitions.location?.city_name).toBeDefined();
      // Non-existing module
      expect(faker.rawDefinitions.missing).toBeUndefined();
      // Non-existing definition in a non-existing module
      expect(faker.rawDefinitions.missing?.missing).toBeUndefined();
      // Non-existing definition in an existing module
      expect(faker.rawDefinitions.location?.missing).toBeUndefined();
    });
  });

  describe('definitions', () => {
    it('locale definition accessibility', () => {
      // Metadata
      expect(faker.definitions.metadata.title).toBeDefined();
      // Standard modules
      expect(faker.definitions.location.city_name).toBeDefined();
      // Non-existing module
      expect(faker.definitions.missing).toBeDefined();
      // Non-existing definition in a non-existing module
      expect(() => faker.definitions.missing.missing).toThrow();
      // Non-existing definition in an existing module
      expect(() => faker.definitions.location.missing).toThrow();
    });
  });

  // This is only here for coverage
  // The actual test is in mersenne.spec.ts
  describe('seed()', () => {
    it('seed()', () => {
      const seed = faker.seed();

      expect(seed).toBeDefined();
      expect(seed).toBeTypeOf('number');
    });

    it('should reset the sequence when calling `seed`', () => {
      const seed = faker.seed();

      const num1 = faker.number.int();

      const newSeed = faker.seed(seed);
      const num2 = faker.number.int();

      expect(num1).toBe(num2);
      expect(newSeed).toBe(seed);

      const num3 = faker.number.int();
      expect(num1).not.toBe(num3);
    });

    it('seed(number)', () => {
      faker.seed(1);

      const actual = faker.animal.cat();
      expect(actual).toBe('Korat');
    });

    it('seed(number[])', () => {
      faker.seed([1, 2, 3]);

      const actual = faker.animal.cat();
      expect(actual).toBe('Oriental');
    });
  });

  describe('defaultRefDate', () => {
    it('should be a defined', () => {
      expect(faker.defaultRefDate).toBeDefined();
    });

    it('should be a date in the very recent past', () => {
      const start = Date.now();
      const refDate = faker.defaultRefDate().getTime();
      const end = Date.now();
      expect(refDate).toBeGreaterThanOrEqual(start);
      expect(refDate).toBeLessThanOrEqual(end);
    });
  });
});