aboutsummaryrefslogtreecommitdiff
path: root/test/simple-faker.spec.ts
blob: c1470624654e0fe7dfba251e83574f165c681039 (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
132
133
134
135
import type { MockInstance } from 'vitest';
import { describe, expect, it, vi } from 'vitest';
import { generateMersenne32Randomizer, SimpleFaker, simpleFaker } from '../src';
import { keys } from '../src/internal/keys';

describe('simpleFaker', () => {
  it('should not log anything on startup', async () => {
    const spies: MockInstance[] = keys(console)
      .filter((key) => typeof console[key] === 'function')
      .map((methodName) => vi.spyOn(console, methodName));

    // Using import() requires types being build but the CI / TS-Check runs without them.
    const { simpleFaker: importedSimpleFaker } = await import('..');
    expect(importedSimpleFaker).toBeDefined();

    expect(new SimpleFaker()).toBeDefined();

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

  describe('constructor()', () => {
    describe('randomizer', () => {
      it('should be possible to provide a custom Randomizer', () => {
        const customFaker = new SimpleFaker({
          randomizer: {
            next: () => 0,
            seed: () => void 0,
          },
        });

        expect(customFaker.number.int()).toBe(0);
        expect(customFaker.number.int()).toBe(0);
        expect(customFaker.number.int()).toBe(0);
      });
    });

    describe('seed', () => {
      it('should be possible to provide an initial seed', () => {
        const customFaker = new SimpleFaker({
          seed: 12345,
        });

        expect(customFaker.number.int()).toBe(8373237378417847);
        expect(customFaker.number.int()).toBe(2849657659447330);
        expect(customFaker.number.int()).toBe(1656593383470774);

        customFaker.seed(12345); // Retry with the expected seed

        expect(customFaker.number.int()).toBe(8373237378417847);
        expect(customFaker.number.int()).toBe(2849657659447330);
        expect(customFaker.number.int()).toBe(1656593383470774);
      });
    });

    describe('randomizer+seed', () => {
      it('should take apply both the randomizer and seed', () => {
        const customFaker = new SimpleFaker({
          randomizer: generateMersenne32Randomizer(67890),
          seed: 12345,
        });

        expect(customFaker.number.int()).toBe(8373237322874880);
        expect(customFaker.number.int()).toBe(8017800868134912);
        expect(customFaker.number.int()).toBe(2849657711493120);

        customFaker.seed(12345); // Retry with the expected seed

        expect(customFaker.number.int()).toBe(8373237322874880);
        expect(customFaker.number.int()).toBe(8017800868134912);
        expect(customFaker.number.int()).toBe(2849657711493120);
      });
    });
  });

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

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

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

      const num1 = simpleFaker.number.int();

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

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

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

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

      const actual = simpleFaker.string.uuid();
      expect(actual).toMatchInlineSnapshot(
        '"6b042125-686a-43e0-8a68-23cf5bee102e"'
      );
    });

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

      const actual = simpleFaker.string.uuid();
      expect(actual).toMatchInlineSnapshot(
        '"9e7e0e9f-9e8e-4408-b5b1-8002907d7dd6"'
      );
    });
  });

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

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