Created
December 9, 2018 00:27
-
-
Save mattiaerre/da3fe3f01504bb5e2e481a16dafb96f4 to your computer and use it in GitHub Desktop.
Revisions
-
mattiaerre created this gist
Dec 9, 2018 .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,12 @@ import fetch from 'node-fetch'; function makeMenusClient(context) { return { getMenus: async rid => fetch(`${context.menusBaseUrl}/${rid}`) .then(response => response.json()) .then(json => json) }; } export default makeMenusClient; 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,14 @@ import makeMenusClient from './makeMenusClient'; async function makeRestaurantDetails(context, options) { const menusClient = makeMenusClient(context); const menus = await menusClient.getMenus(options.rid); return { menus, name: options.name, rid: options.rid }; } export default makeRestaurantDetails; 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,26 @@ import makeMenusClient from './makeMenusClient'; import makeRestaurantDetails from './makeRestaurantDetails'; jest.mock('./makeMenusClient', () => { // check: https://stackoverflow.com/a/51534326/752391 const mockMenusClient = { getMenus: jest.fn() }; return jest.fn(() => mockMenusClient); }); test('makeRestaurantDetails', async () => { const context = { menusBaseUrl: 'http://menus.opentable.com/api/v1' }; const options = { name: 'Urban Remedy', rid: 8 }; makeMenusClient().getMenus.mockImplementation(() => [ { title: 'Brunch' }, { title: 'Dinner' } ]); expect(await makeRestaurantDetails(context, options)).toMatchSnapshot(); }); 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,16 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`makeRestaurantDetails 1`] = ` Object { "menus": Array [ Object { "title": "Brunch", }, Object { "title": "Dinner", }, ], "name": "Urban Remedy", "rid": 8, } `;