From 043e3b5a5e36bf738abbc044d0ccf3c677f547f1 Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Mon, 14 Jan 2019 20:33:11 +1100 Subject: [PATCH] FIX issue with accidentally thinking things are roots --- lib/ui/src/core/stories.js | 2 +- lib/ui/src/core/stories.test.js | 100 +++++++++++++++++++++++++++++--- 2 files changed, 93 insertions(+), 9 deletions(-) diff --git a/lib/ui/src/core/stories.js b/lib/ui/src/core/stories.js index 3dda820238..c802dda60f 100644 --- a/lib/ui/src/core/stories.js +++ b/lib/ui/src/core/stories.js @@ -155,7 +155,7 @@ const initStoriesApi = ({ parent, depth: index, isComponent: index === original.length - 1, - isRoot: index === 0, + isRoot: !!root && index === 0, }, ]); }, []); diff --git a/lib/ui/src/core/stories.test.js b/lib/ui/src/core/stories.test.js index def53578e9..3c9cf0c2c8 100644 --- a/lib/ui/src/core/stories.test.js +++ b/lib/ui/src/core/stories.test.js @@ -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(); @@ -45,10 +49,12 @@ 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({ @@ -56,7 +62,85 @@ describe('stories API', () => { 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, }); });