aboutsummaryrefslogtreecommitdiff
path: root/test/locale-imports.spec.ts
blob: eb1bb508b9cdf3e4b36f7e64eb81052d0c08dda2 (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
import { describe, expect, it } from 'vitest';
import type { Faker } from '../src';
import { allLocales } from '../src';
import { keys } from '../src/internal/keys';

describe.each(keys(allLocales))('locale imports', (locale) => {
  it(`should be possible to directly require('@faker-js/faker/locale/${locale}')`, () => {
    // eslint-disable-next-line @typescript-eslint/no-require-imports, unicorn/prefer-module
    const { faker } = require(`../dist/locale/${locale}.cjs`) as {
      faker: Faker;
    };

    expect(faker).toBeDefined();
    expect(faker.string.alpha()).toBeTypeOf('string');
    expect(faker.definitions.metadata.title).toBe(
      allLocales[locale].metadata?.title
    );
  });

  it(`should be possible to directly import('@faker-js/faker/locale/${locale}')`, async () => {
    const { faker } = (await import(`../dist/locale/${locale}.js`)) as {
      faker: Faker;
    };

    expect(faker).toBeDefined();
    expect(faker.string.alpha()).toBeTypeOf('string');
    expect(faker.definitions.metadata.title).toBe(
      allLocales[locale].metadata?.title
    );
  });

  it('should have complete metadata values', () => {
    const metadata = allLocales[locale].metadata ?? {};
    expect(metadata.title).toBeTypeOf('string');
    expect(metadata.code).toBeTypeOf('string');
    expect(metadata.code).toEqual(locale);
    if (locale !== 'base') {
      expect(metadata.code).toEqual(
        [metadata.language, metadata.country, metadata.variant]
          .filter((v) => v != null)
          .join('_')
      );
      expect(metadata.language).toBeTypeOf('string');
      expect(metadata.language).toMatch(/^[a-z]{2}$/);
      expect(metadata.script).toBeTypeOf('string');
      expect([
        'Arab',
        'Armn',
        'Beng',
        'Cyrl',
        'Deva',
        'Geor',
        'Grek',
        'Hans',
        'Hant',
        'Hebr',
        'Jpan',
        'Kore',
        'Latn',
        'Thaa',
        'Thai',
      ]).toContain(metadata.script);
      expect(metadata.endonym).toBeTypeOf('string');
      expect(metadata.dir).toBeTypeOf('string');
      expect(['ltr', 'rtl']).toContain(metadata.dir);
      if (metadata.country) {
        expect(metadata.country).toBeTypeOf('string');
        expect(metadata.country).toMatch(/^[A-Z]{2}$/);
      }
    }
  });
});