Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(helpers)!: stricter checking for function signature passed to multiple #2563

Merged
merged 9 commits into from
Mar 3, 2024
3 changes: 2 additions & 1 deletion src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1246,11 +1246,12 @@ export class SimpleHelpersModule extends SimpleModuleBase {
* @example
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
* faker.helpers.multiple(faker.person.firstName) // [ 'Aniya', 'Norval', 'Dallin' ]
* faker.helpers.multiple(faker.person.firstName, { count: 3 }) // [ 'Santos', 'Lavinia', 'Lavinia' ]
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
* faker.helpers.multiple((_el, i) => `Person ${i + 1}` ) // [ 'Person 1', 'Person 2', 'Person 3' ]
*
* @since 8.0.0
*/
multiple<TResult>(
method: () => TResult,
method: (() => TResult) | ((v: unknown, k: number) => TResult),
pomali marked this conversation as resolved.
Show resolved Hide resolved
options: {
/**
* The number or range of elements to generate.
Expand Down
24 changes: 24 additions & 0 deletions test/modules/__snapshots__/helpers.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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`] = `
[
37454,
Expand Down Expand Up @@ -338,6 +346,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`] = `
[
92852,
Expand Down Expand Up @@ -560,6 +576,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`] = `
[
26202,
Expand Down
13 changes: 12 additions & 1 deletion test/modules/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ describe('helpers', () => {
.it('with method and count', faker.datatype.number, { count: 5 })
.it('with method and count range', faker.datatype.number, {
count: { min: 1, max: 10 },
});
})
.it('with method using index', (_el, i) => i * 3);
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
});
});

Expand Down Expand Up @@ -1250,6 +1251,16 @@ Try adjusting maxTime or maxRetries parameters for faker.helpers.unique().`)
expect(result.length).toBeGreaterThanOrEqual(1);
expect(result.length).toBeLessThanOrEqual(10);
});

it('should generate values using index of created value', () => {
const result = faker.helpers.multiple((_el, i) => i * 2, {
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
count: 3,
});
expect(result).toBeTypeOf('object');
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(3);
expect(result).toStrictEqual([0, 2, 4]);
});
});
}
);
Expand Down
Loading