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

Allow replacing of stories with a warning (rather than an error) #4061

Merged
merged 3 commits into from
Sep 8, 2018
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
2 changes: 1 addition & 1 deletion lib/core/src/client/preview/client_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default class ClientApi {
}

if (this._storyStore.hasStory(kind, storyName)) {
throw new Error(`Story of "${kind}" named "${storyName}" already exists`);
logger.warn(`Story of "${kind}" named "${storyName}" already exists`);
Copy link
Member

@igor-dv igor-dv Aug 23, 2018

Choose a reason for hiding this comment

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

Basically, I agree with this change, though I wonder whether we want to replace an existing story in the store with this kind
@tmeasday you've worked a lot in this area. WDYT ?

}

// Wrap the getStory function with each decorator. The first
Expand Down
48 changes: 48 additions & 0 deletions lib/core/src/client/preview/client_api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,52 @@ describe('preview.client_api', () => {
});
});
});

describe('storiesOf', () => {
describe('add', () => {
it('should replace stories when adding the same story', () => {
const stories = [jest.fn().mockReturnValue('story1'), jest.fn().mockReturnValue('story2')];
const api = new ClientAPI();
expect(api.getStorybook()).toEqual([]);

api.storiesOf('kind', module).add('story', stories[0]);
{
const book = api.getStorybook();
expect(book).toHaveLength(1);

const entry = book[0];
expect(entry.kind).toMatch('kind');
expect(entry.stories).toHaveLength(1);
expect(entry.stories[0].name).toBe('story');

// v3 returns the same function we passed in
if (jest.isMockFunction(entry.stories[0].render)) {
expect(entry.stories[0].render).toBe(stories[0]);
} else {
expect(entry.stories[0].render()).toBe('story1');
}
}

const warn = jest.spyOn(global.console, 'warn').mockImplementationOnce(jest.fn());
api.storiesOf('kind', module).add('story', stories[1]);
expect(warn).toHaveBeenCalled();
{
const book = api.getStorybook();
expect(book).toHaveLength(1);

const entry = book[0];
expect(entry.kind).toMatch('kind');
expect(entry.stories).toHaveLength(1);
expect(entry.stories[0].name).toBe('story');

// v3 returns the same function we passed in
if (jest.isMockFunction(entry.stories[0].render)) {
expect(entry.stories[0].render).toBe(stories[0]);
} else {
expect(entry.stories[0].render()).toBe('story2');
}
}
});
});
});
});