Throw if root node is collapsed
Brian Vaughn committed
Apr 16, 2019 at 14:10 UTC
037bb0034c70d7b4d5be796b1db7042bfe68ab39
3 files changed
+24
-2
src/__tests__/__snapshots__/store-test.js.snap
+5
@@ -264,3 +264,8 @@ exports[`Store collapseNodesByDefault:true should support mount and update opera
264
`;
265
266
exports[`Store collapseNodesByDefault:true should support mount and update operations: 3: unmount 1`] = ``;
267
+
268
+exports[`Store should not allow a root node to be collapsed: 1: mount 1`] = `
269
+[root]
270
+ <Component>
271
+`;
src/__tests__/store-test.js
+14
-1
@@ -22,7 +22,20 @@ describe('Store', () => {
22
});
23
24
it('should not allow a root node to be collapsed', () => {
25
- // TODO
25
+ const Component = () => <div>Hi</div>;
26
+
27
+ act(() =>
28
+ ReactDOM.render(<Component count={4} />, document.createElement('div'))
29
+ );
30
+ expect(store).toMatchSnapshot('1: mount');
31
+
32
+ expect(store.roots).toHaveLength(1);
33
+
34
+ const rootID = store.roots[0];
35
+
36
+ expect(() => store.toggleIsCollapsed(rootID, true)).toThrow(
37
+ 'Root nodes cannot be collapsed'
38
+ );
39
});
40
41
describe('collapseNodesByDefault:false', () => {
src/devtools/store.js
+5
-1
@@ -452,6 +452,10 @@ export default class Store extends EventEmitter {
452
toggleIsCollapsed(id: number, isCollapsed: boolean): void {
453
const element = this.getElementByID(id);
454
if (element !== null) {
455
+ if (element.type === ElementTypeRoot) {
456
+ throw Error('Root nodes cannot be collapsed');
457
+ }
458
+
459
const oldWeight = element.isCollapsed ? 1 : element.weight;
460
element.isCollapsed = isCollapsed;
461
const newWeight = element.isCollapsed ? 1 : element.weight;
@@ -576,7 +580,7 @@ export default class Store extends EventEmitter {
580
depth: -1,
581
displayName: null,
582
id,
579
- isCollapsed: false, // Never collapse roots
583
+ isCollapsed: false, // Never collapse roots; it would hide the entire tree.
584
key: null,
585
ownerID: 0,
586
parentID: 0,