|
1 | 1 | 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 })); |
13 | 10 | }); |
14 | 11 |
|
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 })); |
18 | 16 | }); |
19 | 17 |
|
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); |
28 | 32 | }); |
29 | 33 |
|
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([]); |
37 | 37 | }); |
38 | 38 |
|
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); |
85 | 44 | }); |
86 | 45 | }); |
0 commit comments