Skip to content

Commit

Permalink
Merge pull request #1401 from storybooks/151-story-hierarchy
Browse files Browse the repository at this point in the history
Story Hierarchy - initial state bug fix
  • Loading branch information
shilman authored Jul 6, 2017
2 parents 03e10d0 + 3096cde commit d6177f8
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/ui/src/modules/ui/components/left_panel/stories_tree/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Treebeard } from 'storybook-react-treebeard';
import PropTypes from 'prop-types';
import React from 'react';
import deepEqual from 'deep-equal';
import treeNodeTypes from './tree_node_type';
import treeDecorators from './tree_decorators';
import treeStyle from './tree_style';
Expand Down Expand Up @@ -46,6 +47,22 @@ class Stories extends React.Component {
};
}

componentWillReceiveProps(nextProps) {
const { selectedHierarchy: nextSelectedHierarchy = [] } = nextProps;
const { selectedHierarchy: currentSelectedHierarchy = [] } = this.props;

if (!deepEqual(nextSelectedHierarchy, currentSelectedHierarchy)) {
const selectedNodes = getSelectedNodes(nextSelectedHierarchy);

this.setState(prevState => ({
nodes: {
...prevState.nodes,
...selectedNodes,
},
}));
}
}

onToggle(node, toggled) {
if (node.story) {
this.fireOnKindAndStory(node.kind, node.story);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,91 @@ describe('manager.ui.components.left_panel.stories', () => {
'some@namespace': true,
});
});

test('should recalculate selected nodes after selectedHierarchy changes', () => {
const data = createHierarchy(
[
{ kind: 'some.name.item1', stories: ['a1', 'a2'] },
{ kind: 'another.space.20', stories: ['b1', 'b2'] },
],
'\\.'
);
const wrap = mount(
<Stories
storiesHierarchy={data}
selectedKind="another.space.20"
selectedStory="b2"
selectedHierarchy={[]}
/>
);

wrap.setProps({ selectedHierarchy: ['another', 'space', '20'] });

const { nodes } = wrap.state();

expect(nodes).toEqual({
'another@namespace': true,
'another@space@namespace': true,
'another@space@20@component': true,
});
});

test('should add selected nodes to the state after selectedHierarchy changes with a new value', () => {
const data = createHierarchy(
[
{ kind: 'some.name.item1', stories: ['a1', 'a2'] },
{ kind: 'another.space.20', stories: ['b1', 'b2'] },
],
'\\.'
);
const wrap = mount(
<Stories
storiesHierarchy={data}
selectedKind="another.space.20"
selectedStory="b2"
selectedHierarchy={['another', 'space', '20']}
/>
);

wrap.setProps({ selectedHierarchy: ['some', 'name', 'item1'] });

const { nodes } = wrap.state();

expect(nodes).toEqual({
'another@namespace': true,
'another@space@namespace': true,
'another@space@20@component': true,
'some@namespace': true,
'some@name@namespace': true,
'some@name@item1@component': true,
});
});

test('should not call setState when selectedHierarchy prop changes with the same value', () => {
const selectedHierarchy = ['another', 'space', '20'];
const data = createHierarchy(
[
{ kind: 'some.name.item1', stories: ['a1', 'a2'] },
{ kind: 'another.space.20', stories: ['b1', 'b2'] },
],
'\\.'
);
const wrap = mount(
<Stories
storiesHierarchy={data}
selectedKind="another.space.20"
selectedStory="b2"
selectedHierarchy={selectedHierarchy}
/>
);

const setState = jest.fn();
wrap.instance().setState = setState;

wrap.setProps({ selectedHierarchy });

expect(setState).not.toHaveBeenCalled();
});
});

describe('events', () => {
Expand Down

0 comments on commit d6177f8

Please sign in to comment.