From c0e8a61398db38e4d9a221a15991e6397d4f0290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chalifour?= Date: Fri, 15 Jun 2018 16:46:17 +0200 Subject: [PATCH 1/2] test(cli): Mock modules with Jest --- .../__tests__/getAttributesFromIndex.test.js | 51 ++++++++++--------- src/cli/__tests__/getConfiguration.test.js | 6 ++- src/cli/getAttributesFromIndex.js | 3 +- src/cli/getConfiguration.js | 3 +- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/cli/__tests__/getAttributesFromIndex.test.js b/src/cli/__tests__/getAttributesFromIndex.test.js index 787b0b506..9b8b4883e 100644 --- a/src/cli/__tests__/getAttributesFromIndex.test.js +++ b/src/cli/__tests__/getAttributesFromIndex.test.js @@ -1,47 +1,48 @@ +const algoliasearch = require('algoliasearch'); const getAttributesFromIndex = require('../getAttributesFromIndex'); -const algoliasearchSuccessFn = () => ({ - initIndex: () => ({ - search: jest.fn(() => ({ - hits: [ - { - _highlightResult: { - brand: 'brand', - description: 'description', - name: 'name', - title: 'title', - }, - }, - ], - })), - }), -}); +jest.mock('algoliasearch'); -const algoliasearchFailureFn = () => ({ - initIndex: () => ({ - search: jest.fn(() => { - throw new Error(); +test('with search success should fetch attributes', async () => { + algoliasearch.mockImplementationOnce(() => ({ + initIndex: () => ({ + search: jest.fn(() => ({ + hits: [ + { + _highlightResult: { + brand: 'brand', + description: 'description', + name: 'name', + title: 'title', + }, + }, + ], + })), }), - }), -}); + })); -test('with search success should fetch attributes', async () => { const attributes = await getAttributesFromIndex({ appId: 'appId', apiKey: 'apiKey', indexName: 'indexName', - algoliasearchFn: algoliasearchSuccessFn, }); expect(attributes).toEqual(['title', 'name', 'description', 'brand']); }); test('with search failure should return default attributes', async () => { + algoliasearch.mockImplementationOnce(() => ({ + initIndex: () => ({ + search: jest.fn(() => { + throw new Error(); + }), + }), + })); + const attributes = await getAttributesFromIndex({ appId: 'appId', apiKey: 'apiKey', indexName: 'indexName', - algoliasearchFn: algoliasearchFailureFn, }); expect(attributes).toEqual(['title', 'name', 'description']); diff --git a/src/cli/__tests__/getConfiguration.test.js b/src/cli/__tests__/getConfiguration.test.js index 6434a4205..344100461 100644 --- a/src/cli/__tests__/getConfiguration.test.js +++ b/src/cli/__tests__/getConfiguration.test.js @@ -1,7 +1,10 @@ const path = require('path'); +const loadJsonFile = require('load-json-file'); const utils = require('../../utils'); const getConfiguration = require('../getConfiguration'); +jest.mock('load-json-file'); + jest.mock('../../utils', () => ({ ...require.requireActual('../../utils'), fetchLibraryVersions: jest.fn(() => Promise.resolve(['1.0.0'])), @@ -63,7 +66,7 @@ test('without stable version available', async () => { }); test('with config file overrides all options', async () => { - const loadJsonFileFn = jest.fn(x => Promise.resolve(x)); + loadJsonFile.mockImplementationOnce(x => Promise.resolve(x)); const ignoredOptions = { libraryVersion: '2.0.0', }; @@ -81,7 +84,6 @@ test('with config file overrides all options', async () => { const configuration = await getConfiguration({ options, answers, - loadJsonFileFn, }); expect(configuration).toEqual(expect.not.objectContaining(ignoredOptions)); diff --git a/src/cli/getAttributesFromIndex.js b/src/cli/getAttributesFromIndex.js index 31ccffe1e..a3ec8a3f2 100644 --- a/src/cli/getAttributesFromIndex.js +++ b/src/cli/getAttributesFromIndex.js @@ -4,9 +4,8 @@ module.exports = async function getAttributesFromIndex({ appId, apiKey, indexName, - algoliasearchFn = algoliasearch, } = {}) { - const client = algoliasearchFn(appId, apiKey); + const client = algoliasearch(appId, apiKey); const index = client.initIndex(indexName); const defaultAttributes = ['title', 'name', 'description']; let attributes = []; diff --git a/src/cli/getConfiguration.js b/src/cli/getConfiguration.js index a6a21c002..b3d0a003f 100644 --- a/src/cli/getConfiguration.js +++ b/src/cli/getConfiguration.js @@ -10,10 +10,9 @@ const { module.exports = async function getConfiguration({ options = {}, answers = {}, - loadJsonFileFn = loadJsonFile, } = {}) { const config = options.config - ? await loadJsonFileFn(options.config) // From configuration file given as an argument + ? await loadJsonFile(options.config) // From configuration file given as an argument : { ...options, ...answers }; // From the arguments and the prompt if (!config.template) { From 7c677a7029395fe6b0d85b74d6b904f13547ea01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chalifour?= Date: Fri, 15 Jun 2018 16:48:57 +0200 Subject: [PATCH 2/2] test(cli): Remove Jest functions --- src/cli/__tests__/getAttributesFromIndex.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cli/__tests__/getAttributesFromIndex.test.js b/src/cli/__tests__/getAttributesFromIndex.test.js index 9b8b4883e..b4d239fdb 100644 --- a/src/cli/__tests__/getAttributesFromIndex.test.js +++ b/src/cli/__tests__/getAttributesFromIndex.test.js @@ -6,7 +6,7 @@ jest.mock('algoliasearch'); test('with search success should fetch attributes', async () => { algoliasearch.mockImplementationOnce(() => ({ initIndex: () => ({ - search: jest.fn(() => ({ + search: () => ({ hits: [ { _highlightResult: { @@ -17,7 +17,7 @@ test('with search success should fetch attributes', async () => { }, }, ], - })), + }), }), })); @@ -33,9 +33,9 @@ test('with search success should fetch attributes', async () => { test('with search failure should return default attributes', async () => { algoliasearch.mockImplementationOnce(() => ({ initIndex: () => ({ - search: jest.fn(() => { + search: () => { throw new Error(); - }), + }, }), }));