Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addon-docs: Fix renaming stories on module / MDX format #7319

Merged
merged 2 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -114,7 +114,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 @@ -271,6 +280,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]) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel a fear of recursion if both api's call each other..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fortunately they don't -- they just modify the data structures directly?

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 @@ -95,6 +95,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