|
| 1 | +import { shuffle } from '../Shuffle.js' |
| 2 | + |
| 3 | +describe('shuffle', () => { |
| 4 | + test('shuffle returns array of same length', () => { |
| 5 | + expect(shuffle([1, 2, 3, 4, 5])).toHaveLength(5) |
| 6 | + }) |
| 7 | + |
| 8 | + test('shuffle contains all original elements', () => { |
| 9 | + const arr = [1, 2, 3, 4, 5] |
| 10 | + const result = shuffle(arr) |
| 11 | + expect(result.sort()).toEqual([...arr].sort()) |
| 12 | + }) |
| 13 | + |
| 14 | + test('shuffle empty array returns empty array', () => { |
| 15 | + expect(shuffle([])).toEqual([]) |
| 16 | + }) |
| 17 | + |
| 18 | + test('shuffle single element returns same array', () => { |
| 19 | + expect(shuffle([1])).toEqual([1]) |
| 20 | + }) |
| 21 | + |
| 22 | + test('shuffle does not mutate original array', () => { |
| 23 | + const arr = [1, 2, 3, 4, 5] |
| 24 | + shuffle(arr) |
| 25 | + expect(arr).toEqual([1, 2, 3, 4, 5]) |
| 26 | + }) |
| 27 | + |
| 28 | + test('shuffle works with strings', () => { |
| 29 | + const arr = ['a', 'b', 'c', 'd'] |
| 30 | + const result = shuffle(arr) |
| 31 | + expect(result.sort()).toEqual([...arr].sort()) |
| 32 | + }) |
| 33 | + |
| 34 | + test('shuffle returns a new array', () => { |
| 35 | + const arr = [1, 2, 3] |
| 36 | + const result = shuffle(arr) |
| 37 | + expect(result).not.toBe(arr) |
| 38 | + }) |
| 39 | + |
| 40 | + test('throws TypeError for non-array input', () => { |
| 41 | + expect(() => shuffle('123')).toThrow(TypeError) |
| 42 | + }) |
| 43 | + |
| 44 | + test('throws TypeError for null input', () => { |
| 45 | + expect(() => shuffle(null)).toThrow(TypeError) |
| 46 | + }) |
| 47 | +}) |
0 commit comments