|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { renderTimes } from './renderTimes.ts'; |
| 4 | + |
| 5 | +describe('renderTimes', () => { |
| 6 | + it('should render components the specified number of times', () => { |
| 7 | + const result = renderTimes(3, index => `Item ${index}`); |
| 8 | + |
| 9 | + expect(result).toHaveLength(3); |
| 10 | + expect(result).toEqual(['Item 0', 'Item 1', 'Item 2']); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should return empty array when count is 0', () => { |
| 14 | + const result = renderTimes(0, index => `Item ${index}`); |
| 15 | + |
| 16 | + expect(result).toHaveLength(0); |
| 17 | + expect(result).toEqual([]); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should handle negative count by returning empty array', () => { |
| 21 | + const result = renderTimes(-1, index => `Item ${index}`); |
| 22 | + |
| 23 | + expect(result).toHaveLength(0); |
| 24 | + expect(result).toEqual([]); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should call renderFunction with correct index for each iteration', () => { |
| 28 | + const indices: number[] = []; |
| 29 | + renderTimes(4, index => { |
| 30 | + indices.push(index); |
| 31 | + return index; |
| 32 | + }); |
| 33 | + |
| 34 | + expect(indices).toEqual([0, 1, 2, 3]); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should work with React components', () => { |
| 38 | + const result = renderTimes(2, index => ({ |
| 39 | + type: 'div', |
| 40 | + key: index.toString(), |
| 41 | + props: { children: `Component ${index}` }, |
| 42 | + })); |
| 43 | + |
| 44 | + expect(result).toHaveLength(2); |
| 45 | + expect(result[0]).toEqual({ |
| 46 | + type: 'div', |
| 47 | + key: '0', |
| 48 | + props: { |
| 49 | + children: 'Component 0', |
| 50 | + }, |
| 51 | + }); |
| 52 | + expect(result[1]).toEqual({ |
| 53 | + type: 'div', |
| 54 | + key: '1', |
| 55 | + props: { |
| 56 | + children: 'Component 1', |
| 57 | + }, |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should work with different return types', () => { |
| 62 | + const numberResult = renderTimes(3, index => index * 2); |
| 63 | + |
| 64 | + expect(numberResult).toEqual([0, 2, 4]); |
| 65 | + |
| 66 | + const booleanResult = renderTimes(2, index => index % 2 === 0); |
| 67 | + |
| 68 | + expect(booleanResult).toEqual([true, false]); |
| 69 | + |
| 70 | + const nullResult = renderTimes(2, () => null); |
| 71 | + |
| 72 | + expect(nullResult).toEqual([null, null]); |
| 73 | + }); |
| 74 | +}); |
0 commit comments