aboutsummaryrefslogtreecommitdiff
path: root/test/locale-imports.spec.ts
blob: 6813dfe786096aa5b3c6c5c6bb44a5ecd58401ae (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
import isISO15924 from 'validator/lib/isISO15924';
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 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(metadata.script).toSatisfy(isISO15924);
      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}$/);
      }
    }
  });
});