@@ -9,7 +9,9 @@ const helpers = require("../../../lib/common/helpers");
9
9
const originalIsInteractive = helpers . isInteractive ;
10
10
11
11
const trackFeatureUsage = "TrackFeatureUsage" ;
12
- const createTestInjector = ( ) : IInjector => {
12
+ const sampleProjectType = "SampleProjectType" ;
13
+
14
+ const createTestInjector = ( opts ?: { projectHelperErrorMsg ?: string , projectDir ?: string } ) : IInjector => {
13
15
const testInjector = new Yok ( ) ;
14
16
testInjector . register ( "options" , { } ) ;
15
17
testInjector . register ( "logger" , stubs . LoggerStub ) ;
@@ -37,8 +39,15 @@ const createTestInjector = (): IInjector => {
37
39
testInjector . register ( "processService" , {
38
40
attachToProcessExitSignals : ( context : any , callback : ( ) => void ) : void => undefined
39
41
} ) ;
40
- testInjector . register ( "projectDataService" , { } ) ;
42
+ testInjector . register ( "projectDataService" , {
43
+ getProjectData : ( projectDir ?: string ) : IProjectData => {
44
+ return < any > {
45
+ projectType : sampleProjectType
46
+ } ;
47
+ }
48
+ } ) ;
41
49
testInjector . register ( "mobileHelper" , { } ) ;
50
+ testInjector . register ( "projectHelper" , new stubs . ProjectHelperStub ( opts && opts . projectHelperErrorMsg , opts && opts . projectDir ) ) ;
42
51
43
52
return testInjector ;
44
53
} ;
@@ -133,11 +142,11 @@ describe("analyticsService", () => {
133
142
134
143
assert . isTrue ( opts . isChildProcessSpawned ) ;
135
144
const logger = testInjector . resolve < stubs . LoggerStub > ( "logger" ) ;
136
- assert . isTrue ( logger . traceOutput . indexOf ( opts . expectedErrorMessage ) !== - 1 ) ;
145
+ assert . isTrue ( logger . traceOutput . indexOf ( opts . expectedErrorMessage ) !== - 1 , `Tried to find error ' ${ opts . expectedErrorMessage } ', but couldn't. logger's trace output is: ${ logger . traceOutput } ` ) ;
137
146
} ;
138
147
139
- const setupTest = ( expectedErrorMessage : string ) : any => {
140
- const testInjector = createTestInjector ( ) ;
148
+ const setupTest = ( expectedErrorMessage : string , projectHelperErrorMsg ?: string ) : any => {
149
+ const testInjector = createTestInjector ( { projectHelperErrorMsg } ) ;
141
150
const opts = {
142
151
isChildProcessSpawned : false ,
143
152
expectedErrorMessage
@@ -209,13 +218,33 @@ describe("analyticsService", () => {
209
218
210
219
await assertExpectedError ( testInjector , opts ) ;
211
220
} ) ;
221
+
222
+ it ( "when trying to get projectDir from projectHelper fails" , async ( ) => {
223
+ const projectHelperErrorMsg = "Failed to find project directory." ;
224
+ const { testInjector, childProcess, opts } = setupTest ( `Unable to get the projectDir from projectHelper Error: ${ projectHelperErrorMsg } ` , projectHelperErrorMsg ) ;
225
+ childProcess . spawn = ( command : string , args ?: string [ ] , options ?: any ) : any => {
226
+ opts . isChildProcessSpawned = true ;
227
+ const spawnedProcess : any = getSpawnedProcess ( ) ;
228
+
229
+ spawnedProcess . connected = true ;
230
+ spawnedProcess . send = ( msg : any , action : ( ) => void ) : void => {
231
+ opts . messageSent = msg ;
232
+ action ( ) ;
233
+ } ;
234
+
235
+ setTimeout ( ( ) => spawnedProcess . emit ( "message" , AnalyticsMessages . BrokerReadyToReceive ) , 1 ) ;
236
+ return spawnedProcess ;
237
+ } ;
238
+
239
+ await assertExpectedError ( testInjector , opts ) ;
240
+ } ) ;
212
241
} ) ;
213
242
214
243
describe ( "sends correct message to broker" , ( ) => {
215
- const setupTest = ( expectedResult : any , dataToSend : any , terminalOpts ?: { isInteractive : boolean } ) : { testInjector : IInjector , opts : any } => {
244
+ const setupTest = ( expectedResult : any , dataToSend : any , terminalOpts ?: { isInteractive : boolean } , projectHelperOpts ?: { projectDir : string } ) : { testInjector : IInjector , opts : any } => {
216
245
helpers . isInteractive = ( ) => terminalOpts ? terminalOpts . isInteractive : true ;
217
246
218
- const testInjector = createTestInjector ( ) ;
247
+ const testInjector = createTestInjector ( projectHelperOpts ) ;
219
248
const opts = {
220
249
isChildProcessSpawned : false ,
221
250
expectedResult,
@@ -260,19 +289,37 @@ describe("analyticsService", () => {
260
289
}
261
290
} ) ;
262
291
263
- const getExpectedResult = ( gaDataType : string , analyticsClient ?: string ) : any => ( {
264
- type : "googleAnalyticsData" ,
265
- category : "CLI" ,
266
- googleAnalyticsDataType : gaDataType ,
267
- customDimensions : { customDimension1 : "value1" , cd5 : analyticsClient || "CLI" }
268
- } ) ;
292
+ const getExpectedResult = ( gaDataType : string , analyticsClient ?: string , projectType ?: string ) : any => {
293
+ const expectedResult : any = {
294
+ type : "googleAnalyticsData" ,
295
+ category : "CLI" ,
296
+ googleAnalyticsDataType : gaDataType ,
297
+ customDimensions : { customDimension1 : "value1" , cd5 : analyticsClient || "CLI" }
298
+ } ;
299
+
300
+ if ( projectType ) {
301
+ expectedResult . customDimensions [ GoogleAnalyticsCustomDimensions . projectType ] = projectType ;
302
+ }
303
+
304
+ return expectedResult ;
305
+ } ;
269
306
270
307
_ . each ( [ GoogleAnalyticsDataType . Page , GoogleAnalyticsDataType . Event ] , ( googleAnalyticsDataType : string ) => {
271
308
it ( `when data is ${ googleAnalyticsDataType } ` , async ( ) => {
272
309
const { testInjector, opts } = setupTest ( getExpectedResult ( googleAnalyticsDataType ) , getDataToSend ( googleAnalyticsDataType ) ) ;
273
310
await assertExpectedResult ( testInjector , opts ) ;
274
311
} ) ;
275
312
313
+ it ( `when data is ${ googleAnalyticsDataType } and command is executed from projectDir` , async ( ) => {
314
+ const { testInjector, opts } = setupTest (
315
+ getExpectedResult ( googleAnalyticsDataType , null , sampleProjectType ) ,
316
+ getDataToSend ( googleAnalyticsDataType ) ,
317
+ null ,
318
+ { projectDir : "/some-dir" }
319
+ ) ;
320
+ await assertExpectedResult ( testInjector , opts ) ;
321
+ } ) ;
322
+
276
323
it ( `when data is ${ googleAnalyticsDataType } and terminal is not interactive` , async ( ) => {
277
324
const { testInjector, opts } = setupTest ( getExpectedResult ( googleAnalyticsDataType , AnalyticsClients . Unknown ) , getDataToSend ( googleAnalyticsDataType ) , { isInteractive : false } ) ;
278
325
await assertExpectedResult ( testInjector , opts ) ;
0 commit comments