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

refactor(index): rewrite answer parsing #541

Merged
merged 20 commits into from
Dec 2, 2021
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ $ create-instantsearch-app --help
--library-version <libraryVersion> The version of the library
--config <config> The configuration file to get the options from
--no-installation Ignore dependency installation
--no-interactive Do not ask any interactive questions
-h, --help output usage information
```

Expand Down
78 changes: 78 additions & 0 deletions e2e/installs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('Installation', () => {
`yarn start ${appPath} \
--name ${appName} \
--template "InstantSearch.js" \
--no-interactive \
--no-installation`,
{ stdio: 'ignore' }
);
Expand All @@ -47,6 +48,7 @@ describe('Installation', () => {
`yarn start ${appPath} \
--name ${appName} \
--template "InstantSearch.js" \
--no-interactive \
--no-installation`,
{ stdio: 'ignore' }
);
Expand All @@ -62,6 +64,7 @@ describe('Installation', () => {
`yarn start ${appPath} \
--name ${appName} \
--template "InstantSearch.js" \
--no-interactive \
--no-installation`,
{ stdio: 'ignore' }
);
Expand All @@ -82,6 +85,7 @@ describe('Installation', () => {
`yarn start ${appPath}/file \
--name ${appName} \
--template "InstantSearch.js" \
--no-interactive \
--no-installation`,
{ stdio: 'ignore' }
);
Expand All @@ -90,4 +94,78 @@ describe('Installation', () => {
expect(fs.existsSync(`${appPath}/file`)).toBe(true);
});
});

describe('Arguments', () => {
test('uses name from argument', () => {
execSync(
`yarn start ${appPath} \
--name ${appName} \
--template "InstantSearch.js" \
--no-interactive \
--no-installation`,
{ stdio: 'ignore' }
);

const { name } = require(`${appPath}/package.json`);

expect(name).toBe(appName);
});

test('uses name from path', () => {
execSync(
`yarn start ${appPath} \
--template "InstantSearch.js" \
--no-interactive \
--no-installation`,
{ stdio: 'ignore' }
);

const { name } = require(`${appPath}/package.json`);

expect(name).toBe(appName);
});

test('uses template from argument (vanilla)', () => {
execSync(
`yarn start ${appPath} \
--name ${appName} \
--template "InstantSearch.js" \
--no-interactive \
--no-installation`,
{ stdio: 'ignore' }
);

const { dependencies } = require(`${appPath}/package.json`);

expect(dependencies['instantsearch.js']).toEqual(expect.any(String));
});

test('uses template from argument (react)', () => {
execSync(
`yarn start ${appPath}/react \
--name ${appName} \
--template "React InstantSearch" \
--no-interactive \
--no-installation`,
{ stdio: 'ignore' }
);

const { dependencies } = require(`${appPath}/react/package.json`);

expect(dependencies['react-instantsearch-dom']).toEqual(
expect.any(String)
);
});

test('without template fails', () => {
expect(() => {
execSync(
`yarn start ${appPath} \
--no-interactive \
--no-installation`,
{ stdio: 'ignore' }
);
}).toThrow();
});
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@algolia/cache-in-memory": "4.11.0",
"algoliasearch": "4.11.0",
"chalk": "3.0.0",
"commander": "4.0.1",
"commander": "4.1.1",
"inquirer": "8.0.0",
"jstransformer-handlebars": "1.1.0",
"latest-semver": "2.0.0",
Expand Down
18 changes: 17 additions & 1 deletion scripts/build-app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path');
const fs = require('fs');
const { execSync } = require('child_process');

const usage = `
Expand All @@ -20,10 +21,25 @@ if (!templateName) {

const appName = path.basename(appPath);

const config = {
template: templateName,
appId: 'appId',
apiKey: 'apiKey',
indexName: 'indexName',
searchPlaceholder: 'Search placeholder',
attributesToDisplay: ['attribute1', 'attribute2'],
attributesForFaceting: ['facet1', 'facet2'],
organization: 'algolia',
};

const configFilePath = `cisa.config.json`;

fs.writeFileSync(configFilePath, JSON.stringify(config));

execSync(
`yarn start ${appPath} \
--name "${appName}" \
--template "${templateName}"`,
--config "${configFilePath}"`,
{ stdio: 'inherit' }
);

Expand Down
23 changes: 1 addition & 22 deletions src/cli/__tests__/getConfiguration.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
const loadJsonFile = require('load-json-file');
const utils = require('../../utils');
const { getConfiguration, getLibraryVersion } = require('../getConfiguration');
const getConfiguration = require('../getConfiguration');

jest.mock('load-json-file');

jest.mock('../../utils', () => ({
...require.requireActual('../../utils'),
fetchLibraryVersions: jest.fn(() => Promise.resolve(['1.0.0'])),
}));

test('without template throws', async () => {
expect.assertions(1);

Expand Down Expand Up @@ -36,21 +30,6 @@ test('with options from arguments and prompt merge', async () => {
);
});

test('without stable version available', async () => {
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
utils.fetchLibraryVersions.mockImplementationOnce(() =>
Promise.resolve(['1.0.0-beta.0'])
);

const libraryVersion = await getLibraryVersion(
{
template: 'InstantSearch.js',
},
utils.getAppTemplateConfig(utils.getTemplatePath('InstantSearch.js'))
);

expect(libraryVersion).toBe('1.0.0-beta.0');
});

test('with config file overrides all options', async () => {
loadJsonFile.mockImplementationOnce(x => Promise.resolve(x));
const ignoredOptions = {
Expand Down
72 changes: 0 additions & 72 deletions src/cli/__tests__/getOptionsFromArguments.test.js

This file was deleted.

42 changes: 32 additions & 10 deletions src/cli/__tests__/isQuestionAsked.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ test('with appId undefined should ask', () => {
expect(
isQuestionAsked({
question: { name: 'appId', validate: input => Boolean(input) },
args: { appId: undefined },
args: { appId: undefined, interactive: true },
})
).toBe(true);
).toBe(false);
});

test('with appId defined should not ask', () => {
expect(
isQuestionAsked({
question: { name: 'appId', validate: input => Boolean(input) },
args: { appId: 'APP_ID' },
args: { appId: 'APP_ID', interactive: true },
})
).toBe(false);
).toBe(true);
});

test('with unvalid template should ask', () => {
test('with invalid template should ask', () => {
expect(
isQuestionAsked({
question: {
name: 'template',
validate: () => false,
},
args: { template: 'Unvalid' },
args: { template: 'Unvalid', interactive: true },
})
).toBe(true);
).toBe(false);
});

test('with valid template should not ask', () => {
Expand All @@ -37,9 +37,9 @@ test('with valid template should not ask', () => {
name: 'template',
validate: () => true,
},
args: { template: 'InstantSearch.js' },
args: { template: 'InstantSearch.js', interactive: true },
})
).toBe(false);
).toBe(true);
});

test('with indexName should ask attributesToDisplay', () => {
Expand All @@ -48,7 +48,29 @@ test('with indexName should ask attributesToDisplay', () => {
question: {
name: 'attributesToDisplay',
},
args: { indexName: 'INDEX_NAME' },
args: { indexName: 'INDEX_NAME', interactive: true },
})
).toBe(false);
});

test('with config it does not ask', () => {
expect(
isQuestionAsked({
question: {
name: 'attributesToDisplay',
},
args: { config: '' },
})
).toBe(true);
});

test('with --no-interactive it does not ask', () => {
expect(
isQuestionAsked({
question: {
name: 'attributesToDisplay',
},
args: { interactive: false },
})
).toBe(true);
});
Loading