aboutsummaryrefslogtreecommitdiff
path: root/test/locale-proxy.spec.ts
blob: 6faf6ac0b2c7e2accc5e133649d5bdad2ab75740 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { describe, expect, it } from 'vitest';
import { en, FakerError } from '../src';
import { createLocaleProxy } from '../src/locale-proxy';

describe('LocaleProxy', () => {
  const locale = createLocaleProxy(en);
  const enAirline = en.airline ?? { never: 'missing' };

  describe('locale', () => {
    it('should be possible to use equals on locale', () => {
      expect(locale).toEqual(createLocaleProxy(en));
    });

    it('should be possible to use not equals on locale', () => {
      expect(locale).not.toEqual(createLocaleProxy({}));
    });
  });

  describe('category', () => {
    it('should be possible to check for a missing category', () => {
      expect('category' in locale).toBe(true);
    });

    it('should be possible to check for an existing category', () => {
      expect('airline' in locale).toBe(true);
    });

    it('should be possible to access the title', () => {
      expect(locale.metadata.title).toBe('English');
    });

    it('should be possible to access a missing category', () => {
      expect(locale.category).toBeDefined();
    });

    it('should not be possible to add a new category', () => {
      expect(() => {
        // @ts-expect-error: LocaleProxy is read-only.
        locale.category = {};
      }).toThrowError(
        new FakerError('You cannot edit the locale data on the faker instance')
      );
    });

    it('should not be possible to replace a category', () => {
      expect(() => {
        // @ts-expect-error: LocaleProxy is read-only.
        locale.airline = {};
      }).toThrowError(
        new FakerError('You cannot edit the locale data on the faker instance')
      );
    });

    it('should not be possible to delete a missing category', () => {
      expect(() => {
        // @ts-expect-error: LocaleProxy is read-only.
        delete locale.category;
      }).toThrowError(
        new FakerError('You cannot edit the locale data on the faker instance')
      );
    });

    it('should not be possible to delete an existing category', () => {
      expect(() => {
        // @ts-expect-error: LocaleProxy is read-only.
        delete locale.airline;
      }).toThrowError(
        new FakerError('You cannot edit the locale data on the faker instance')
      );
    });

    it('should be possible to get all categories keys on empty locale', () => {
      const empty = createLocaleProxy({});

      expect(Object.keys(empty)).toEqual([]);
    });

    it('should be possible to get all categories keys on actual locale', () => {
      expect(Object.keys(locale).sort()).toEqual(Object.keys(en).sort());
    });
  });

  describe('entry', () => {
    it('should be possible to check for a missing entry in a missing category', () => {
      expect('missing' in locale.category).toBe(false);
    });

    it('should be possible to check for a missing entry in a present category', () => {
      expect('missing' in locale.airline).toBe(false);
    });

    it('should be possible to check for a present entry', () => {
      expect('airline' in locale.airline).toBe(true);
    });

    it('should not be possible to access a missing entry in a missing category', () => {
      expect(() => locale.category.missing).toThrowError(
        new FakerError(
          `The locale data for 'category.missing' are missing in this locale.
  Please contribute the missing data to the project or use a locale/Faker instance that has these data.
  For more information see https://next.fakerjs.dev/guide/localization.html`
        )
      );
    });

    it('should not be possible to access a missing entry in a present category', () => {
      expect(() => locale.airline.missing).toThrowError(
        new FakerError(
          `The locale data for 'airline.missing' are missing in this locale.
  Please contribute the missing data to the project or use a locale/Faker instance that has these data.
  For more information see https://next.fakerjs.dev/guide/localization.html`
        )
      );
    });

    it('should be possible to access a present entry', () => {
      expect(locale.airline.airline).toBeDefined();
    });

    it('should not be possible to access an unavailable entry in a present category', () => {
      const unavailable = createLocaleProxy({
        airline: { airline: null },
      });

      expect(() => unavailable.airline.airline).toThrowError(
        new FakerError(
          `The locale data for 'airline.airline' aren't applicable to this locale.
  If you think this is a bug, please report it at: https://github.com/faker-js/faker`
        )
      );
    });

    it('should not be possible to add a new entry in a missing category', () => {
      expect(() => {
        // @ts-expect-error: LocaleProxy is read-only.
        locale.category.missing = {};
      }).toThrowError(
        new FakerError('You cannot edit the locale data on the faker instance')
      );
    });

    it('should not be possible to add a new entry in an existing category', () => {
      expect(() => {
        // @ts-expect-error: LocaleProxy is read-only.
        locale.airline.missing = {};
      }).toThrowError(
        new FakerError('You cannot edit the locale data on the faker instance')
      );
    });

    it('should not be possible to replace an entry in an existing category', () => {
      expect(() => {
        // @ts-expect-error: LocaleProxy is read-only.
        locale.airline.airline = ['dummy'];
      }).toThrowError(
        new FakerError('You cannot edit the locale data on the faker instance')
      );
    });

    it('should not be possible to delete a missing entry in a missing category', () => {
      expect(() => {
        // @ts-expect-error: LocaleProxy is read-only.
        delete locale.category.missing;
      }).toThrowError(
        new FakerError('You cannot edit the locale data on the faker instance')
      );
    });

    it('should not be possible to delete a missing entry in an existing category', () => {
      expect(() => {
        // @ts-expect-error: LocaleProxy is read-only.
        delete locale.airline.missing;
      }).toThrowError(
        new FakerError('You cannot edit the locale data on the faker instance')
      );
    });

    it('should not be possible to delete an existing entry in an existing category', () => {
      expect(() => {
        // @ts-expect-error: LocaleProxy is read-only.
        delete locale.airline.airline;
      }).toThrowError(
        new FakerError('You cannot edit the locale data on the faker instance')
      );
    });

    it('should be possible to get all keys from missing category', () => {
      expect(Object.keys(locale.missing)).toEqual([]);
    });

    it('should be possible to get all keys from existing category', () => {
      expect(Object.keys(locale.airline).sort()).toEqual(
        Object.keys(enAirline).sort()
      );
    });
  });
});