From 5a84fb0a4499c720104e9809ea12431ca92abf56 Mon Sep 17 00:00:00 2001 From: Jeff Knaggs Date: Sun, 10 Apr 2016 01:38:56 +1200 Subject: [PATCH] Implement linkTo functionality (#86) * Write some tests for linkTo * Implement linkTo functionality --- dist/client/client_api.js | 28 ++++++++++++++++ dist/client/index.js | 3 +- src/client/__tests__/client_api.js | 53 ++++++++++++++++++++++++++++++ src/client/client_api.js | 24 ++++++++++++++ src/client/index.js | 1 + 5 files changed, 108 insertions(+), 1 deletion(-) diff --git a/dist/client/client_api.js b/dist/client/client_api.js index 1b68253a1fae..2ac2f6721ced 100644 --- a/dist/client/client_api.js +++ b/dist/client/client_api.js @@ -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; }(); diff --git a/dist/client/index.js b/dist/client/index.js index 7a22a82e6cce..b5e5d4da36a0 100644 --- a/dist/client/index.js +++ b/dist/client/index.js @@ -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; @@ -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); \ No newline at end of file diff --git a/src/client/__tests__/client_api.js b/src/client/__tests__/client_api.js index d95087bd71da..1be24e6e03b4 100644 --- a/src/client/__tests__/client_api.js +++ b/src/client/__tests__/client_api.js @@ -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'); + }); + }); }); diff --git a/src/client/client_api.js b/src/client/client_api.js index 1ecf0f86f816..bb3670c5b5a5 100644 --- a/src/client/client_api.js +++ b/src/client/client_api.js @@ -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, + }); + }; + } } diff --git a/src/client/index.js b/src/client/index.js index 5006bebbc0fd..c715adc05db9 100644 --- a/src/client/index.js +++ b/src/client/index.js @@ -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);