-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: deepMerge | remove duplicates when merging arrays (#325)
* fix: deepMerge | remove duplicates when merging arrays * test: add unit tests for depMeerge function
- Loading branch information
Showing
5 changed files
with
63 additions
and
47 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,44 +1,60 @@ | ||
// deepMerge.test.ts | ||
import deepMerge from '@utils/deepMerge' | ||
|
||
describe('deepMerge', () => { | ||
test('merge objects', () => { | ||
expect(deepMerge( | ||
{ | ||
id: 'not visible', | ||
type: 'unique', | ||
nested: { | ||
stay: 'still here', | ||
override: 'not here', | ||
merged: [2] | ||
} | ||
}, | ||
{ | ||
id: 'visible', | ||
nested: { | ||
override: 'new value', | ||
merged: ['test'] | ||
}, | ||
description: 'from second' | ||
} | ||
)).toStrictEqual( | ||
{ | ||
id: 'visible', | ||
type: 'unique', | ||
nested: { | ||
stay: 'still here', | ||
override: 'new value', | ||
merged: [2, 'test'] | ||
}, | ||
description: 'from second' | ||
} | ||
) | ||
}) | ||
|
||
test('argument 1 is string, return source', () => { | ||
expect(deepMerge('test', { value: 1 })).toStrictEqual({ value: 1 }) | ||
}) | ||
|
||
test('argument 2 is string, return source', () => { | ||
expect(deepMerge({ value: 1 }, 'test')).toStrictEqual('test') | ||
it('should merge two objects', () => { | ||
const target = { a: 1, b: 2 } | ||
const source = { b: 3, c: 4 } | ||
const result = deepMerge(target, source) | ||
expect(result).toEqual({ a: 1, b: 3, c: 4 }) | ||
}) | ||
|
||
it('should merge nested objects', () => { | ||
const target = { a: { b: 1 } } | ||
const source = { a: { c: 2 } } | ||
const result = deepMerge(target, source) | ||
expect(result).toEqual({ a: { b: 1, c: 2 } }) | ||
}) | ||
|
||
it('should merge arrays without duplicates', () => { | ||
const target = { a: [1, 2] } | ||
const source = { a: [2, 3] } | ||
const result = deepMerge(target, source) | ||
expect(result).toEqual({ a: [1, 2, 3] }) | ||
}) | ||
|
||
it('should overwrite non-object values', () => { | ||
const target = { a: 1 } | ||
const source = { a: 2 } | ||
const result = deepMerge(target, source) | ||
expect(result).toEqual({ a: 2 }) | ||
}) | ||
|
||
it('should return source if target is not an object', () => { | ||
const target = null | ||
const source = { a: 1 } | ||
const result = deepMerge(target, source) | ||
expect(result).toEqual({ a: 1 }) | ||
}) | ||
|
||
it('should return source if source is not an object', () => { | ||
const target = { a: 1 } | ||
const source = null | ||
const result = deepMerge(target, source) | ||
expect(result).toEqual(null) | ||
}) | ||
|
||
it('should handle empty objects', () => { | ||
const target = {} | ||
const source = {} | ||
const result = deepMerge(target, source) | ||
expect(result).toEqual({}) | ||
}) | ||
|
||
it('should handle empty arrays', () => { | ||
const target = { a: [] } | ||
const source = { a: [] } | ||
const result = deepMerge(target, source) | ||
expect(result).toEqual({ a: [] }) | ||
}) | ||
}) |