-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dashboard): prevent initialState from being shared across dashboa…
…rd instances
- Loading branch information
Showing
5 changed files
with
82 additions
and
3 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
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,25 @@ | ||
import merge from 'lodash/merge'; | ||
import { initialState } from './state'; | ||
|
||
it('does not modify initialState', () => { | ||
const illegalModification = () => { | ||
merge(initialState, { | ||
dashboardConfiguration: { | ||
widgets: [ | ||
{ | ||
id: 'test-widget', | ||
type: 'test', | ||
x: 0, | ||
y: 0, | ||
z: 1, | ||
height: 10, | ||
width: 10, | ||
properties: {}, | ||
}, | ||
], | ||
}, | ||
}); | ||
}; | ||
|
||
expect(illegalModification).toThrowError(); | ||
}); |
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,29 @@ | ||
import { deepFreeze } from './deepFreeze'; | ||
|
||
it('prevents extensions of an object', () => { | ||
const obj = deepFreeze({}); | ||
|
||
const illegalExtension = () => { | ||
/** | ||
* Should prevent extension even if you try to | ||
* circumvent the type restrictions | ||
*/ | ||
(obj as any)['test'] = 1; // eslint-disable-line | ||
}; | ||
|
||
expect(illegalExtension).toThrowError(); | ||
}); | ||
|
||
it('prevents modifications of an object', () => { | ||
const obj = deepFreeze({ | ||
nestedObject: { | ||
value: 1, | ||
}, | ||
}); | ||
|
||
const illegalModification = () => { | ||
obj.nestedObject.value = 2; | ||
}; | ||
|
||
expect(illegalModification).toThrowError(); | ||
}); |
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,9 @@ | ||
export const deepFreeze = <T extends object>(obj: T) => { | ||
for (const key in obj) { | ||
const value = obj[key]; | ||
if (value && typeof value === 'object' && !Object.isFrozen(value)) { | ||
deepFreeze(value); | ||
} | ||
} | ||
return Object.freeze(obj); | ||
}; |