From 01f79bc1dc96a414c89fbcd2b568894f6eebff9b Mon Sep 17 00:00:00 2001 From: Evgeny Kochetkov Date: Sat, 14 Jan 2017 21:11:08 +0300 Subject: [PATCH 1/4] Warn if story with a given name already exists MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Right now if you add another story with the same name, the first one is lost. For example: ```js // … storiesOf('Button', module) .add('with text', () => ( // this story will be lost )) .add('with some emoji', () => ( )) .add('with text', () => (

this will silently overwrite the first story

)); ``` --- src/client/preview/story_store.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/client/preview/story_store.js b/src/client/preview/story_store.js index a231c598f48f..10a97c0d52eb 100644 --- a/src/client/preview/story_store.js +++ b/src/client/preview/story_store.js @@ -11,6 +11,10 @@ export default class StoryStore { } addStory(kind, name, fn) { + if (this.hasStory(kind, name)) { + throw new Error(`Story of ${kind} named "${name}" alredy exists!`); + } + if (!this._data[kind]) { this._data[kind] = { kind, From 61545ca5527d546c1ad8a1459945d1d0b9f5b619 Mon Sep 17 00:00:00 2001 From: Evgeny Kochetkov Date: Sun, 15 Jan 2017 09:31:34 +0300 Subject: [PATCH 2/4] Move check for duplicate story name to ClientApi --- src/client/preview/client_api.js | 4 ++++ src/client/preview/story_store.js | 4 ---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/client/preview/client_api.js b/src/client/preview/client_api.js index 33fa10295270..d83a1e60fd46 100644 --- a/src/client/preview/client_api.js +++ b/src/client/preview/client_api.js @@ -45,6 +45,10 @@ export default class ClientApi { }); api.add = (storyName, getStory) => { + if (this._storyStore.hasStory(kind, storyName)) { + throw new Error(`Story of "${kind}" named "${storyName}" alredy exists`); + } + // Wrap the getStory function with each decorator. The first // decorator will wrap the story function. The second will // wrap the first decorator and so on. diff --git a/src/client/preview/story_store.js b/src/client/preview/story_store.js index 10a97c0d52eb..a231c598f48f 100644 --- a/src/client/preview/story_store.js +++ b/src/client/preview/story_store.js @@ -11,10 +11,6 @@ export default class StoryStore { } addStory(kind, name, fn) { - if (this.hasStory(kind, name)) { - throw new Error(`Story of ${kind} named "${name}" alredy exists!`); - } - if (!this._data[kind]) { this._data[kind] = { kind, From d218fbbf9295d4cbacb5f549e397f3904a14bcd5 Mon Sep 17 00:00:00 2001 From: Evgeny Kochetkov Date: Sun, 15 Jan 2017 09:40:10 +0300 Subject: [PATCH 3/4] add hasStory method to StoryStore class in tests The real one in `src/client/preview/story_store.js` has this method. --- src/client/preview/__tests__/client_api.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/client/preview/__tests__/client_api.js b/src/client/preview/__tests__/client_api.js index 40140a2b8bfa..377c78e96117 100644 --- a/src/client/preview/__tests__/client_api.js +++ b/src/client/preview/__tests__/client_api.js @@ -38,6 +38,10 @@ class StoryStore { return fn; }, null); } + + hasStory(kind, name) { + return Boolean(this.getStory(kind, name)); + } } describe('preview.client_api', () => { From c54097ea43e54daf7884d99908cda01686025811 Mon Sep 17 00:00:00 2001 From: Evgeny Kochetkov Date: Thu, 26 Jan 2017 17:15:41 +0300 Subject: [PATCH 4/4] Fixed typo in warning "alredy" -> "already" --- src/client/preview/client_api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/preview/client_api.js b/src/client/preview/client_api.js index d83a1e60fd46..fa6e10451a35 100644 --- a/src/client/preview/client_api.js +++ b/src/client/preview/client_api.js @@ -46,7 +46,7 @@ export default class ClientApi { api.add = (storyName, getStory) => { if (this._storyStore.hasStory(kind, storyName)) { - throw new Error(`Story of "${kind}" named "${storyName}" alredy exists`); + throw new Error(`Story of "${kind}" named "${storyName}" already exists`); } // Wrap the getStory function with each decorator. The first