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