Skip to content

Commit

Permalink
Merge branch 'main' into refactor/image/deprecate-unsplash-avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
import-brain authored Jun 14, 2022
2 parents 4b74651 + 5ea8252 commit 3e43b48
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 40 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@
"@types/react": "~18.0.12",
"@types/sanitize-html": "~2.6.2",
"@types/validator": "~13.7.3",
"@typescript-eslint/eslint-plugin": "~5.27.1",
"@typescript-eslint/parser": "~5.27.1",
"@typescript-eslint/eslint-plugin": "~5.28.0",
"@typescript-eslint/parser": "~5.28.0",
"@vitest/ui": "~0.14.2",
"c8": "~7.11.3",
"conventional-changelog-cli": "~2.2.2",
Expand Down
72 changes: 36 additions & 36 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 3e43b48

Please sign in to comment.