aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorST-DDT <[email protected]>2022-12-23 20:42:23 +0100
committerGitHub <[email protected]>2022-12-23 20:42:23 +0100
commit75a31f620c880413c05f012f1924b2ad89fb950c (patch)
tree4f1c7fdb26d22962f98ae191f6364689193d8155 /test
parent4ce8e98fcc19d99bf6df3abb3e24c4667f586076 (diff)
downloadfaker-75a31f620c880413c05f012f1924b2ad89fb950c.tar.xz
faker-75a31f620c880413c05f012f1924b2ad89fb950c.zip
feat(helpers): fake from array (#1453)
Diffstat (limited to 'test')
-rw-r--r--test/__snapshots__/helpers.spec.ts.snap24
-rw-r--r--test/helpers.spec.ts42
2 files changed, 51 insertions, 15 deletions
diff --git a/test/__snapshots__/helpers.spec.ts.snap b/test/__snapshots__/helpers.spec.ts.snap
index 06e0bc43..4ce31832 100644
--- a/test/__snapshots__/helpers.spec.ts.snap
+++ b/test/__snapshots__/helpers.spec.ts.snap
@@ -29,9 +29,13 @@ exports[`helpers > 42 > arrayElements > with array and count 1`] = `
]
`;
-exports[`helpers > 42 > fake > with args 1`] = `"my string: Cky2eiXX/J"`;
+exports[`helpers > 42 > fake > with a dynamic template 1`] = `"my string: Cky2eiXX/J"`;
-exports[`helpers > 42 > fake > with plain string 1`] = `"my test string"`;
+exports[`helpers > 42 > fake > with a static template 1`] = `"my test string"`;
+
+exports[`helpers > 42 > fake > with multiple dynamic templates 1`] = `"Sandy"`;
+
+exports[`helpers > 42 > fake > with multiple static templates 1`] = `"B"`;
exports[`helpers > 42 > maybe > with only value 1`] = `"Hello World!"`;
@@ -208,9 +212,13 @@ exports[`helpers > 1211 > arrayElements > with array and count 1`] = `
]
`;
-exports[`helpers > 1211 > fake > with args 1`] = `"my string: wKti5-}$_/"`;
+exports[`helpers > 1211 > fake > with a dynamic template 1`] = `"my string: wKti5-}$_/"`;
+
+exports[`helpers > 1211 > fake > with a static template 1`] = `"my test string"`;
-exports[`helpers > 1211 > fake > with plain string 1`] = `"my test string"`;
+exports[`helpers > 1211 > fake > with multiple dynamic templates 1`] = `"La Crosse"`;
+
+exports[`helpers > 1211 > fake > with multiple static templates 1`] = `"C"`;
exports[`helpers > 1211 > maybe > with only value 1`] = `undefined`;
@@ -383,9 +391,13 @@ exports[`helpers > 1337 > arrayElements > with array and count 1`] = `
]
`;
-exports[`helpers > 1337 > fake > with args 1`] = `"my string: 9U/4:SK$>6"`;
+exports[`helpers > 1337 > fake > with a dynamic template 1`] = `"my string: 9U/4:SK$>6"`;
+
+exports[`helpers > 1337 > fake > with a static template 1`] = `"my test string"`;
+
+exports[`helpers > 1337 > fake > with multiple dynamic templates 1`] = `"U/4:SK$>6Q"`;
-exports[`helpers > 1337 > fake > with plain string 1`] = `"my test string"`;
+exports[`helpers > 1337 > fake > with multiple static templates 1`] = `"A"`;
exports[`helpers > 1337 > maybe > with only value 1`] = `"Hello World!"`;
diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts
index 5829ddd8..37a1bd02 100644
--- a/test/helpers.spec.ts
+++ b/test/helpers.spec.ts
@@ -96,10 +96,14 @@ describe('helpers', () => {
});
t.describe('fake', (t) => {
- t.it('with plain string', 'my test string').it(
- 'with args',
- 'my string: {{datatype.string}}'
- );
+ t.it('with a static template', 'my test string')
+ .it('with a dynamic template', 'my string: {{string.sample}}')
+ .it('with multiple static templates', ['A', 'B', 'C'])
+ .it('with multiple dynamic templates', [
+ '{{string.sample}}',
+ '{{location.city_name}}',
+ '{{location.cityName}}',
+ ]);
});
t.describe('rangeToNumber', (t) => {
@@ -598,11 +602,16 @@ describe('helpers', () => {
expect(actual).toMatch(/^\d{5}$/);
});
- it('does not allow undefined parameters', () => {
- expect(() =>
- // @ts-expect-error: The parameter is required
- faker.helpers.fake()
- ).toThrowError(new FakerError('string parameter is required!'));
+ it('does not allow empty string parameters', () => {
+ expect(() => faker.helpers.fake('')).toThrowError(
+ new FakerError('Pattern string cannot be empty.')
+ );
+ });
+
+ it('does not allow empty array parameters', () => {
+ expect(() => faker.helpers.fake([])).toThrowError(
+ new FakerError('Array of pattern strings cannot be empty.')
+ );
});
it('does not allow invalid module name', () => {
@@ -655,6 +664,21 @@ describe('helpers', () => {
);
});
+ it('should be able to pass multiple static templates', () => {
+ expect(['A', 'B', 'C']).toContain(
+ faker.helpers.fake(['A', 'B', 'C'])
+ );
+ });
+
+ it('should be able to pass multiple dynamic templates', () => {
+ expect(faker.definitions.location.city_name).toContain(
+ faker.helpers.fake([
+ '{{location.city_name}}',
+ '{{location.cityName}}',
+ ])
+ );
+ });
+
it('should be able to handle only {{ brackets', () => {
expect(faker.helpers.fake('{{hello')).toBe('{{hello');
expect(faker.helpers.fake('hello{{')).toBe('hello{{');