-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathproject-commands.ts
201 lines (158 loc) · 6.43 KB
/
project-commands.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { Yok } from "../lib/common/yok";
import * as stubs from "./stubs";
import { CreateProjectCommand } from "../lib/commands/create-project";
import { StringCommandParameter } from "../lib/common/command-params";
import helpers = require("../lib/common/helpers");
import * as constants from "../lib/constants";
import { assert } from "chai";
import { PrompterStub } from "./stubs";
let selectedTemplateName: string;
let isProjectCreated: boolean;
let createProjectCalledWithForce: boolean;
let validateProjectCallsCount: number;
const dummyArgs = ["dummyArgsString"];
class ProjectServiceMock implements IProjectService {
async validateProjectName(opts: { projectName: string, force: boolean, pathToProject: string }): Promise<string> {
validateProjectCallsCount++;
return null;
}
async createProject(projectOptions: IProjectSettings): Promise<ICreateProjectData> {
createProjectCalledWithForce = projectOptions.force;
selectedTemplateName = projectOptions.template;
isProjectCreated = true;
return null;
}
isValidNativeScriptProject(pathToProject?: string): boolean {
return true;
}
}
class ProjectNameValidatorMock implements IProjectNameValidator {
public validate(name: string): boolean {
return true;
}
}
function createTestInjector() {
const testInjector = new Yok();
testInjector.register("injector", testInjector);
testInjector.register("staticConfig", {});
testInjector.register("projectService", ProjectServiceMock);
testInjector.register("errors", stubs.ErrorsStub);
testInjector.register("logger", stubs.LoggerStub);
testInjector.register("projectNameValidator", ProjectNameValidatorMock);
testInjector.register("options", {
ng: false,
template: undefined
});
testInjector.register("createCommand", CreateProjectCommand);
testInjector.register("stringParameter", StringCommandParameter);
testInjector.register("prompter", PrompterStub);
return testInjector;
}
describe("Project commands tests", () => {
let testInjector: IInjector;
let options: IOptions;
let createProjectCommand: ICommand;
function setupAnswers(opts: {
projectNameAnswer?: string,
flavorAnswer?: string,
templateAnswer?: string,
}) {
const prompterStub = <stubs.PrompterStub>testInjector.resolve("$prompter");
const choices: IDictionary<string> = {};
if (opts.projectNameAnswer) {
choices["First, what will be the name of your app?"] = opts.projectNameAnswer;
}
if (opts.flavorAnswer) {
choices[opts.projectNameAnswer ? "Next" : "First" + ", which flavor would you like to use?"] = opts.flavorAnswer;
}
if (opts.templateAnswer) {
choices[opts.projectNameAnswer ? "Finally" : "Next" + ", which template would you like to start from?"] = opts.templateAnswer;
}
prompterStub.expect({
choices
});
}
beforeEach(() => {
testInjector = createTestInjector();
helpers.isInteractive = () => true;
isProjectCreated = false;
validateProjectCallsCount = 0;
createProjectCalledWithForce = false;
selectedTemplateName = undefined;
options = testInjector.resolve("$options");
createProjectCommand = testInjector.resolve("$createCommand");
});
describe("#CreateProjectCommand", () => {
it("should not fail when using only --ng.", async () => {
options.ng = true;
await createProjectCommand.execute(dummyArgs);
assert.isTrue(isProjectCreated);
assert.equal(validateProjectCallsCount, 1);
assert.isTrue(createProjectCalledWithForce);
});
it("should not fail when using only --tsc.", async () => {
options.tsc = true;
await createProjectCommand.execute(dummyArgs);
assert.isTrue(isProjectCreated);
assert.equal(validateProjectCallsCount, 1);
assert.isTrue(createProjectCalledWithForce);
});
it("should not fail when using only --template.", async () => {
options.template = "ng";
await createProjectCommand.execute(dummyArgs);
assert.isTrue(isProjectCreated);
assert.equal(validateProjectCallsCount, 1);
assert.isTrue(createProjectCalledWithForce);
});
it("should set the template name correctly when used --ng.", async () => {
options.ng = true;
await createProjectCommand.execute(dummyArgs);
assert.deepEqual(selectedTemplateName, constants.ANGULAR_NAME);
assert.equal(validateProjectCallsCount, 1);
assert.isTrue(createProjectCalledWithForce);
});
it("should set the template name correctly when used --tsc.", async () => {
options.tsc = true;
await createProjectCommand.execute(dummyArgs);
assert.deepEqual(selectedTemplateName, constants.TYPESCRIPT_NAME);
assert.equal(validateProjectCallsCount, 1);
assert.isTrue(createProjectCalledWithForce);
});
it("should fail when --ng and --template are used simultaneously.", async () => {
options.ng = true;
options.template = "ng";
await assert.isRejected(createProjectCommand.execute(dummyArgs));
});
it("should fail when --tsc and --template are used simultaneously.", async () => {
options.tsc = true;
options.template = "tsc";
await assert.isRejected(createProjectCommand.execute(dummyArgs));
});
it("should ask for a template when ng flavor is selected.", async () => {
setupAnswers({ flavorAnswer: constants.NgFlavorName, templateAnswer: "Hello World" });
await createProjectCommand.execute(dummyArgs);
assert.deepEqual(selectedTemplateName, "tns-template-hello-world-ng");
assert.equal(validateProjectCallsCount, 1);
assert.isTrue(createProjectCalledWithForce);
});
it("should ask for a template when ts flavor is selected.", async () => {
setupAnswers({ flavorAnswer: constants.TsFlavorName, templateAnswer: "Hello World" });
await createProjectCommand.execute(dummyArgs);
assert.deepEqual(selectedTemplateName, "tns-template-hello-world-ts");
assert.equal(validateProjectCallsCount, 1);
assert.isTrue(createProjectCalledWithForce);
});
it("should ask for a template when js flavor is selected.", async () => {
setupAnswers({ flavorAnswer: constants.JsFlavorName, templateAnswer: "Hello World" });
await createProjectCommand.execute(dummyArgs);
assert.deepEqual(selectedTemplateName, "tns-template-hello-world");
assert.equal(validateProjectCallsCount, 1);
assert.isTrue(createProjectCalledWithForce);
});
it("should select the default vue template when the vue flavor is selected.", async () => {
setupAnswers({ flavorAnswer: constants.VueFlavorName });
await createProjectCommand.execute(dummyArgs);
assert.deepEqual(selectedTemplateName, "https://github.com/NativeScript/template-blank-vue/tarball/0.9.0");
});
});
});