From fd06dce262b79f13f3fbfceba988bce11103f0c7 Mon Sep 17 00:00:00 2001 From: Guy Elsmore-Paddock Date: Sun, 24 Nov 2019 02:40:18 -0500 Subject: [PATCH] [#171] Allow Parent Nodes to Have an Empty `children` Array In computer science terms, a node without any children is a "leaf". But in practical use, it's common to run into structures (e.g. empty folders in file systems) where a node is still a parent because it does not _yet_ have children but has the capacity to have children in the future. Closes #171. --- src/js/NodeModel.js | 2 +- test/CheckboxTree.js | 50 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/js/NodeModel.js b/src/js/NodeModel.js index 8058f336..6d58e691 100644 --- a/src/js/NodeModel.js +++ b/src/js/NodeModel.js @@ -52,7 +52,7 @@ class NodeModel { } nodeHasChildren(node) { - return Array.isArray(node.children) && node.children.length > 0; + return Array.isArray(node.children); } getDisabledState(node, parent, disabledProp, noCascade) { diff --git a/test/CheckboxTree.js b/test/CheckboxTree.js index 17ba31a2..c9628fe9 100644 --- a/test/CheckboxTree.js +++ b/test/CheckboxTree.js @@ -181,6 +181,56 @@ describe('', () => { { children: [null, null] }, ); }); + + it('should render a node with no "children" array as a leaf', () => { + const wrapper = shallow( + , + ); + + assert.equal(false, wrapper.find(TreeNode).prop('isParent')); + assert.equal(true, wrapper.find(TreeNode).prop('isLeaf')); + }); + + it('should render a node with an empty "children" array as a parent', () => { + const wrapper = shallow( + , + ); + + assert.equal(true, wrapper.find(TreeNode).prop('isParent')); + assert.equal(false, wrapper.find(TreeNode).prop('isLeaf')); + }); + + it('should render a node with a non-empty "children" array as a parent', () => { + const wrapper = shallow( + , + ); + + assert.equal(true, wrapper.find(TreeNode).prop('isParent')); + assert.equal(false, wrapper.find(TreeNode).prop('isLeaf')); + }); }); describe('noCascade', () => {