Skip to content

Commit

Permalink
Implement linkTo functionality (#86)
Browse files Browse the repository at this point in the history
* Write some tests for linkTo

* Implement linkTo functionality
  • Loading branch information
jeef3 authored and arunoda committed Apr 9, 2016
1 parent 15ae006 commit 5a84fb0
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
28 changes: 28 additions & 0 deletions dist/client/client_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,34 @@ var ClientApi = function () {
syncedStore.setData({ actions: actions });
};
}
}, {
key: "linkTo",
value: function linkTo(kind, story) {
var syncedStore = this._syncedStore;

return function () {
var resolvedKind = typeof kind === "function" ? kind.apply(undefined, arguments) : kind;

var resolvedStory = void 0;
if (story) {
resolvedStory = typeof story === "function" ? story.apply(undefined, arguments) : story;
} else {
var _syncedStore$getData2 = syncedStore.getData();

var storyStore = _syncedStore$getData2.storyStore;


resolvedStory = storyStore.find(function (item) {
return item.kind === kind;
}).stories[0];
}

syncedStore.setData({
selectedKind: resolvedKind,
selectedStory: resolvedStory
});
};
}
}]);
return ClientApi;
}();
Expand Down
3 changes: 2 additions & 1 deletion dist/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.configure = exports.action = exports.storiesOf = undefined;
exports.configure = exports.linkTo = exports.action = exports.storiesOf = undefined;
exports.getStoryStore = getStoryStore;
exports.getSyncedStore = getSyncedStore;

Expand Down Expand Up @@ -41,4 +41,5 @@ function getSyncedStore() {

var storiesOf = exports.storiesOf = clientApi.storiesOf.bind(clientApi);
var action = exports.action = clientApi.action.bind(clientApi);
var linkTo = exports.linkTo = clientApi.linkTo.bind(clientApi);
var configure = exports.configure = configApi.configure.bind(configApi);
53 changes: 53 additions & 0 deletions src/client/__tests__/client_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,57 @@ describe('client.ClientApi', () => {
]);
});
});

describe('linkTo', () => {
it('should send kind to the syncedStore', () => {
const api = getClientApi();
api._syncedStore.getData = () => ({
storyStore: [{ kind: 'Another Kind', stories: [] }],
selectedKind: 'Some Kind',
});
api._syncedStore.setData = sinon.stub();

const cb = api.linkTo('Another Kind');
cb();

const args = api._syncedStore.setData.args[0];
expect(args[0].selectedKind).to.equal('Another Kind');
});

it('should send story to the syncedStore', () => {
const api = getClientApi();
api._syncedStore.getData = () => ({
storyStore: [{ kind: 'Another Kind', stories: [] }],
selectedKind: 'Some Kind',
selectedStory: 'Some Story',
});
api._syncedStore.setData = sinon.stub();

const cb = api.linkTo('Another Kind', 'Another Story');
cb();

const args = api._syncedStore.setData.args[0];
expect(args[0].selectedKind).to.equal('Another Kind');
expect(args[0].selectedStory).to.equal('Another Story');
});

it('should allow functions for story and kind', () => {
const api = getClientApi();
api._syncedStore.getData = () => ({
storyStore: [{ kind: 'Another Kind', stories: [] }],
selectedKind: 'Some Kind',
selectedStory: 'Some Story',
});
api._syncedStore.setData = sinon.stub();

const cb = api.linkTo(
() => 'Another Kind',
() => 'Another Story');
cb();

const args = api._syncedStore.setData.args[0];
expect(args[0].selectedKind).to.equal('Another Kind');
expect(args[0].selectedStory).to.equal('Another Story');
});
});
});
24 changes: 24 additions & 0 deletions src/client/client_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,28 @@ export default class ClientApi {
syncedStore.setData({ actions });
};
}

linkTo(kind, story) {
const syncedStore = this._syncedStore;

return function (...args) {
const resolvedKind = typeof kind === 'function' ? kind(...args) : kind;

let resolvedStory;
if (story) {
resolvedStory = typeof story === 'function' ? story(...args) : story;
} else {
const { storyStore } = syncedStore.getData();

resolvedStory = storyStore
.find(item => item.kind === kind)
.stories[0];
}

syncedStore.setData({
selectedKind: resolvedKind,
selectedStory: resolvedStory,
});
};
}
}
1 change: 1 addition & 0 deletions src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export function getSyncedStore() {

export const storiesOf = clientApi.storiesOf.bind(clientApi);
export const action = clientApi.action.bind(clientApi);
export const linkTo = clientApi.linkTo.bind(clientApi);
export const configure = configApi.configure.bind(configApi);

0 comments on commit 5a84fb0

Please sign in to comment.