@santi100/array-shuffle
Version:
Santi's Array Shuffling Library: Shuffle it up!
81 lines (71 loc) • 2.7 kB
JavaScript
// Generated by CodiumAI
describe('shuffle', () => {
const shuffle = require('..');
describe('error handling', () => {
it('should throw an error if array is not an array', () => {
expect(shuffle).toThrow(/must be an Array/);
expect(() => shuffle('not an Array')).toThrow(/must be an Array/);
});
it('should throw an error if opts.inPlace is not boolean', () => {
expect(() => shuffle([1, 2, 3, 4], { inPlace: 'not a boolean' })).toThrow(
/must be of type "boolean"/
);
});
});
// Tests that shuffle can shuffle an array of numbers
it('should shuffle an array of numbers', () => {
const array = [1, 2, 3, 4, 5];
const shuffledArray = shuffle(array);
expect(shuffledArray).not.toEqual(array);
expect(shuffledArray.sort()).toEqual(array.sort());
});
// Tests that shuffle can shuffle an array of strings
it('should shuffle an array of strings', () => {
const array = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
const shuffledArray = shuffle(array);
expect(shuffledArray).not.toEqual(array);
expect(shuffledArray.sort()).toEqual(array.sort());
});
// Tests that shuffle can shuffle an array of objects
it('should shuffle an array of objects', () => {
const array = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 },
{ name: 'David', age: 40 },
{ name: 'Eve', age: 45 },
];
const shuffledArray = shuffle(array);
expect(shuffledArray).not.toEqual(array);
expect(shuffledArray.sort((a, b) => a.age - b.age)).toEqual(
array.sort((a, b) => a.age - b.age)
);
});
// Tests that shuffle can shuffle an empty array
it('should return an empty array when given an empty array', () => {
const array = [];
const shuffledArray = shuffle(array);
expect(shuffledArray).toEqual([]);
});
// Tests that shuffle can shuffle an array with one element
it('should return the same array when given an array with one element', () => {
const array = [1];
const shuffledArray = shuffle(array);
expect(shuffledArray).toEqual(array);
});
// Tests that shuffle can shuffle an array with duplicate elements
it('should shuffle an array with duplicate elements', () => {
const array = [1, 2, 2, 3, 3, 3];
const shuffledArray = shuffle(array);
expect(shuffledArray).not.toEqual(array);
expect(shuffledArray.sort()).toEqual(array.sort());
});
// Tests that shuffle can shuffle in place
it('should shuffle an array of numbers in place', () => {
const array = [1, 2, 3, 4, 5];
const previousArray = [...array];
shuffle(array, { inPlace: true });
expect(previousArray).not.toEqual(array);
expect(previousArray.sort()).toEqual(array.sort());
});
});