Throw if root node is collapsed

This commit is contained in:
Brian Vaughn
2019-04-16 14:10:06 -07:00
parent 4b64d7c017
commit 037bb0034c
3 changed files with 24 additions and 2 deletions
@@ -264,3 +264,8 @@ exports[`Store collapseNodesByDefault:true should support mount and update opera
`;
exports[`Store collapseNodesByDefault:true should support mount and update operations: 3: unmount 1`] = ``;
exports[`Store should not allow a root node to be collapsed: 1: mount 1`] = `
[root]
<Component>
`;
+14 -1
View File
@@ -22,7 +22,20 @@ describe('Store', () => {
});
it('should not allow a root node to be collapsed', () => {
// TODO
const Component = () => <div>Hi</div>;
act(() =>
ReactDOM.render(<Component count={4} />, document.createElement('div'))
);
expect(store).toMatchSnapshot('1: mount');
expect(store.roots).toHaveLength(1);
const rootID = store.roots[0];
expect(() => store.toggleIsCollapsed(rootID, true)).toThrow(
'Root nodes cannot be collapsed'
);
});
describe('collapseNodesByDefault:false', () => {
+5 -1
View File
@@ -452,6 +452,10 @@ export default class Store extends EventEmitter {
toggleIsCollapsed(id: number, isCollapsed: boolean): void {
const element = this.getElementByID(id);
if (element !== null) {
if (element.type === ElementTypeRoot) {
throw Error('Root nodes cannot be collapsed');
}
const oldWeight = element.isCollapsed ? 1 : element.weight;
element.isCollapsed = isCollapsed;
const newWeight = element.isCollapsed ? 1 : element.weight;
@@ -576,7 +580,7 @@ export default class Store extends EventEmitter {
depth: -1,
displayName: null,
id,
isCollapsed: false, // Never collapse roots
isCollapsed: false, // Never collapse roots; it would hide the entire tree.
key: null,
ownerID: 0,
parentID: 0,