Skip to content

Commit

Permalink
updated tests to fix bugs with empty arrays not cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
nktnet committed Oct 12, 2023
1 parent c412424 commit 4a57bbc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const objectClone: CloneFn = <T>(obj: T): T => {
return obj;
}
if (Array.isArray(obj)) {
return obj.map(objectClone) as T;
// Empty array doesn't clone properly in Jest with just map
return (obj.length === 0 ? [] : [...obj.map(objectClone)]) as T;
}
const cloneObj: Record<string, any> = {};
for (const [key, value] of Object.entries(obj)) {
Expand Down
10 changes: 9 additions & 1 deletion tests/options/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ test('callback function upon success', () => {
jewire('../variables/variables', {
callback: (_, hE) => {
expect(hE.symbols.variables).toStrictEqual([
'numberFive', 'array', 'shallowObject', 'arrayOfObjects', 'deeplyNestedObject',
'numberFive',
'array',
'shallowObject',
'arrayOfObjects',
'deeplyNestedObject',
'nullVariable',
'emptyObject',
'emptyArray',
'undefinedVariable',
]);
}
});
Expand Down
8 changes: 8 additions & 0 deletions tests/variables/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ const deeplyNestedObject = {
},
}
};

const nullVariable = null;

const emptyObject = {};

const emptyArray = [];

const undefinedVariable = undefined;
23 changes: 23 additions & 0 deletions tests/variables/variables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ const {
shallowObject,
arrayOfObjects,
deeplyNestedObject,
nullVariable,
emptyObject,
emptyArray,
undefinedVariable,
} = jewire('./variables');

test('variables are imported correctly', () => {
expect(numberFive).toStrictEqual(5);
});

test('arrays are imported correctly', () => {
expect(array).toBeInstanceOf(Array);
expect(array).toStrictEqual([1, 2, 3]);
});

Expand All @@ -35,3 +40,21 @@ test('nested object passes strict equality', () => {
test('Array of Objects', () => {
expect(arrayOfObjects).toStrictEqual([{ name: 'Tam', age: 22 }, { name: 'Spam', age: 23 }]);
});

test('Null variable', () => {
expect(nullVariable).toStrictEqual(null);
});

test('Empty object', () => {
expect(emptyObject).toBeInstanceOf(Object);
expect(emptyObject).toStrictEqual({});
});

test('Empty array', () => {
expect(emptyArray).toBeInstanceOf(Array);
expect(emptyArray).toStrictEqual([]);
});

test('Undefined variable', () => {
expect(undefinedVariable).toStrictEqual(undefined);
});

0 comments on commit 4a57bbc

Please sign in to comment.