From 9d23bcb441f006d4e30b585e67ca773255fca930 Mon Sep 17 00:00:00 2001 From: Mike Duminy Date: Wed, 22 Aug 2018 12:53:23 +0200 Subject: [PATCH 1/2] allow replacing of stories fixes #4056 remove try catch in test --- lib/core/src/client/preview/client_api.js | 2 +- .../src/client/preview/client_api.test.js | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/core/src/client/preview/client_api.js b/lib/core/src/client/preview/client_api.js index b7d0c8c3ef3a..d7faf8f8091d 100644 --- a/lib/core/src/client/preview/client_api.js +++ b/lib/core/src/client/preview/client_api.js @@ -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 diff --git a/lib/core/src/client/preview/client_api.test.js b/lib/core/src/client/preview/client_api.test.js index 1a451da4cdeb..ccbb36d65366 100644 --- a/lib/core/src/client/preview/client_api.test.js +++ b/lib/core/src/client/preview/client_api.test.js @@ -344,4 +344,40 @@ describe('preview.client_api', () => { }); }); }); + + describe('storiesOf', () => { + class MockModule { + filename = 'mock'; + } + + describe('add', () => { + it('should replace stories when adding the same story', () => { + const module = new MockModule(); + const stories = [jest.fn(), jest.fn()]; + const warn = jest.spyOn(global.console, 'warn').mockImplementationOnce(jest.fn()); + + const api = new ClientAPI(); + expect(api.getStorybook()).toEqual([]); + + api.storiesOf('kind', module).add('story', stories[0]); + expect(api.getStorybook()).toEqual([ + { + fileName: 'mock', + kind: 'kind', + stories: [{ name: 'story', render: stories[0] }], + }, + ]); + + api.storiesOf('kind', module).add('story', stories[1]); + expect(warn).toHaveBeenCalled(); + expect(api.getStorybook()).toEqual([ + { + fileName: 'mock', + kind: 'kind', + stories: [{ name: 'story', render: stories[1] }], + }, + ]); + }); + }); + }); }); From 8041d150f90481119c37af7126a80b15d33f8a94 Mon Sep 17 00:00:00 2001 From: Mike Duminy Date: Thu, 23 Aug 2018 13:52:21 +0200 Subject: [PATCH 2/2] story replacement test to work with v3 and v4 fixes #4056 --- .../src/client/preview/client_api.test.js | 56 +++++++++++-------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/lib/core/src/client/preview/client_api.test.js b/lib/core/src/client/preview/client_api.test.js index ccbb36d65366..b83284ffccc2 100644 --- a/lib/core/src/client/preview/client_api.test.js +++ b/lib/core/src/client/preview/client_api.test.js @@ -346,37 +346,49 @@ describe('preview.client_api', () => { }); describe('storiesOf', () => { - class MockModule { - filename = 'mock'; - } - describe('add', () => { it('should replace stories when adding the same story', () => { - const module = new MockModule(); - const stories = [jest.fn(), jest.fn()]; - const warn = jest.spyOn(global.console, 'warn').mockImplementationOnce(jest.fn()); - + 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]); - expect(api.getStorybook()).toEqual([ - { - fileName: 'mock', - kind: 'kind', - stories: [{ name: 'story', render: 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(); - expect(api.getStorybook()).toEqual([ - { - fileName: 'mock', - kind: 'kind', - stories: [{ name: 'story', render: stories[1] }], - }, - ]); + { + 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'); + } + } }); }); });