Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
- add script to extract faker.js locales
Browse files Browse the repository at this point in the history
- add and split tests
- update faker.js to latest version, make setSeed null safe
Closes #8
  • Loading branch information
Guillaume committed May 7, 2021
1 parent c5d081a commit 8047d1f
Show file tree
Hide file tree
Showing 7 changed files with 435 additions and 342 deletions.
56 changes: 28 additions & 28 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
"node": ">=10.0.0"
},
"dependencies": {
"@mockoon/commons": "2.1.0",
"@mockoon/commons": "2.1.1",
"bson-objectid": "1.3.1",
"cookie-parser": "1.4.5",
"date-fns": "2.16.1",
"express": "4.17.1",
"faker": "5.1.0",
"faker": "5.5.3",
"handlebars": "4.7.6",
"http-proxy-middleware": "1.0.6",
"killable": "1.0.1",
Expand All @@ -54,7 +54,7 @@
"@types/chai": "4.2.14",
"@types/cookie-parser": "1.4.2",
"@types/express": "4.17.8",
"@types/faker": "5.1.2",
"@types/faker": "5.5.4",
"@types/lodash": "4.14.161",
"@types/mime-types": "2.1.0",
"@types/mocha": "8.2.0",
Expand Down
17 changes: 17 additions & 0 deletions scripts/extract-faker-locales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fakerLocales = require('../node_modules/faker/lib/locales.js');

/**
* Extract locales list from Faker.js to be used in the commons librairy and main app
*/
const localesList = Object.keys(fakerLocales);
const locales = [];

localesList.forEach((locale) => {
locales.push({
code: locale,
label: fakerLocales[locale].title
});
});

console.log(localesList);
console.log(locales.sort((a, b) => (a.label < b.label ? -1 : 1)));
4 changes: 3 additions & 1 deletion src/libs/faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ export const SetFakerLocale = (locale: FakerAvailableLocales) => {
* @param seed
*/
export const SetFakerSeed = (seed: number) => {
faker.seed(seed);
if (seed !== undefined && seed !== null) {
faker.seed(seed);
}
};
70 changes: 70 additions & 0 deletions test/templating-helpers/faker-wrapper.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { expect } from 'chai';
import faker from 'faker';
import { TemplateParser } from '../../src/libs/template-parser';

faker.seed(1);

describe('Template parser: Faker wrapper', () => {
it('should return value for simple helper', () => {
const parseResult = TemplateParser(
"{{faker 'name.firstName'}}",
{} as any,
{} as any
);
expect(parseResult).to.be.equal('Hayden');
});

it('should return value for helper with named parameters', () => {
const parseResult = TemplateParser(
"{{faker 'random.number' min=10 max=20}}",
{} as any,
{} as any
);
expect(parseResult).to.be.equal('20');
});

it('should return value for helper with arguments', () => {
const parseResult = TemplateParser(
"{{faker 'random.alphaNumeric' 1}}",
{} as any,
{} as any
);
expect(parseResult).to.be.equal('p');
});

it('should throw if helper name is missing', () => {
expect(() => {
TemplateParser('{{faker}}', {} as any, {} as any);
}).to.throw(
'Faker method name is missing (valid: "address.zipCode", "date.past", etc) line 1'
);
});

it('should throw if helper name is malformed', () => {
expect(() => {
TemplateParser("{{faker 'random'}}", {} as any, {} as any);
}).to.throw(
'random is not a valid Faker method (valid: "address.zipCode", "date.past", etc) line 1'
);
});

it('should throw if helper name is malformed', () => {
expect(() => {
TemplateParser("{{faker 'random.'}}", {} as any, {} as any);
}).to.throw(
'random. is not a valid Faker method (valid: "address.zipCode", "date.past", etc) line 1'
);
});

it('should throw if helper name does not exists', () => {
expect(() => {
TemplateParser(
"{{faker 'donotexists.donotexists'}}",
{} as any,
{} as any
);
}).to.throw(
'donotexists.donotexists is not a valid Faker method (valid: "address.zipCode", "date.past", etc) line 1'
);
});
});
Loading

0 comments on commit 8047d1f

Please sign in to comment.