-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ignore prototype methods when using setData on objects
- Loading branch information
Showing
2 changed files
with
130 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,105 @@ | ||
/** | ||
* @vitest-environment node | ||
*/ | ||
|
||
import { describe, expect, it } from 'vitest' | ||
import { mergeDeep } from '../src/utils' | ||
|
||
describe('utils', () => { | ||
it('should be possible to replace a primitive value with another', () => { | ||
// ARRANGE | ||
const state = { | ||
foo: 'bar' | ||
} | ||
|
||
// ACT | ||
const result = mergeDeep(state, { | ||
foo: true | ||
}) | ||
|
||
// ASSERT | ||
expect(result).toStrictEqual({ | ||
foo: true | ||
}) | ||
}) | ||
|
||
it('should be possible to merge a nested object', () => { | ||
// ARRANGE | ||
const state = { | ||
foo: { | ||
bar: 'baz' | ||
} | ||
} | ||
|
||
// ACT | ||
const result = mergeDeep(state, { | ||
foo: { | ||
bar: 'bar', | ||
foo: 'foo' | ||
} | ||
}) | ||
|
||
// ASSERT | ||
expect(result).toStrictEqual({ | ||
foo: { | ||
bar: 'bar', | ||
foo: 'foo' | ||
} | ||
}) | ||
}) | ||
|
||
it('should be possible to replace an array', () => { | ||
// ARRANGE | ||
const state = { | ||
foo: [] | ||
} | ||
|
||
// ACT | ||
const result = mergeDeep(state, { | ||
foo: ['bar', 'baz'] | ||
}) | ||
|
||
// ASSERT | ||
expect(result).toStrictEqual({ | ||
foo: ['bar', 'baz'] | ||
}) | ||
}) | ||
|
||
it('should be possible to add additional key', () => { | ||
// ARRANGE | ||
const state = { | ||
foo: 'bar' | ||
} | ||
|
||
// ACT | ||
const result = mergeDeep(state, { | ||
baz: 'foo' | ||
}) | ||
|
||
// ASSERT | ||
expect(result).toStrictEqual({ | ||
foo: 'bar', | ||
baz: 'foo' | ||
}) | ||
}) | ||
|
||
it('should result in the same value as before merging', () => { | ||
// ARRANGE | ||
const state = { | ||
foo: [], | ||
bar: [] | ||
} | ||
|
||
// ACT | ||
const result = mergeDeep(state, { | ||
foo: [], | ||
bar: [] | ||
}) | ||
|
||
// ASSERT | ||
expect(result).toStrictEqual({ | ||
foo: [], | ||
bar: [] | ||
}) | ||
}) | ||
}) |