1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-06 01:15:28 +02:00
unleash.unleash/frontend/src/utils/reorderObject.test.ts
2023-12-19 15:35:39 +01:00

41 lines
1.5 KiB
TypeScript

import { reorderObject } from './reorderObject';
describe('reorderObject', () => {
it('correctly reorders the object based on provided keys', () => {
const myObj = { a: 1, b: 2, c: 3, d: 4 };
const order = ['b', 'a'];
const result = reorderObject(myObj, order);
const expected = { b: 2, a: 1, c: 3, d: 4 };
expect(result).toEqual(expected);
});
it('ignores non-existent keys in the order array', () => {
const myObj = { a: 1, b: 2, c: 3 };
const order = ['c', 'z', 'a']; // 'z' does not exist in myObj
const result = reorderObject(myObj, order);
const expected = { c: 3, a: 1, b: 2 };
expect(result).toEqual(expected);
});
it('returns the original object when order array is empty', () => {
const myObj = { a: 1, b: 2, c: 3 };
const order: string[] = [];
const result = reorderObject(myObj, order);
expect(result).toEqual(myObj);
});
it('returns the object with the same order when order array contains all object keys', () => {
const myObj = { a: 1, b: 2, c: 3 };
const order = ['a', 'b', 'c'];
const result = reorderObject(myObj, order);
expect(result).toEqual(myObj);
});
it('does not modify the original object', () => {
const myObj = { a: 1, b: 2, c: 3 };
const order = ['b', 'a'];
const result = reorderObject(myObj, order);
expect(myObj).toEqual({ a: 1, b: 2, c: 3 }); // myObj should remain unchanged
});
});