-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(js): support updating Element options
Before this fix, a call to update() like this: update({panelContainer: document.createElement('div')}) would throw the following error: TypeError: panelContainer.contains is not a function The reason lies in an incorrect object deep merge implementation where object entry values of Element type are merged like plain objects instead of being replaced, resulting in broken non-Element entries. This change fixes the issue with HTML Elements (Nodes of any kind to be more precise) but it might be worth considering replacing the current mergeDeep implementation with something more robust and future proof. Deep merging of objects surprisingly hard to get right, see the implementation in Lodash for example: https://github.com/lodash/lodash/blob/4.17.15/lodash.js#L3633
- Loading branch information
Showing
3 changed files
with
124 additions
and
2 deletions.
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
46 changes: 46 additions & 0 deletions
46
packages/autocomplete-js/src/utils/__tests__/mergeDeep.test.ts
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,46 @@ | ||
import { mergeDeep } from '../mergeDeep'; | ||
|
||
describe('mergeDeep', () => { | ||
test('arrays', () => { | ||
expect(mergeDeep({ key: ['test1'] }, { key: ['test2'] })).toEqual({ | ||
key: ['test1', 'test2'], | ||
}); | ||
}); | ||
|
||
test('plain objects', () => { | ||
expect( | ||
mergeDeep({ key: { test1: 'value1' } }, { key: { test2: 'value2' } }) | ||
).toEqual({ | ||
key: { test1: 'value1', test2: 'value2' }, | ||
}); | ||
}); | ||
|
||
test('HTML Elements', () => { | ||
expect( | ||
mergeDeep( | ||
{ key: document.createElement('div') }, | ||
{ key: document.createElement('span') } | ||
) | ||
).toEqual({ | ||
key: document.createElement('span'), | ||
}); | ||
}); | ||
|
||
test('primitives', () => { | ||
expect(mergeDeep({ key: 1 }, { key: 2 })).toEqual({ | ||
key: 2, | ||
}); | ||
}); | ||
|
||
test('null', () => { | ||
expect(mergeDeep({ key: 1 }, { key: null })).toEqual({ | ||
key: null, | ||
}); | ||
}); | ||
|
||
test('undefined', () => { | ||
expect(mergeDeep({ key: 1 }, { key: undefined })).toEqual({ | ||
key: undefined, | ||
}); | ||
}); | ||
}); |
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