Skip to content

Commit

Permalink
Merge pull request #7319 from storybookjs/7169-fix-renaming-stories
Browse files Browse the repository at this point in the history
Addon-docs: Fix renaming stories on module / MDX format
  • Loading branch information
shilman committed Jul 31, 2019
1 parent 9539a88 commit fc0e049
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/client-api/src/story_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,16 @@ export default class StoryStore extends EventEmitter {

remove = id => {
const { _data } = this;
const story = _data[id];
delete _data[id];

if (story) {
const { kind, name } = story;
const kindData = this._legacydata[toKey(kind)];
if (kindData) {
delete kindData.stories[toKey(name)];
}
}
};

addStory(
Expand Down Expand Up @@ -282,6 +291,13 @@ export default class StoryStore extends EventEmitter {
removeStoryKind(kind) {
if (this.hasStoryKind(kind)) {
this._legacydata[toKey(kind)].stories = {};

this._data = Object.entries(this._data).reduce((acc, [id, story]) => {
if (story.kind !== kind) {
Object.assign(acc, { [id]: story });
}
return acc;
}, {});
}
}

Expand Down
35 changes: 35 additions & 0 deletions lib/client-api/src/story_store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,41 @@ describe('preview.story_store', () => {
const store = new StoryStore({ channel });
store.removeStoryKind('kind');
});
it('should remove the kind in both modern and legacy APIs', () => {
const store = new StoryStore({ channel });
store.addStory(...make('kind-1', 'story-1.1', () => 0));
store.addStory(...make('kind-1', 'story-1.2', () => 0));
store.addStory(...make('kind-2', 'story-2.1', () => 0));
store.addStory(...make('kind-2', 'story-2.2', () => 0));

store.removeStoryKind('kind-1');

// _legacydata
expect(store.hasStory('kind-1', 'story-1.1')).toBeFalsy();
expect(store.hasStory('kind-2', 'story-2.1')).toBeTruthy();

// _data
expect(store.fromId(toId('kind-1', 'story-1.1'))).toBeFalsy();
expect(store.fromId(toId('kind-2', 'story-2.1'))).toBeTruthy();
});
});

describe('remove', () => {
it('should remove the kind in both modern and legacy APIs', () => {
const store = new StoryStore({ channel });
store.addStory(...make('kind-1', 'story-1.1', () => 0));
store.addStory(...make('kind-1', 'story-1.2', () => 0));

store.remove(toId('kind-1', 'story-1.1'));

// _legacydata
expect(store.hasStory('kind-1', 'story-1.1')).toBeFalsy();
expect(store.hasStory('kind-1', 'story-1.2')).toBeTruthy();

// _data
expect(store.fromId(toId('kind-1', 'story-1.1'))).toBeFalsy();
expect(store.fromId(toId('kind-1', 'story-1.2'))).toBeTruthy();
});
});

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

0 comments on commit fc0e049

Please sign in to comment.