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