Skip to content

Commit

Permalink
Merge pull request #4061 from michaelduminy/master
Browse files Browse the repository at this point in the history
Allow replacing of stories (again)
  • Loading branch information
Hypnosphi authored Sep 8, 2018
2 parents e4f96f9 + 836622f commit b96f1d3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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`);
}

// 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');
}
}
});
});
});
});

0 comments on commit b96f1d3

Please sign in to comment.