Skip to content

Commit

Permalink
Fixes #877
Browse files Browse the repository at this point in the history
Throw on adding a global decorator after stories have already been added. The global decorator will not be applied.
  • Loading branch information
mattleff committed Jul 17, 2017
1 parent e2e1197 commit 8a48f80
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/react/src/client/preview/client_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default class ClientApi {
this._storyStore = storyStore;
this._addons = {};
this._globalDecorators = [];
this._storiesAdded = false;
}

setAddon(addon) {
Expand All @@ -18,6 +19,9 @@ export default class ClientApi {
}

addDecorator(decorator) {
if (this._storiesAdded) {
throw new Error('Global decorators added after loading stories will not be applied');
}
this._globalDecorators.push(decorator);
}

Expand Down Expand Up @@ -51,6 +55,8 @@ export default class ClientApi {
});

api.add = (storyName, getStory) => {
this._storiesAdded = true;

if (typeof storyName !== 'string') {
throw new Error(`Invalid or missing storyName provided for a "${kind}" story.`);
}
Expand Down
10 changes: 10 additions & 0 deletions app/react/src/client/preview/client_api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ describe('preview.client_api', () => {
expect(storyStore.stories[0].fn()).toBe('bb-Hello');
});

it('should throw on adding global decorators after stories', () => {
const storyStore = new StoryStore();
const api = new ClientAPI({ storyStore });
const localApi = api.storiesOf('none');
localApi.add('storyName', () => 'Hello');
expect(() => {
api.addDecorator(fn => `bb-${fn()}`);
}).toThrow('Global decorators added after loading stories will not be applied');
});

it('should utilize both decorators at once', () => {
const storyStore = new StoryStore();
const api = new ClientAPI({ storyStore });
Expand Down

1 comment on commit 8a48f80

@ntucker
Copy link

@ntucker ntucker commented on 8a48f80 Aug 4, 2017

Choose a reason for hiding this comment

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

This breaks HMR

Please sign in to comment.