-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove unnecessary safe or
- Loading branch information
1 parent
14ffb74
commit 8dcd203
Showing
2 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { shuffleArray } from '../../../src/utils/array' | ||
|
||
describe('shuffleArray', () => { | ||
it('should return an array with the same elements in a different order', () => { | ||
const originalArray = [1, 2, 3, 4, 5] | ||
const shuffledArray = shuffleArray([...originalArray]) | ||
|
||
expect(shuffledArray).toHaveLength(originalArray.length) | ||
expect(shuffledArray).not.toEqual(originalArray) | ||
expect(shuffledArray.sort()).toEqual(originalArray.sort()) | ||
}) | ||
|
||
it('should shuffle elements randomly (mocked Math.random)', () => { | ||
const originalArray = [1, 2, 3, 4, 5] | ||
|
||
// to control the shuffle | ||
jest | ||
.spyOn(global.Math, 'random') | ||
.mockReturnValueOnce(0.1) | ||
.mockReturnValueOnce(0.3) | ||
.mockReturnValueOnce(0.7) | ||
.mockReturnValueOnce(0.9) | ||
.mockReturnValueOnce(0.5) | ||
|
||
const shuffledArray = shuffleArray([...originalArray]) | ||
|
||
expect(shuffledArray).not.toEqual(originalArray) | ||
|
||
jest.restoreAllMocks() | ||
}) | ||
|
||
it('should not modify the original array', () => { | ||
const originalArray = [1, 2, 3, 4, 5] | ||
const arrayCopy = [...originalArray] | ||
|
||
shuffleArray(arrayCopy) | ||
|
||
expect(originalArray).toEqual([1, 2, 3, 4, 5]) | ||
}) | ||
|
||
it('should handle an empty array gracefully', () => { | ||
const emptyArray: number[] = [] | ||
const shuffledArray = shuffleArray(emptyArray) | ||
|
||
expect(shuffledArray).toEqual([]) | ||
}) | ||
|
||
it('should handle an array with one element gracefully', () => { | ||
const singleElementArray = [1] | ||
const shuffledArray = shuffleArray(singleElementArray) | ||
|
||
expect(shuffledArray).toEqual([1]) | ||
}) | ||
}) |