Skip to content

Commit

Permalink
Merge pull request #152 from tomasz-szymanek/faker-locale-support
Browse files Browse the repository at this point in the history
feat: added faker locale support
  • Loading branch information
RobinCK authored Sep 16, 2020
2 parents d84fc12 + 2385ab9 commit 4d15352
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/interface/IFixture.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface IFixture {
parameters: { [key: string]: any };
processor?: string;
locale?: string;
entity: string;
name: string;
dependencies: string[];
Expand Down
1 change: 1 addition & 0 deletions src/interface/IFixturesConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface IFixturesConfig {
entity: string;
locale?: string;
parameters?: { [key: string]: any };
processor?: string;
items: { [key: string]: any };
Expand Down
9 changes: 7 additions & 2 deletions src/parsers/FakerParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as faker from 'faker';
import { IParser } from '../interface';
import { IFixture, IParser } from '../interface';

export class FakerParser implements IParser {
/**
Expand All @@ -17,9 +17,14 @@ export class FakerParser implements IParser {

/**
* @param {string} value
* @param {IFixture} fixture
* @return {any}
*/
parse(value: string): any {
parse(value: string, fixture?: IFixture): any {
if (fixture?.locale) {
// @ts-ignore
faker.locale = fixture.locale;
}
const result = faker.fake(value);

if ((+result).toString() === result) {
Expand Down
46 changes: 46 additions & 0 deletions src/schema/jFixturesSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,52 @@ import * as Joi from '@hapi/joi';

export const jFixturesSchema = Joi.object().keys({
entity: Joi.string().min(1).required(),
locale: Joi.string().valid(
'az',
'ar',
'cz',
'de',
'de_AT',
'de_CH',
'en',
'en_AU',
'en_AU_ock',
'en_BORK',
'en_CA',
'en_GB',
'en_IE',
'en_IND',
'en_US',
'en_Z',
'es',
'es_M',
'fa',
'fi',
'fr',
'fr_CA',
'fr_CH',
'ge',
'id_I',
'it',
'ja',
'ko',
'nb_NO',
'nep',
'nl',
'nl_B',
'pl',
'pt_BR',
'pt_P',
'ro',
'ru',
'sk',
'sv',
'tr',
'uk',
'vi',
'zh_CN',
'zh_TW',
),
parameters: Joi.object(),
processor: Joi.string(),
items: Joi.object().required(),
Expand Down
1 change: 1 addition & 0 deletions test/intergation/assets/fixtures/Comment.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
entity: Comment
locale: 'pl'
parameters: {}
items:
comment{1..10}:
Expand Down
1 change: 1 addition & 0 deletions test/intergation/assets/fixtures/Group.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
entity: Group
locale: 'pl'
parameters:
names:
admin: Admin
Expand Down
1 change: 1 addition & 0 deletions test/intergation/assets/fixtures/Post.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
entity: Post
locale: 'pl'
parameters: {}
items:
post1:
Expand Down
1 change: 1 addition & 0 deletions test/intergation/assets/fixtures/Profile.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
entity: Profile
locale: 'pl'
parameters: {}
items:
profile1:
Expand Down
1 change: 1 addition & 0 deletions test/intergation/assets/fixtures/User.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
entity: User
locale: 'pl'
parameters: {}
processor: ../processor/UserProcessor
items:
Expand Down
3 changes: 3 additions & 0 deletions test/unit/Loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('Loader', () => {
expect(configs).to.deep.equal([
{
entity: 'Post',
locale: 'pl',
processor: '',
items: {
post1: {
Expand All @@ -30,6 +31,7 @@ describe('Loader', () => {
},
{
entity: 'User',
locale: 'pl',
processor: '',
items: {
user1: {
Expand Down Expand Up @@ -58,6 +60,7 @@ describe('Loader', () => {
{
entity: 'Post',
processor: '',
locale: 'pl',
items: {
post1: {
title: '{{name.title}}',
Expand Down
1 change: 1 addition & 0 deletions test/unit/assets/fixtures/Post.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
entity: Post
locale: 'pl'
processor: '../processor/PostProcessor'
items:
post1:
Expand Down
1 change: 1 addition & 0 deletions test/unit/assets/fixtures/User.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"entity": "User",
"locale": "pl",
"processor": "/foo/boo",
"items": {
"user1": {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/parsers/FakerParser.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'mocha';
import { expect, assert } from 'chai';
import { FakerParser } from '../../../src/parsers';
import { IFixture } from '../../../src/interface';

describe('Faker parser', () => {
it('should be support', () => {
Expand Down Expand Up @@ -35,4 +36,13 @@ describe('Faker parser', () => {

assert.isString(result);
});

it('should be translated string', () => {
const parser = new FakerParser();
const result = parser.parse('{{random.word}}', {
locale: 'pl',
} as IFixture);

assert.isString(result);
});
});

0 comments on commit 4d15352

Please sign in to comment.