Skip to content

Commit

Permalink
Merge pull request #5236 from storybooks/5227-fix-treeview-cases
Browse files Browse the repository at this point in the history
FIX issue with accidentally thinking things are roots
  • Loading branch information
shilman authored Jan 14, 2019
2 parents a3b2454 + 043e3b5 commit 48a49d1
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/ui/src/core/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ const initStoriesApi = ({
parent,
depth: index,
isComponent: index === original.length - 1,
isRoot: index === 0,
isRoot: !!root && index === 0,
},
]);
}, []);
Expand Down
100 changes: 92 additions & 8 deletions lib/ui/src/core/stories.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ describe('stories API', () => {
viewMode: 'components',
});
});

// A hash of stories as it will come over the channel
const parameters = { options: { hierarchyRootSeparator: '|', hierarchySeparator: '/' } };
const storiesHash = {
'a--1': { kind: 'a', name: '1', parameters: { options: {} }, path: 'a--1', id: 'a--1' },
'a--2': { kind: 'a', name: '2', parameters: { options: {} }, path: 'a--2', id: 'a--2' },
'b-c--1': { kind: 'b/c', name: '1', parameters: { options: {} }, path: 'b-c--1', id: 'b-c--1' },
'a--1': { kind: 'a', name: '1', parameters, path: 'a--1', id: 'a--1' },
'a--2': { kind: 'a', name: '2', parameters, path: 'a--2', id: 'a--2' },
'b-c--1': {
kind: 'b/c',
name: '1',
parameters,
path: 'b-c--1',
id: 'b-c--1',
},
};

describe('setStories', () => {
it('stores basic kinds and stories w/ correct keys', () => {
const navigate = jest.fn();
Expand All @@ -45,18 +49,98 @@ describe('stories API', () => {
const { storiesHash: storedStoriesHash } = store.getState();

// We need exact key ordering, even if in theory JS doens't guarantee it
expect(Object.keys(storedStoriesHash)).toEqual(['a', 'a--1', 'a--2', 'b-c', 'b-c--1']);
expect(Object.keys(storedStoriesHash)).toEqual(['a', 'a--1', 'a--2', 'b', 'b-c', 'b-c--1']);
expect(storedStoriesHash.a).toMatchObject({
id: 'a',
children: ['a--1', 'a--2'],
isRoot: false,
isComponent: true,
});

expect(storedStoriesHash['a--1']).toMatchObject({
id: 'a--1',
parent: 'a',
kind: 'a',
name: '1',
parameters: { options: {} },
parameters,
});

expect(storedStoriesHash['a--2']).toMatchObject({
id: 'a--2',
parent: 'a',
kind: 'a',
name: '2',
parameters,
});

expect(storedStoriesHash.b).toMatchObject({
id: 'b',
children: ['b-c'],
isRoot: false,
isComponent: false,
});

expect(storedStoriesHash['b-c']).toMatchObject({
id: 'b-c',
parent: 'b',
children: ['b-c--1'],
isRoot: false,
isComponent: true,
});

expect(storedStoriesHash['b-c--1']).toMatchObject({
id: 'b-c--1',
parent: 'b-c',
kind: 'b/c',
name: '1',
parameters,
});
});

it('handles roots also', () => {
const navigate = jest.fn();
const store = createMockStore();

const {
api: { setStories },
} = initStories({ store, navigate });

setStories({
'a--1': { kind: 'a', name: '1', parameters, path: 'a--1', id: 'a--1' },
'a--2': { kind: 'a', name: '2', parameters, path: 'a--2', id: 'a--2' },
'b-c--1': {
kind: 'b|c',
name: '1',
parameters,
path: 'b-c--1',
id: 'b-c--1',
},
});
const { storiesHash: storedStoriesHash } = store.getState();

// We need exact key ordering, even if in theory JS doens't guarantee it
expect(Object.keys(storedStoriesHash)).toEqual(['a', 'a--1', 'a--2', 'b', 'b-c', 'b-c--1']);
expect(storedStoriesHash.b).toMatchObject({
id: 'b',
children: ['b-c'],
isRoot: true,
isComponent: false,
});

expect(storedStoriesHash['b-c']).toMatchObject({
id: 'b-c',
parent: 'b',
children: ['b-c--1'],
isRoot: false,
isComponent: true,
});

expect(storedStoriesHash['b-c--1']).toMatchObject({
id: 'b-c--1',
parent: 'b-c',
kind: 'b|c',
name: '1',
parameters,
});
});

Expand Down

0 comments on commit 48a49d1

Please sign in to comment.