-
-
Save ivn-cote/1f2b6e39c93c7194cf50cea62e0cf37c to your computer and use it in GitHub Desktop.
Revisions
-
jsdf renamed this gist
Dec 11, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
jsdf created this gist
Dec 11, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ // async function based version import request from 'request'; import stuffStore from './stuffStore'; export default async function getStuff(id) { stuffStore.startLoading(id); try { const res = await request('/stuff/'+id); const data = await res.json(); stuffStore.loaded(id, data) } catch (err) { if (!err instanceof request.RequestError) throw err; stuffStore.failedLoading(id, err); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ // synchronous version which would block the browser while waiting for data import request from 'request'; import stuffStore from './stuffStore'; export default function getStuff(id) { stuffStore.startLoading(id); try { const res = request('/stuff/'+id); const data = res.json(); stuffStore.loaded(id, data) } catch (err) { if (!err instanceof request.RequestError) throw err; stuffStore.failedLoading(id, err); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ jest.dontMock('../getStuff'); describe('getStuff', () => { let getStuff; let request; let stuffStore; it('loads the data', () => { const id = 1; const data = {a: 1}; stuffStore = require('../stuffStore'); request = require('request'); // mock request() to return a promise resolving to a mocked response // object, which has a method called .json(), which returns a promise // resolving to the data object. request.mockImpl(() => { return Promise.resolve({ json: () => Promise.resolve(data), }); }); getStuff = require('../getStuff'); // kick off the request getStuff(id); // resolve all promises jest.runAllTimers(); // make assertions about what should have occurred expect(stuffStore.startLoading).toBeCalledWith(id); expect(stuffStore.loaded).toBeCalledWith(id, data); expect(stuffStore.failedLoading).not.toBeCalled(); }); }) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ // promise based version import request from 'request'; import stuffStore from './stuffStore'; export default function getStuff(id) { stuffStore.startLoading(id); request('/stuff/'+id) .then(res => res.json()) .then(data => { stuffStore.loaded(id, data) }) .catch(err => { if (!err instanceof request.RequestError) return Promise.reject(err); stuffStore.failedLoading(id, err); }); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ const stuffStore = { startLoading(id) { }, loaded(id, data) { }, failedLoading(id, err) { }, } export default stuffStore;