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
Merged
3 changes: 2 additions & 1 deletion src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1135,11 +1135,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((_, i) => `Person ${i + 1}` ) // [ 'Person 1', 'Person 2', 'Person 3' ]
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
*
* @since 8.0.0
*/
multiple<const TResult>(
method: () => TResult,
method: (v: unknown, index: number) => TResult,
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`] = `
[
3373557479352566,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
41 changes: 30 additions & 11 deletions test/modules/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', (_el, i) => i * 3);
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
});
});

Expand Down Expand Up @@ -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((_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