-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
58 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
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,30 @@ | ||
import shuffleElements from './shuffleElements'; | ||
|
||
const originalSlides = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }]; | ||
|
||
describe('shuffleElement', () => { | ||
it('returns a new array with the same elements but in a different order', () => { | ||
const shuffled = shuffleElements(originalSlides); | ||
|
||
// Check that the shuffled array is not the same reference as the original | ||
expect(shuffled).not.toBe(originalSlides); | ||
|
||
// Check that the shuffled array has the same elements as the original | ||
expect(shuffled).toEqual(expect.arrayContaining(originalSlides)); | ||
expect(originalSlides).toEqual(expect.arrayContaining(shuffled)); | ||
|
||
// Verify that at least one element is in a different position to ensure it's shuffled | ||
const isShuffled = originalSlides.some( | ||
(slide, index) => slide.id !== shuffled[index].id | ||
); | ||
expect(isShuffled).toBe(true); | ||
}); | ||
|
||
it('does not mutate the original array', () => { | ||
const originalSlidesCopy = [...originalSlides]; | ||
shuffleElements(originalSlides); | ||
|
||
// Verify that the original array remains unchanged | ||
expect(originalSlides).toEqual(originalSlidesCopy); | ||
}); | ||
}); |
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,10 @@ | ||
const shuffleElements = (slides: any[]) => { | ||
const slidesCopy = [...slides]; // Create a copy to avoid mutating the original array | ||
for (let i = slidesCopy.length - 1; i > 0; i--) { | ||
const j = Math.floor(Math.random() * (i + 1)); | ||
[slidesCopy[i], slidesCopy[j]] = [slidesCopy[j], slidesCopy[i]]; | ||
} | ||
return slidesCopy; | ||
}; | ||
|
||
export default shuffleElements; |