Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 26 additions & 25 deletions src/cli/__tests__/getAttributesFromIndex.test.js
Original file line number Diff line number Diff line change
@@ -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: () => ({
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: () => {
throw new Error();
},
}),
}));

const attributes = await getAttributesFromIndex({
appId: 'appId',
apiKey: 'apiKey',
indexName: 'indexName',
algoliasearchFn: algoliasearchFailureFn,
});

expect(attributes).toEqual(['title', 'name', 'description']);
Expand Down
6 changes: 4 additions & 2 deletions src/cli/__tests__/getConfiguration.test.js
Original file line number Diff line number Diff line change
@@ -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'])),
Expand Down Expand Up @@ -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',
};
Expand All @@ -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));
Expand Down
3 changes: 1 addition & 2 deletions src/cli/getAttributesFromIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
3 changes: 1 addition & 2 deletions src/cli/getConfiguration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down