Skip to content

Commit

Permalink
refactor: dublicate getProjectFolderName method
Browse files Browse the repository at this point in the history
  • Loading branch information
kakha urigashvili authored and RonWang committed Dec 8, 2020
1 parent ef32432 commit ff33a67
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 208 deletions.
27 changes: 2 additions & 25 deletions lib/commands/init/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const CONSTANTS = require('@src/utils/constants');
const stringUtils = require('@src/utils/string-utils');
const JsonView = require('@src/view/json-view');
const Messenger = require('@src/view/messenger');
const prompts = require('@src/view/prompt-view');

module.exports = {
showInitInstruction,
Expand All @@ -15,7 +16,7 @@ module.exports = {
getCodeSrcForRegion,
getSkillInfra,
showPreviewAndConfirm,
getProjectFolderName
getProjectFolderName: prompts.getProjectFolderName
};

function showInitInstruction(profile) {
Expand Down Expand Up @@ -156,27 +157,3 @@ ${JsonView.toString(askStates)}
callback(error);
});
}

/**
* To get user's input project folder name
* @param {string} defaultName a default project name
* @param {callback} callback { error, response }
*/
function getProjectFolderName(defaultName, callback) {
inquirer.prompt([{
message: 'Please type in your folder name for the skill project (alphanumeric): ',
type: 'input',
default: defaultName,
name: 'projectFolderName',
validate: (input) => {
if (!input || stringUtils.filterNonAlphanumeric(input) === '') {
return 'Project folder name should consist of alphanumeric character(s) plus "-" only.';
}
return true;
}
}]).then((answer) => {
callback(null, stringUtils.filterNonAlphanumeric(answer.projectFolderName));
}).catch((error) => {
callback(error);
});
}
22 changes: 2 additions & 20 deletions lib/commands/new/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ const inquirer = require('inquirer');

const CONSTANTS = require('@src/utils/constants');
const stringUtils = require('@src/utils/string-utils');
const prompts = require('@src/view/prompt-view');

const SKIP_DEPLOY_DELEGATE_SELECTION = 'self-hosted and manage your own hosting';

module.exports = {
getSkillName,
getSkillLocale,
getSkillDefaultRegion,
getProjectFolderName,
getProjectFolderName: prompts.getProjectFolderName,
selectSkillCodeLanguage,
getTargetTemplateName,
confirmUsingUnofficialTemplate,
Expand Down Expand Up @@ -68,25 +69,6 @@ function getSkillDefaultRegion(callback) {
});
}

function getProjectFolderName(defaultName, callback) {
inquirer.prompt([{
message: 'Please type in your folder name for the skill project (alphanumeric): ',
type: 'input',
default: defaultName,
name: 'projectFolderName',
validate: (input) => {
if (!input || stringUtils.filterNonAlphanumeric(input) === '') {
return 'Project folder name should be consisted of alphanumeric character(s) plus "-" only.';
}
return true;
}
}]).then((answer) => {
callback(null, stringUtils.filterNonAlphanumeric(answer.projectFolderName));
}).catch((error) => {
callback(error);
});
}

function selectSkillCodeLanguage(callback) {
inquirer.prompt([{
type: 'list',
Expand Down
31 changes: 31 additions & 0 deletions lib/view/prompt-view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const inquirer = require('inquirer');

const stringUtils = require('@src/utils/string-utils');

module.exports = {
getProjectFolderName
};

/**
* To get user's input project folder name
* @param {string} defaultName a default project name
* @param {callback} callback { error, response }
*/
function getProjectFolderName(defaultName, callback) {
inquirer.prompt([{
message: 'Please type in your folder name for the skill project (alphanumeric): ',
type: 'input',
default: defaultName,
name: 'projectFolderName',
validate: (input) => {
if (!input || stringUtils.filterNonAlphanumeric(input) === '') {
return 'Project folder name should consist of alphanumeric character(s) plus "-" only.';
}
return true;
}
}]).then((answer) => {
callback(null, stringUtils.filterNonAlphanumeric(answer.projectFolderName));
}).catch((error) => {
callback(error);
});
}
78 changes: 0 additions & 78 deletions test/unit/commands/init/ui-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,82 +365,4 @@ ${TEST_ASK_STATES_JSON}
});
});
});

