Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit a48eeeb

Browse files
test(cli): Mock modules with Jest (#58)
1 parent e764cd5 commit a48eeeb

File tree

4 files changed

+32
-31
lines changed

4 files changed

+32
-31
lines changed

src/cli/__tests__/getAttributesFromIndex.test.js

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,48 @@
1+
const algoliasearch = require('algoliasearch');
12
const getAttributesFromIndex = require('../getAttributesFromIndex');
23

3-
const algoliasearchSuccessFn = () => ({
4-
initIndex: () => ({
5-
search: jest.fn(() => ({
6-
hits: [
7-
{
8-
_highlightResult: {
9-
brand: 'brand',
10-
description: 'description',
11-
name: 'name',
12-
title: 'title',
13-
},
14-
},
15-
],
16-
})),
17-
}),
18-
});
4+
jest.mock('algoliasearch');
195

20-
const algoliasearchFailureFn = () => ({
21-
initIndex: () => ({
22-
search: jest.fn(() => {
23-
throw new Error();
6+
test('with search success should fetch attributes', async () => {
7+
algoliasearch.mockImplementationOnce(() => ({
8+
initIndex: () => ({
9+
search: () => ({
10+
hits: [
11+
{
12+
_highlightResult: {
13+
brand: 'brand',
14+
description: 'description',
15+
name: 'name',
16+
title: 'title',
17+
},
18+
},
19+
],
20+
}),
2421
}),
25-
}),
26-
});
22+
}));
2723

28-
test('with search success should fetch attributes', async () => {
2924
const attributes = await getAttributesFromIndex({
3025
appId: 'appId',
3126
apiKey: 'apiKey',
3227
indexName: 'indexName',
33-
algoliasearchFn: algoliasearchSuccessFn,
3428
});
3529

3630
expect(attributes).toEqual(['title', 'name', 'description', 'brand']);
3731
});
3832

3933
test('with search failure should return default attributes', async () => {
34+
algoliasearch.mockImplementationOnce(() => ({
35+
initIndex: () => ({
36+
search: () => {
37+
throw new Error();
38+
},
39+
}),
40+
}));
41+
4042
const attributes = await getAttributesFromIndex({
4143
appId: 'appId',
4244
apiKey: 'apiKey',
4345
indexName: 'indexName',
44-
algoliasearchFn: algoliasearchFailureFn,
4546
});
4647

4748
expect(attributes).toEqual(['title', 'name', 'description']);

src/cli/__tests__/getConfiguration.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
const path = require('path');
2+
const loadJsonFile = require('load-json-file');
23
const utils = require('../../utils');
34
const getConfiguration = require('../getConfiguration');
45

6+
jest.mock('load-json-file');
7+
58
jest.mock('../../utils', () => ({
69
...require.requireActual('../../utils'),
710
fetchLibraryVersions: jest.fn(() => Promise.resolve(['1.0.0'])),
@@ -63,7 +66,7 @@ test('without stable version available', async () => {
6366
});
6467

6568
test('with config file overrides all options', async () => {
66-
const loadJsonFileFn = jest.fn(x => Promise.resolve(x));
69+
loadJsonFile.mockImplementationOnce(x => Promise.resolve(x));
6770
const ignoredOptions = {
6871
libraryVersion: '2.0.0',
6972
};
@@ -81,7 +84,6 @@ test('with config file overrides all options', async () => {
8184
const configuration = await getConfiguration({
8285
options,
8386
answers,
84-
loadJsonFileFn,
8587
});
8688

8789
expect(configuration).toEqual(expect.not.objectContaining(ignoredOptions));

src/cli/getAttributesFromIndex.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ module.exports = async function getAttributesFromIndex({
44
appId,
55
apiKey,
66
indexName,
7-
algoliasearchFn = algoliasearch,
87
} = {}) {
9-
const client = algoliasearchFn(appId, apiKey);
8+
const client = algoliasearch(appId, apiKey);
109
const index = client.initIndex(indexName);
1110
const defaultAttributes = ['title', 'name', 'description'];
1211
let attributes = [];

src/cli/getConfiguration.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ const {
1010
module.exports = async function getConfiguration({
1111
options = {},
1212
answers = {},
13-
loadJsonFileFn = loadJsonFile,
1413
} = {}) {
1514
const config = options.config
16-
? await loadJsonFileFn(options.config) // From configuration file given as an argument
15+
? await loadJsonFile(options.config) // From configuration file given as an argument
1716
: { ...options, ...answers }; // From the arguments and the prompt
1817

1918
if (!config.template) {

0 commit comments

Comments
 (0)