-
-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: messages `deepCopy` mutates `src` arguments * fix: `deepCopy` should never merge arrays
- Loading branch information
1 parent
28a83ba
commit 19054e0
Showing
2 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { deepCopy } from '../src/index' | ||
|
||
test('deepCopy merges without mutating src argument', () => { | ||
const msg1 = { | ||
hello: 'Greetings', | ||
about: { | ||
title: 'About us' | ||
}, | ||
overwritten: 'Original text', | ||
fruit: [{ name: 'Apple' }] | ||
} | ||
const copy1 = structuredClone(msg1) | ||
|
||
const msg2 = { | ||
bye: 'Goodbye', | ||
about: { | ||
content: 'Some text' | ||
}, | ||
overwritten: 'New text', | ||
fruit: [{ name: 'Strawberry' }], | ||
// @ts-ignore | ||
car: ({ plural }) => plural(['car', 'cars']) | ||
} | ||
|
||
const merged = {} | ||
|
||
deepCopy(msg1, merged) | ||
deepCopy(msg2, merged) | ||
|
||
expect(merged).toMatchInlineSnapshot(` | ||
{ | ||
"about": { | ||
"content": "Some text", | ||
"title": "About us", | ||
}, | ||
"bye": "Goodbye", | ||
"car": [Function], | ||
"fruit": [ | ||
{ | ||
"name": "Strawberry", | ||
}, | ||
], | ||
"hello": "Greetings", | ||
"overwritten": "New text", | ||
} | ||
`) | ||
|
||
// should not mutate source object | ||
expect(msg1).toStrictEqual(copy1) | ||
}) |