|
1 | 1 | import { shuffleArray } from "../shuffleArray"; |
2 | 2 |
|
3 | 3 | describe("shuffleArray", () => { |
4 | | - test("should return an array with the same elements but in different order", () => { |
5 | | - const array = [1, 2, 3, 4, 5]; |
6 | | - const shuffledArray = shuffleArray([...array]); |
| 4 | + test("should shuffle array elements", () => { |
| 5 | + const originalArray = [1, 2, 3, 4, 5]; |
| 6 | + const shuffledArray = shuffleArray([...originalArray]); |
7 | 7 |
|
8 | | - expect(shuffledArray).toHaveLength(array.length); |
9 | | - expect(shuffledArray).toEqual(expect.arrayContaining(array)); |
10 | | - expect(array).toEqual(expect.arrayContaining(shuffledArray!)); |
| 8 | + expect(shuffledArray).toHaveLength(5); |
| 9 | + expect(shuffledArray).toEqual(expect.arrayContaining(originalArray)); |
| 10 | + }); |
11 | 11 |
|
12 | | - const isOrderDifferent = array.some( |
13 | | - (value, index) => value !== shuffledArray?.[index] |
14 | | - ); |
15 | | - expect(isOrderDifferent).toBe(true); |
| 12 | + test("should handle empty array", () => { |
| 13 | + const result = shuffleArray([]); |
| 14 | + expect(result).toEqual([]); |
16 | 15 | }); |
17 | 16 |
|
18 | | - test("should handle an empty array", () => { |
19 | | - const emptyArray: number[] = []; |
20 | | - const shuffledArray = shuffleArray(emptyArray); |
21 | | - expect(shuffledArray).toEqual([]); |
| 17 | + test("should handle single element array", () => { |
| 18 | + const result = shuffleArray([42]); |
| 19 | + expect(result).toEqual([42]); |
22 | 20 | }); |
23 | 21 |
|
24 | | - test("should handle an array with one element", () => { |
25 | | - const singleElementArray = [1]; |
26 | | - const shuffledArray = shuffleArray(singleElementArray); |
27 | | - expect(shuffledArray).toEqual([1]); |
| 22 | + test("should handle error cases gracefully", () => { |
| 23 | + // Mock console.error to avoid noise in tests |
| 24 | + const consoleSpy = jest |
| 25 | + .spyOn(console, "error") |
| 26 | + .mockImplementation(() => {}); |
| 27 | + |
| 28 | + // Test with null/undefined to trigger error handling |
| 29 | + expect(shuffleArray(null as any)).toBeUndefined(); |
| 30 | + expect(shuffleArray(undefined as any)).toBeUndefined(); |
| 31 | + |
| 32 | + expect(consoleSpy).toHaveBeenCalled(); |
| 33 | + consoleSpy.mockRestore(); |
28 | 34 | }); |
29 | 35 | }); |
0 commit comments