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

Support story decorators #115

Merged
merged 1 commit into from
Apr 17, 2016
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
24 changes: 21 additions & 3 deletions dist/client/client_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,30 @@ var ClientApi = function () {
});
}

var add = function add(storyName, fn) {
var decorators = [];
var api = {};

api.add = function (storyName, getStory) {
// 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.
var fn = decorators.reduce(function (decorated, decorator) {
return function () {
return decorator(decorated);
};
}, getStory);

// Add the fully decorated getStory function.
_this._storyStore.addStory(kind, storyName, fn);
return { add: add };
return api;
};

return { add: add };
api.addDecorator = function (decorator) {
decorators.push(decorator);
return api;
};

return api;
}
}, {
key: 'action',
Expand Down
23 changes: 23 additions & 0 deletions src/client/__tests__/client_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ describe('client.ClientApi', () => {
});
});

describe('storiesOf.addDecorator', () => {
it('should wrap a given story', () => {
const api = getClientApi();
const handle = () => ['h'];
const decorate1 = fn => fn().concat('d1');
const decorate2 = fn => fn().concat('d2');

api._storyStore.addStory = sinon.stub();
api.storiesOf('kind')
.addDecorator(decorate1)
.addDecorator(decorate2)
.add('name', handle);

const args = api._storyStore.addStory.args[0];
expect(args[0]).to.be.equal('kind');
expect(args[1]).to.be.equal('name');
expect(args[2]).to.be.a('function');

const decoratedFn = args[2];
expect(decoratedFn()).to.deep.equal(['h', 'd1', 'd2']);
});
});

describe('action', () => {
it('should send action info to the syncedStore', () => {
const api = getClientApi();
Expand Down
22 changes: 19 additions & 3 deletions src/client/client_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,28 @@ export default class ClientApi {
});
}

const add = (storyName, fn) => {
const decorators = [];
const api = {};

api.add = (storyName, getStory) => {
// 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.
const fn = decorators.reduce((decorated, decorator) => {
return () => decorator(decorated);
}, getStory);

// Add the fully decorated getStory function.
this._storyStore.addStory(kind, storyName, fn);
return { add };
return api;
};

api.addDecorator = decorator => {
decorators.push(decorator);
return api;
};

return { add };
return api;
}

action(name) {
Expand Down