Skip to content

Commit

Permalink
Addon API: Clean preview hooks when removing a story (#8408)
Browse files Browse the repository at this point in the history
Addon API: Clean preview hooks when removing a story
  • Loading branch information
shilman committed Oct 14, 2019
1 parent 9594d72 commit a98aece
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/client-api/src/story_store.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import createChannel from '@storybook/channel-postmessage';
import { toId } from '@storybook/router/utils';
import addons from '@storybook/addons';

import StoryStore from './story_store';
import { defaultDecorateStory } from './client_api';
Expand Down Expand Up @@ -97,6 +98,7 @@ describe('preview.story_store', () => {
});
it('should remove the kind in both modern and legacy APIs', () => {
const store = new StoryStore({ channel });
addons.setChannel(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));
Expand All @@ -117,6 +119,7 @@ describe('preview.story_store', () => {
describe('remove', () => {
it('should remove the kind in both modern and legacy APIs', () => {
const store = new StoryStore({ channel });
addons.setChannel(channel);
store.addStory(...make('kind-1', 'story-1.1', () => 0));
store.addStory(...make('kind-1', 'story-1.2', () => 0));

Expand Down
7 changes: 5 additions & 2 deletions lib/client-api/src/story_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export default class StoryStore extends EventEmitter {
delete _data[id];

if (story) {
story.hooks.clean();
const { kind, name } = story;
const kindData = this._legacydata[toKey(kind)];
if (kindData) {
Expand Down Expand Up @@ -357,7 +358,7 @@ export default class StoryStore extends EventEmitter {
removeStoryKind(kind: string) {
if (this.hasStoryKind(kind)) {
this._legacydata[toKey(kind)].stories = {};

this.cleanHooksForKind(kind);
this._data = Object.entries(this._data).reduce((acc, [id, story]) => {
if (story.kind !== kind) {
Object.assign(acc, { [id]: story });
Expand Down Expand Up @@ -394,7 +395,9 @@ export default class StoryStore extends EventEmitter {
}

cleanHooks(id: string) {
this._data[id].hooks.clean();
if (this._data[id]) {
this._data[id].hooks.clean();
}
}

cleanHooksForKind(kind: string) {
Expand Down

0 comments on commit a98aece

Please sign in to comment.