diff options
| author | pomali <[email protected]> | 2024-03-03 11:12:17 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-03-03 11:12:17 +0100 |
| commit | 2b15f2ee7eeba7147c75a24d71042ee996966c92 (patch) | |
| tree | 2812cdda5e77ae70cc5038ee659745fa130c22ac /test/modules | |
| parent | 9348138893bb95faa5037c653443fbd525ce2939 (diff) | |
| download | faker-2b15f2ee7eeba7147c75a24d71042ee996966c92.tar.xz faker-2b15f2ee7eeba7147c75a24d71042ee996966c92.zip | |
feat(helpers)!: stricter checking for function signature passed to `multiple` (#2563)
Co-authored-by: ST-DDT <[email protected]>
Co-authored-by: Matt Mayer <[email protected]>
Diffstat (limited to 'test/modules')
| -rw-r--r-- | test/modules/__snapshots__/helpers.spec.ts.snap | 24 | ||||
| -rw-r--r-- | test/modules/helpers.spec.ts | 41 |
2 files changed, 54 insertions, 11 deletions
diff --git a/test/modules/__snapshots__/helpers.spec.ts.snap b/test/modules/__snapshots__/helpers.spec.ts.snap index af17437a..8ecb47a8 100644 --- a/test/modules/__snapshots__/helpers.spec.ts.snap +++ b/test/modules/__snapshots__/helpers.spec.ts.snap @@ -98,6 +98,14 @@ exports[`helpers > 42 > multiple > with method and count range 1`] = ` ] `; +exports[`helpers > 42 > multiple > with method using index 1`] = ` +[ + 0, + 3, + 6, +] +`; + exports[`helpers > 42 > multiple > with only method 1`] = ` [ 3373557479352566, @@ -330,6 +338,14 @@ exports[`helpers > 1211 > multiple > with method and count range 1`] = ` ] `; +exports[`helpers > 1211 > multiple > with method using index 1`] = ` +[ + 0, + 3, + 6, +] +`; + exports[`helpers > 1211 > multiple > with only method 1`] = ` [ 8363366038243348, @@ -544,6 +560,14 @@ exports[`helpers > 1337 > multiple > with method and count range 1`] = ` ] `; +exports[`helpers > 1337 > multiple > with method using index 1`] = ` +[ + 0, + 3, + 6, +] +`; + exports[`helpers > 1337 > multiple > with only method 1`] = ` [ 2360108457524098, diff --git a/test/modules/helpers.spec.ts b/test/modules/helpers.spec.ts index 1ba43c70..5a2294bb 100644 --- a/test/modules/helpers.spec.ts +++ b/test/modules/helpers.spec.ts @@ -173,11 +173,14 @@ describe('helpers', () => { }); t.describe('multiple', (t) => { - t.it('with only method', faker.number.int) - .it('with method and count', faker.number.int, { count: 5 }) - .it('with method and count range', faker.number.int, { + t.it('with only method', () => faker.number.int()) + .it('with method and count', () => faker.number.int(), { + count: 5, + }) + .it('with method and count range', () => faker.number.int(), { count: { min: 1, max: 10 }, - }); + }) + .it('with method using index', (_, i) => i * 3); }); }); @@ -1144,30 +1147,46 @@ describe('helpers', () => { describe('multiple()', () => { it('should generate values from the function with a default length of 3', () => { - const result = faker.helpers.multiple(faker.person.firstName); + const result = faker.helpers.multiple(() => faker.person.firstName()); expect(result).toBeTypeOf('object'); expect(Array.isArray(result)).toBe(true); expect(result.length).toBe(3); }); it('should generate the given amount of values from the function', () => { - const result = faker.helpers.multiple(faker.person.firstName, { - count: 5, - }); + const result = faker.helpers.multiple( + () => faker.person.firstName(), + { + count: 5, + } + ); expect(result).toBeTypeOf('object'); expect(Array.isArray(result)).toBe(true); expect(result.length).toBe(5); }); it('should generate a ranged number of values from the function', () => { - const result = faker.helpers.multiple(faker.person.firstName, { - count: { min: 1, max: 10 }, - }); + const result = faker.helpers.multiple( + () => faker.person.firstName(), + { + count: { min: 1, max: 10 }, + } + ); expect(result).toBeTypeOf('object'); expect(Array.isArray(result)).toBe(true); expect(result.length).toBeGreaterThanOrEqual(1); expect(result.length).toBeLessThanOrEqual(10); }); + + it('should generate values using index of created value', () => { + const result = faker.helpers.multiple((_, i) => i * 2, { + count: 3, + }); + expect(result).toBeTypeOf('object'); + expect(Array.isArray(result)).toBe(true); + expect(result.length).toBe(3); + expect(result).toStrictEqual([0, 2, 4]); + }); }); } ); |
