Socialify

Folder ..

Viewing fake.spec.ts
50 lines (43 loc) • 1.6 KB

 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
import { describe, expect, it } from 'vitest';
import { faker } from '../dist/cjs';

describe('fake', () => {
  describe('fake()', () => {
    it('replaces a token with a random value for a method with no parameters', () => {
      const name = faker.fake('{{phone.phoneNumber}}');
      expect(name).match(/\d/);
    });

    it('replaces multiple tokens with random values for methods with no parameters', () => {
      const name = faker.fake(
        '{{helpers.randomize}}{{helpers.randomize}}{{helpers.randomize}}'
      );
      expect(name).match(/[abc]{3}/);
    });

    it('replaces a token with a random value for a methods with a simple parameter', () => {
      const random = faker.fake('{{helpers.slugify("Will This Work")}}');
      expect(random).toBe('Will-This-Work');
    });

    it('replaces a token with a random value for a method with an array parameter', () => {
      const arr = ['one', 'two', 'three'];
      const random = faker.fake(
        '{{helpers.randomize(["one", "two", "three"])}}'
      );
      expect(arr).toContain(random);
    });

    it('does not allow undefined parameters', () => {
      expect(() =>
        // @ts-expect-error
        faker.fake()
      ).toThrowError(Error('string parameter is required!'));
    });

    it('does not allow invalid module name', () => {
      expect(() => faker.fake('{{foo.bar}}')).toThrowError(
        Error('Invalid module: foo')
      );
    });

    it('does not allow invalid method name', () => {
      expect(() => faker.fake('{{address.foo}}')).toThrowError(
        Error('Invalid method: address.foo')
      );
    });
  });
});