Skip to content

Commit 92fc060

Browse files
refactor: pass question to buid project method
1 parent 6fb5f1f commit 92fc060

5 files changed

Lines changed: 55 additions & 43 deletions

File tree

src/__tests__/actions/add.action.spec.ts

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import emoji from 'node-emoji';
33

44
import { AddAction } from '../../actions/add.action';
55
import * as projectBuilder from '../../core/project/builder/project.builder';
6-
import * as projectQuestionsBuilder from '../../core/project/builder/questions.builder';
76
import { ProjectRepository } from '../../core/project/persistence/repository';
87
import { Project } from '../../core/project/project.entity';
98
import { OpenVSCode } from '../../core/project/tasks/open-editor/vscode.task';
@@ -15,29 +14,58 @@ jest.mock('../../core/project/builder/questions.builder');
1514
const buildProject = projectBuilder.buildProject as jest.MockedFunction<
1615
typeof projectBuilder.buildProject
1716
>;
18-
const buildProjectQuestions = projectQuestionsBuilder.buildProjectQuestions as jest.MockedFunction<
19-
typeof projectQuestionsBuilder.buildProjectQuestions
20-
>;
2117

2218
describe('Add action', () => {
2319
describe('Setup', () => {
24-
it('should return a function calling handle with inputs', () => {
25-
const action = new AddAction({} as ProjectRepository);
26-
action.handle = jest.fn();
27-
const setup = action.setup();
20+
describe('When occurr some error handling action', () => {
21+
it('should exit process with code 1', async () => {
22+
global.process.exit = jest.fn() as any;
23+
const action = new AddAction({} as ProjectRepository);
24+
action.handle = jest.fn().mockRejectedValue(new Error());
25+
26+
const setup = action.setup();
27+
28+
const path = '/var/www/html';
29+
const alias = 'fun';
30+
31+
const command = { path };
32+
33+
await setup(alias, command);
34+
35+
expect(action.handle).toHaveBeenCalledTimes(1);
36+
expect(action.handle).toHaveBeenCalledWith([
37+
{ name: 'alias', value: alias },
38+
{ name: 'path', value: path },
39+
]);
40+
41+
expect(process.exit).toHaveBeenCalledTimes(1);
42+
expect(process.exit).toHaveBeenCalledWith(1);
43+
});
44+
});
45+
46+
describe('When handle with success', () => {
47+
it('should return a function calling handle with inputs', () => {
48+
global.process.exit = jest.fn() as any;
49+
50+
const action = new AddAction({} as ProjectRepository);
51+
action.handle = jest.fn();
52+
const setup = action.setup();
53+
54+
const path = '/var/www/html';
55+
const alias = 'fun';
2856

29-
const path = '/var/www/html';
30-
const alias = 'fun';
57+
const command = { path };
3158

32-
const command = { path };
59+
setup(alias, command);
3360

34-
setup(alias, command);
61+
expect(action.handle).toHaveBeenCalledTimes(1);
62+
expect(action.handle).toHaveBeenCalledWith([
63+
{ name: 'alias', value: alias },
64+
{ name: 'path', value: path },
65+
]);
3566

36-
expect(action.handle).toHaveBeenCalledTimes(1);
37-
expect(action.handle).toHaveBeenCalledWith([
38-
{ name: 'alias', value: alias },
39-
{ name: 'path', value: path },
40-
]);
67+
expect(process.exit).not.toHaveBeenCalled();
68+
});
4169
});
4270
});
4371

@@ -133,14 +161,12 @@ describe('Add action', () => {
133161

134162
describe('When project not exists', () => {
135163
const path = `${__dirname}/../fixtures`;
136-
const questions = ['fake-questions'];
137164
let action: AddAction;
138165

139166
describe('When persist project with success', () => {
140167
let project: Project;
141168
beforeAll(async () => {
142169
global.console.log = jest.fn();
143-
buildProjectQuestions.mockReturnValue(questions);
144170

145171
repositoryMock.create.mockResolvedValue(true);
146172

@@ -160,16 +186,8 @@ describe('Add action', () => {
160186
project = projectDefinitionsFactory();
161187
});
162188

163-
it('should build questions with project alias', () => {
164-
expect(buildProjectQuestions).toHaveBeenCalled();
165-
});
166-
167189
it('should build project with build project questions', () => {
168-
expect(buildProject).toHaveBeenCalledWith(
169-
alias,
170-
path,
171-
questions,
172-
);
190+
expect(buildProject).toHaveBeenCalledWith(alias, path);
173191
});
174192

175193
it('should persist the built project', () => {

src/__tests__/core/project/builder/project.builder.spec.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as inquirer from 'inquirer';
22

33
import { buildProject } from '../../../../core/project/builder/project.builder';
4-
import { BuildProjectQuestionCollection } from '../../../../core/project/builder/questions.builder';
54
import { Project } from '../../../../core/project/project.entity';
65
import { OpenVSCode } from '../../../../core/project/tasks/open-editor/vscode.task';
76

@@ -16,14 +15,12 @@ describe('Build project', () => {
1615

1716
let builtProject: Project;
1817
beforeAll(async () => {
19-
const questions = {} as BuildProjectQuestionCollection;
20-
2118
const answers = {
2219
tasks: ['open-vscode'],
2320
};
2421

2522
prompt.mockResolvedValue(answers);
26-
builtProject = await buildProject(alias, path, questions);
23+
builtProject = await buildProject(alias, path);
2724
});
2825

2926
it('should built project with open-vscode action', () => {

src/actions/abstract.action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Input } from '../commands/command.input';
22

33
export abstract class AbstractAction {
4-
setup(this: AbstractAction): (...args: any[]) => void {
4+
setup(this: AbstractAction): (...args: any[]) => Promise<void> {
55
return async (...args: any[]) => {
66
const inputs = this.mountInputs(...args);
77

src/actions/add.action.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as fs from 'fs';
44

55
import { Input } from '../commands/command.input';
66
import { buildProject } from '../core/project/builder/project.builder';
7-
import { buildProjectQuestions } from '../core/project/builder/questions.builder';
87
import { ProjectRepository } from '../core/project/persistence/repository';
98
import {
109
ADD_ACTION_SUCCESS,
@@ -29,13 +28,10 @@ export class AddAction extends AbstractAction {
2928

3029
public async handle(inputs: Input[]) {
3130
const projectAlias = getProjectAlias(inputs);
32-
const projectPath = this._getProjectPath(inputs);
33-
3431
await this._ensureProjectDoesNotExists(projectAlias);
3532

36-
const questions = buildProjectQuestions();
37-
38-
const project = await buildProject(projectAlias, projectPath, questions);
33+
const projectPath = this._getProjectPath(inputs);
34+
const project = await buildProject(projectAlias, projectPath);
3935

4036
await this.repository.create(project);
4137

@@ -46,9 +42,9 @@ export class AddAction extends AbstractAction {
4642
private async _ensureProjectDoesNotExists(
4743
projectAlias: string,
4844
): Promise<void> {
49-
const existentProject = await this.repository.findOne(projectAlias);
45+
const project = await this.repository.findOne(projectAlias);
5046

51-
if (!!existentProject) {
47+
if (!!project) {
5248
const errorMessage = PROJECT_ALREADY_EXISTS(projectAlias);
5349

5450
console.error(errorMessage);

src/core/project/builder/project.builder.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import * as inquirer from 'inquirer';
22

33
import { Project } from '../project.entity';
44
import { createTask } from '../tasks/task.factory';
5-
import { BuildProjectQuestionCollection } from './questions.builder';
5+
import { buildProjectQuestions } from './questions.builder';
66

77
export async function buildProject(
88
alias: string,
99
path: string,
10-
questions: BuildProjectQuestionCollection,
1110
): Promise<Project> {
11+
const questions = buildProjectQuestions();
12+
1213
const answers = await inquirer.prompt(questions);
1314

1415
const project = new Project(alias, path);

0 commit comments

Comments
 (0)