Skip to content

Commit

Permalink
added some tests, cover 100% again
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandr-dukhovnyy committed Oct 29, 2023
1 parent 8e85637 commit afeefe2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 21 deletions.
14 changes: 6 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,12 @@ export const deepClone = <T>(item: T): T => {
const res: Record<string, unknown> = {};

for (const key in item) {
if (Object.prototype.hasOwnProperty.call(item, key)) {
if (item[key] instanceof RegExp) {
res[key] = item[key];
} else if (typeof item[key] === 'object') {
res[key] = deepClone(item[key]);
} else {
res[key] = item[key];
}
if (item[key] instanceof RegExp) {
res[key] = item[key];
} else if (typeof item[key] === 'object') {
res[key] = deepClone(item[key]);
} else {
res[key] = item[key];
}
}

Expand Down
92 changes: 79 additions & 13 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,30 @@ describe('utils', () => {
);
});

test('[deepClone]', () => {
delete globalThis.structuredClone;

const obj = {
data: {
deepData: {
key: 'value',
},
},
};
// test('[deepClone]', () => {
// delete globalThis.structuredClone;

const clonedObj = utils.deepClone(obj);
// const obj = {
// data: {
// deepData: {
// key: 'value',
// },
// },
// };

expect(obj.data.deepData === clonedObj.data.deepData).toEqual(false);
});
// const clonedObj = utils.deepClone(obj);

// expect(obj.data.deepData === clonedObj.data.deepData).toEqual(false);
// });

// test('[deepClone]', () => {
// delete globalThis.structuredClone;

// const val = 5;
// const copiedVal = utils.deepClone(val);

// expect(val === copiedVal).toEqual(true);
// });

test('[removeComments]', () => {
const node = new DOMParser().parseFromString(
Expand All @@ -157,3 +166,60 @@ describe('utils', () => {
expect(utils.removeComments({})).toEqual(false);
});
});

describe('utils.deepClone', () => {
it('should return the same string', () => {
const input = 'Hello, world!';
const cloned = utils.deepClone(input);
expect(cloned).toBe(input);
});

it('should clone an array', () => {
const input = [1, 2, [3, 4]];
const cloned = utils.deepClone(input);
expect(cloned).toEqual(input);
expect(cloned).not.toBe(input);
});

it('should clone an object', () => {
const input = { name: 'John', age: 30 };
const cloned = utils.deepClone(input);
expect(cloned).toEqual(input);
expect(cloned).not.toBe(input);
});

it('should handle RegExp objects', () => {
const input = { pattern: /test/i };
const cloned = utils.deepClone(input);
expect(cloned).toEqual(input);
expect(cloned).not.toBe(input);
});

it('should handle nested objects and arrays', () => {
const input = { numbers: [1, 2, 3], person: { name: 'Alice' } };
const cloned = utils.deepClone(input);
expect(cloned).toEqual(input);
expect(cloned).not.toBe(input);
});

it('should return the same value for null or other types', () => {
const input = null;
const cloned = utils.deepClone(input);
expect(cloned).toBe(input);

const numberInput = 42;
const numberCloned = utils.deepClone(numberInput);
expect(numberCloned).toBe(numberInput);
});

it('should handle object properties with hasOwnProperty check', () => {
const input = { name: 'John', age: 30 };
const cloned = utils.deepClone(input);

for (const key in cloned) {
if (Object.prototype.hasOwnProperty.call(cloned, key)) {
expect(cloned[key]).toEqual(input[key]);
}
}
});
});

0 comments on commit afeefe2

Please sign in to comment.