Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue: add test for mergeDeep fail #4088

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions packages/core/src/utilities/mergeDeep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ export function mergeDeep(target: Record<string, any>, source: Record<string, an

if (isPlainObject(target) && isPlainObject(source)) {
Object.keys(source).forEach(key => {
if (isPlainObject(source[key])) {
if (!(key in target)) {
Object.assign(output, { [key]: source[key] })
} else {
output[key] = mergeDeep(target[key], source[key])
}
if (isPlainObject(source[key]) && isPlainObject(target[key])) {
output[key] = mergeDeep(target[key], source[key])
} else {
Object.assign(output, { [key]: source[key] })
output[key] = source[key]
}
})
}
Expand Down
45 changes: 45 additions & 0 deletions tests/cypress/integration/core/mergeDeep.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ describe('mergeDeep', () => {
expect(merged).to.deep.eq(result)
})

it('should merge when source has null value', () => {
const one = {
a: null,
}
const two = {
a: { c: 3 },
}
const result = {
a: { c: 3 },
}
const merged = mergeDeep(one, two)

expect(merged).to.deep.eq(result)
})

it('should not merge array', () => {
const one = {
a: [1],
Expand All @@ -34,6 +49,36 @@ describe('mergeDeep', () => {
expect(merged).to.deep.eq(result)
})

it('should merge when source has null value', () => {
const one = {
a: null,
}
const two = {
a: { c: 3 },
}
const result = {
a: { c: 3 },
}
const merged = mergeDeep(one, two)

expect(merged).to.deep.eq(result)
})

it('should allow nulling a value', () => {
const one = {
a: { c: 3 },
}
const two = {
a: { c: null },
}
const result = {
a: { c: null },
}
const merged = mergeDeep(one, two)

expect(merged).to.deep.eq(result)
})

it('should merge deep', () => {
const one = {
a: 1,
Expand Down