@@ -9,7 +9,9 @@ const helpers = require("../../../lib/common/helpers");
99const originalIsInteractive = helpers . isInteractive ;
1010
1111const trackFeatureUsage = "TrackFeatureUsage" ;
12- const createTestInjector = ( ) : IInjector => {
12+ const sampleProjectType = "SampleProjectType" ;
13+
14+ const createTestInjector = ( opts ?: { projectHelperErrorMsg ?: string , projectDir ?: string } ) : IInjector => {
1315 const testInjector = new Yok ( ) ;
1416 testInjector . register ( "options" , { } ) ;
1517 testInjector . register ( "logger" , stubs . LoggerStub ) ;
@@ -37,8 +39,15 @@ const createTestInjector = (): IInjector => {
3739 testInjector . register ( "processService" , {
3840 attachToProcessExitSignals : ( context : any , callback : ( ) => void ) : void => undefined
3941 } ) ;
40- testInjector . register ( "projectDataService" , { } ) ;
42+ testInjector . register ( "projectDataService" , {
43+ getProjectData : ( projectDir ?: string ) : IProjectData => {
44+ return < any > {
45+ projectType : sampleProjectType
46+ } ;
47+ }
48+ } ) ;
4149 testInjector . register ( "mobileHelper" , { } ) ;
50+ testInjector . register ( "projectHelper" , new stubs . ProjectHelperStub ( opts && opts . projectHelperErrorMsg , opts && opts . projectDir ) ) ;
4251
4352 return testInjector ;
4453} ;
@@ -133,11 +142,11 @@ describe("analyticsService", () => {
133142
134143 assert . isTrue ( opts . isChildProcessSpawned ) ;
135144 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 } ` ) ;
137146 } ;
138147
139- const setupTest = ( expectedErrorMessage : string ) : any => {
140- const testInjector = createTestInjector ( ) ;
148+ const setupTest = ( expectedErrorMessage : string , projectHelperErrorMsg ?: string ) : any => {
149+ const testInjector = createTestInjector ( { projectHelperErrorMsg } ) ;
141150 const opts = {
142151 isChildProcessSpawned : false ,
143152 expectedErrorMessage
@@ -209,13 +218,33 @@ describe("analyticsService", () => {
209218
210219 await assertExpectedError ( testInjector , opts ) ;
211220 } ) ;
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+ } ) ;
212241 } ) ;
213242
214243 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 } => {
216245 helpers . isInteractive = ( ) => terminalOpts ? terminalOpts . isInteractive : true ;
217246
218- const testInjector = createTestInjector ( ) ;
247+ const testInjector = createTestInjector ( projectHelperOpts ) ;
219248 const opts = {
220249 isChildProcessSpawned : false ,
221250 expectedResult,
@@ -260,19 +289,38 @@ describe("analyticsService", () => {
260289 }
261290 } ) ;
262291
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+ expectedResult . customDimensions [ GoogleAnalyticsCustomDimensions . isShared ] = false . toString ( ) ;
303+ }
304+
305+ return expectedResult ;
306+ } ;
269307
270308 _ . each ( [ GoogleAnalyticsDataType . Page , GoogleAnalyticsDataType . Event ] , ( googleAnalyticsDataType : string ) => {
271309 it ( `when data is ${ googleAnalyticsDataType } ` , async ( ) => {
272310 const { testInjector, opts } = setupTest ( getExpectedResult ( googleAnalyticsDataType ) , getDataToSend ( googleAnalyticsDataType ) ) ;
273311 await assertExpectedResult ( testInjector , opts ) ;
274312 } ) ;
275313
314+ it ( `when data is ${ googleAnalyticsDataType } and command is executed from projectDir` , async ( ) => {
315+ const { testInjector, opts } = setupTest (
316+ getExpectedResult ( googleAnalyticsDataType , null , sampleProjectType ) ,
317+ getDataToSend ( googleAnalyticsDataType ) ,
318+ null ,
319+ { projectDir : "/some-dir" }
320+ ) ;
321+ await assertExpectedResult ( testInjector , opts ) ;
322+ } ) ;
323+
276324 it ( `when data is ${ googleAnalyticsDataType } and terminal is not interactive` , async ( ) => {
277325 const { testInjector, opts } = setupTest ( getExpectedResult ( googleAnalyticsDataType , AnalyticsClients . Unknown ) , getDataToSend ( googleAnalyticsDataType ) , { isInteractive : false } ) ;
278326 await assertExpectedResult ( testInjector , opts ) ;
0 commit comments