Skip to content

Commit

Permalink
feat: throw error on unknown locale (#1071)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Jun 14, 2022
1 parent e8985f6 commit 5ea8252
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,34 @@ const metadataKeys: ReadonlyArray<keyof LocaleDefinition> = [

export class Faker {
locales: UsedLocales;
locale: UsableLocale;
localeFallback: UsableLocale;
private _locale: UsableLocale;
private _localeFallback: UsableLocale;

get locale(): UsableLocale {
return this._locale;
}

set locale(locale: UsableLocale) {
if (!this.locales[locale]) {
throw new FakerError(
`Locale ${locale} is not supported. You might want to add the requested locale first to \`faker.locales\`.`
);
}
this._locale = locale;
}

get localeFallback(): UsableLocale {
return this._localeFallback;
}

set localeFallback(localeFallback: UsableLocale) {
if (!this.locales[localeFallback]) {
throw new FakerError(
`Locale ${localeFallback} is not supported. You might want to add the requested locale first to \`faker.locales\`.`
);
}
this._localeFallback = localeFallback;
}

readonly definitions: LocaleDefinition = this.initDefinitions();

Expand Down
18 changes: 18 additions & 0 deletions test/faker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ describe('faker', () => {
);
});

it('should throw error if locale is not known', () => {
const instance = new Faker({ locales: { en: { title: 'English' } } });
expect(() => (instance.locale = 'unknown')).toThrow(
new FakerError(
'Locale unknown is not supported. You might want to add the requested locale first to `faker.locales`.'
)
);
});

it('should throw error if localeFallback is not known', () => {
const instance = new Faker({ locales: { en: { title: 'English' } } });
expect(() => (instance.localeFallback = 'unknown')).toThrow(
new FakerError(
'Locale unknown is not supported. You might want to add the requested locale first to `faker.locales`.'
)
);
});

it('should not log anything on startup', () => {
const spies: Array<SpyInstance> = Object.keys(console)
.filter((key) => typeof console[key] === 'function')
Expand Down

0 comments on commit 5ea8252

Please sign in to comment.