ADD parent field to story_store data

This commit is contained in:
Tom Coleman 2018-12-26 13:20:48 +11:00
parent a9e439932f
commit a567b5d9f9
2 changed files with 9 additions and 1 deletions

View File

@ -157,11 +157,13 @@ export default class StoryStore extends EventEmitter {
// Map a bunch of extra fields onto the groups, collecting the path as we go (thus the reduce)
.reduce((soFar, group, index, original) => {
const { name } = group;
const path = sanitize(index === 0 ? name : `${soFar[index - 1].path}-${name}`);
const parent = index > 0 && soFar[index - 1].path;
const path = sanitize(parent ? `${parent}-${name}` : name);
return soFar.concat([
{
...group,
path,
parent,
depth: index,
isComponent: index === original.length - 1,
isRoot: index === 0,
@ -199,6 +201,7 @@ Story ids need to be unique -- ensure you aren't using the same names modolo url
parameters,
id: storyId,
path: storyId,
parent: rootAndGroups[rootAndGroups.length - 1].path,
depth: rootAndGroups.length,
isComponent: false,
isRoot: false,

View File

@ -30,6 +30,7 @@ describe('preview.story_store', () => {
expect(extracted['a--1']).toMatchObject({
id: 'a--1',
path: 'a--1',
parent: 'a',
kind: 'a',
name: '1',
parameters: {},
@ -57,24 +58,28 @@ describe('preview.story_store', () => {
isRoot: false,
isComponent: false,
children: ['a-b-c'],
parent: 'a',
});
expect(extracted['a-b-c']).toMatchObject({
name: 'c',
isRoot: false,
isComponent: false,
children: ['a-b-c-d'],
parent: 'a-b',
});
expect(extracted['a-b-c-d']).toMatchObject({
name: 'd',
isRoot: false,
isComponent: true,
children: ['a-b-c-d--1'],
parent: 'a-b-c',
});
expect(extracted['a-b-c-d--1']).toMatchObject({
kind: 'a|b/c/d',
name: '1',
isRoot: false,
isComponent: false,
parent: 'a-b-c-d',
});
});
});