describe('# validate ui.getProjectFolderName', () => {
beforeEach(() => {
infoStub = sinon.stub();
sinon.stub(inquirer, 'prompt');
sinon.stub(Messenger, 'getInstance').returns({
info: infoStub
});
});

afterEach(() => {
sinon.restore();
});

it('| confirm project folder name from user fails, expect error thrown', (done) => {
// setup
const TEST_DEFAULT_NAME = 'TEST_DEFAULT_NAME';
inquirer.prompt.rejects(TEST_ERROR);
// call
ui.getProjectFolderName(TEST_DEFAULT_NAME, (err, response) => {
// verify
validateInquirerConfig(inquirer.prompt.args[0][0][0], {
message: 'Please type in your folder name for the skill project (alphanumeric): ',
type: 'input',
default: TEST_DEFAULT_NAME,
});
expect(err.name).equal(TEST_ERROR);
expect(response).equal(undefined);
done();
});
});

it('| confirm project folder name from user', (done) => {
// setup
const TEST_DEFAULT_NAME = 'HOSTED-SKILL_NAME@';
const TEST_FILTERED_NAME = 'HOSTEDSKILLNAME';
inquirer.prompt.resolves({ projectFolderName: TEST_FILTERED_NAME });
// call
ui.getProjectFolderName(TEST_DEFAULT_NAME, (err, response) => {
// verify
validateInquirerConfig(inquirer.prompt.args[0][0][0], {
message: 'Please type in your folder name for the skill project (alphanumeric): ',
type: 'input',
default: TEST_DEFAULT_NAME,
name: 'projectFolderName',
});
expect(err).equal(null);
expect(response).equal(TEST_FILTERED_NAME);
done();
});
});

it('| project folder name from user with NonAlphanumeric filter is empty, expect error thrown', (done) => {
// setup
const TEST_DEFAULT_NAME = 'HOSTED-SKILL_NAME@';
inquirer.prompt.resolves({ projectFolderName: TEST_DEFAULT_NAME });
// call
ui.getProjectFolderName(TEST_DEFAULT_NAME, () => {
// verify
expect(inquirer.prompt.args[0][0][0].validate(TEST_DEFAULT_NAME)).equal(true);
done();
});
});

it('| project folder name from user with NonAlphanumeric filter is valid, expect return true', (done) => {
// setup
const TEST_DEFAULT_NAME = '@_@';
const TEST_EMPTY_ERROR = 'Project folder name should consist of alphanumeric character(s) plus "-" only.';
inquirer.prompt.resolves({ projectFolderName: TEST_DEFAULT_NAME });
// call
ui.getProjectFolderName(TEST_DEFAULT_NAME, () => {
// verify
expect(inquirer.prompt.args[0][0][0].validate(TEST_DEFAULT_NAME))
.equal(TEST_EMPTY_ERROR);
done();
});
});
});
});
85 changes: 0 additions & 85 deletions test/unit/commands/new/ui-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,91 +213,6 @@ describe('Commands new - UI test', () => {
});
});

describe('# validate ui.getProjectFolderName', () => {
beforeEach(() => {
sinon.stub(inquirer, 'prompt');
});

afterEach(() => {
sinon.restore();
});

it('| project folder name is retrieved correctly', (done) => {
// setup
inquirer.prompt.resolves({ projectFolderName: TEST_FOLDER_NAME });
// call
ui.getProjectFolderName(TEST_REPO_NAME, (err, response) => {
// verify
validateInquirerConfig(inquirer.prompt.args[0][0][0], {
message: 'Please type in your folder name for the skill project (alphanumeric): ',
type: 'input',
default: TEST_REPO_NAME,
});
expect(err).equal(null);
expect(response).equal(TEST_FOLDER_NAME);
done();
});
});

it('| project folder name is filtered correctly', (done) => {
// setup
const TEST_FOLDER_NAME_WITH_NON_ALPHANUMERIC = `${TEST_FOLDER_NAME}?/.%^&*`;
inquirer.prompt.resolves({ projectFolderName: TEST_FOLDER_NAME_WITH_NON_ALPHANUMERIC });
// call
ui.getProjectFolderName(TEST_REPO_NAME, (err, response) => {
// verify
validateInquirerConfig(inquirer.prompt.args[0][0][0], {
message: 'Please type in your folder name for the skill project (alphanumeric): ',
type: 'input',
default: TEST_REPO_NAME,
});
expect(err).equal(null);
expect(response).equal(TEST_FOLDER_NAME);
done();
});
});

it('| get project folder name throws error from inquirer', (done) => {
// setup
inquirer.prompt.rejects(new Error(TEST_ERROR));
// call
ui.getProjectFolderName(TEST_REPO_NAME, (err, response) => {
// verify
validateInquirerConfig(inquirer.prompt.args[0][0][0], {
message: 'Please type in your folder name for the skill project (alphanumeric): ',
type: 'input',
default: TEST_REPO_NAME,
});
expect(err.message).equal(TEST_ERROR);
expect(response).equal(undefined);
done();
});
});

it('| check the validate logic from inquirer and returns true', (done) => {
// setup
inquirer.prompt.resolves({ skillName: TEST_SKILL_NAME });
// call
ui.getProjectFolderName(TEST_REPO_NAME, () => {
// verify
expect(inquirer.prompt.args[0][0][0].validate(' !?!** '))
.equal('Project folder name should be consisted of alphanumeric character(s) plus "-" only.');
done();
});
});

it('| check the validate logic from inquirer and returns error', (done) => {
// setup
inquirer.prompt.resolves({ skillName: TEST_SKILL_NAME });
// call
ui.getProjectFolderName(TEST_REPO_NAME, () => {
// verify
expect(inquirer.prompt.args[0][0][0].validate('input')).equal(true);
done();
});
});
});

describe('# validate ui.selectSkillCodeLanguage', () => {
beforeEach(() => {
sinon.stub(inquirer, 'prompt');
Expand Down
1 change: 1 addition & 0 deletions test/unit/run-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ process.env.ASK_SHARE_USAGE = false;
// view
'@test/unit/view/messenger-test',
'@test/unit/view/json-view-test',
'@test/unit/view/prompt-view-test',
'@test/unit/view/spinner-view-test',
'@test/unit/view/multi-tasks-view-test',
'@test/unit/view/cli-repl-view-test',
Expand Down
Loading

0 comments on commit ff33a67

Please sign in to comment.