Skip to content

Commit 58e8eba

Browse files
committed
fix: 🐞 toCollection returns an array
BREAKING CHANGE: 🧨 toCollection now always returns an array βœ… Closes: #366
1 parent 449ee54 commit 58e8eba

File tree

2 files changed

+44
-92
lines changed

2 files changed

+44
-92
lines changed

β€Žpackages/falso/src/lib/collection.tsβ€Ž

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,21 @@ import { FakeOptions, fake } from './core/core';
55
*
66
* @category util
77
*
8-
* @example
9-
*
10-
* toCollection(() => {
11-
* return { data: randNumber(); }
12-
* })
138
*
149
* @example
1510
*
1611
* toCollection(() => {
1712
* return { data: randNumber(); }
18-
* }, { length: 10 }) // default is no length.
13+
* }, { length: 10 })
1914
*
2015
*/
21-
export function toCollection<
22-
Collection = never,
23-
Options extends FakeOptions = never
24-
>(
25-
generateCollection: (options?: Options) => Collection,
26-
options?: Options
27-
): Collection | Collection[] {
28-
return fake<Collection, Options>(
29-
() => generateCollection(options),
30-
options
31-
) as Collection | Collection[];
16+
17+
export function toCollection<Collection, Options extends FakeOptions = never>(
18+
generator: () => Collection,
19+
options: Options
20+
): Collection[] {
21+
return fake<Collection, Options>(() => generator(), {
22+
length: 5,
23+
...options,
24+
}) as Collection[];
3225
}
Lines changed: 34 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,45 @@
11
import { toCollection } from '../lib/collection';
2-
import { randNumber, random, seed } from '@ngneat/falso';
3-
import * as numberFunctions from '../lib/number';
4-
import Mock = jest.Mock;
5-
6-
interface fakeData {
7-
data: number;
8-
}
9-
10-
describe('randCollection', () => {
11-
it('should create', () => {
12-
expect(toCollection).toBeTruthy();
2+
import { randNumber } from '../lib/number';
3+
4+
describe('toCollection', () => {
5+
it('should return an array of 5 items by default', () => {
6+
const result = toCollection(() => ({ data: 1 }), {});
7+
expect(Array.isArray(result)).toBe(true);
8+
expect(result).toHaveLength(5);
9+
result.forEach((item) => expect(item).toEqual({ data: 1 }));
1310
});
1411

15-
it('should return one collection if length is not specified', () => {
16-
const expectedData: fakeData = { data: 1 };
17-
expect(toCollection<fakeData>(() => expectedData)).toEqual(expectedData);
12+
it('should return an array of custom length', () => {
13+
const result = toCollection(() => ({ data: 2 }), { length: 3 });
14+
expect(result).toHaveLength(3);
15+
result.forEach((item) => expect(item).toEqual({ data: 2 }));
1816
});
1917

20-
it('should return an array of one collection if length is specified to 1', () => {
21-
const expectedData: fakeData = { data: 1 };
22-
expect(toCollection<fakeData>(() => expectedData)).toEqual(expectedData);
23-
expect(
24-
toCollection(() => expectedData, {
25-
length: 1,
26-
})
27-
).toEqual([expectedData]);
18+
it('should call the generator function for each item and allow randomness', () => {
19+
let callCount = 0;
20+
const result = toCollection(
21+
() => {
22+
callCount++;
23+
return { data: randNumber({ min: 1, max: 1000 }) };
24+
},
25+
{ length: 4 }
26+
);
27+
expect(result).toHaveLength(4);
28+
expect(callCount).toBe(4);
29+
// Check that at least two items are different (high probability)
30+
const unique = new Set(result.map((r) => r.data));
31+
expect(unique.size).toBeGreaterThan(1);
2832
});
2933

30-
it('should return a collection of 2 elements if length=2 specified', () => {
31-
const expectedData: fakeData = { data: 1 };
32-
expect(
33-
toCollection(() => expectedData, {
34-
length: 2,
35-
})
36-
).toEqual([expectedData, expectedData]);
34+
it('should return an empty array if length is 0', () => {
35+
const result = toCollection(() => ({ data: 3 }), { length: 0 });
36+
expect(result).toEqual([]);
3737
});
3838

39-
describe('should call generators functions', () => {
40-
let generatorFunction: Mock;
41-
42-
beforeAll(() => {
43-
generatorFunction = jest.fn();
44-
generatorFunction.mockReturnValue({ data: 1 });
45-
});
46-
47-
afterEach(() => {
48-
jest.clearAllMocks();
49-
});
50-
51-
it('should return a collection of 2 elements that call another function', () => {
52-
const expectedData: fakeData = { data: 1 };
53-
expect(
54-
toCollection(generatorFunction, {
55-
length: 2,
56-
})
57-
).toEqual([{ data: 1 }, { data: 1 }]);
58-
expect(generatorFunction).toHaveBeenCalledTimes(2);
59-
expect(generatorFunction).toHaveBeenCalledWith({ length: 2 });
60-
});
61-
});
62-
describe('with call external random function', () => {
63-
let randNumberSpy: jest.SpyInstance;
64-
65-
beforeAll(() => {
66-
randNumberSpy = jest.spyOn(numberFunctions, 'randNumber');
67-
});
68-
69-
afterEach(() => {
70-
jest.clearAllMocks();
71-
});
72-
73-
it('should return a collection of 2 elements that call another function', () => {
74-
const expectedData: fakeData = { data: 1 };
75-
randNumberSpy.mockReturnValueOnce(1).mockReturnValueOnce(2);
76-
expect(
77-
toCollection(
78-
() => {
79-
return { data: randNumber() };
80-
},
81-
{ length: 2 }
82-
)
83-
).toEqual([{ data: 1 }, { data: 2 }]);
84-
});
39+
it('should call the generator function the correct number of times', () => {
40+
const mockGen = jest.fn().mockReturnValue({ data: 4 });
41+
const result = toCollection(mockGen, { length: 7 });
42+
expect(result).toHaveLength(7);
43+
expect(mockGen).toHaveBeenCalledTimes(7);
8544
});
8645
});

0 commit comments

Comments
Β (0)