@@ -4,13 +4,26 @@ import { assert } from "chai";
4
4
import { CacheDecoratorsTest } from "./mocks/decorators-cache" ;
5
5
import { InvokeBeforeDecoratorsTest } from "./mocks/decorators-invoke-before" ;
6
6
import { isPromise } from "../../helpers" ;
7
+ import * as stubs from "../../../../test/stubs" ;
8
+ import * as sinon from "sinon" ;
9
+ import { PerformanceService } from "../../../services/performance-service" ;
7
10
8
11
describe ( "decorators" , ( ) => {
9
- const moduleName = "moduleName" , // This is the name of the injected dependency that will be resolved, for example fs, devicesService, etc.
10
- propertyName = "propertyName" ; // This is the name of the method/property from the resolved module
12
+ const moduleName = "moduleName" ; // This is the name of the injected dependency that will be resolved, for example fs, devicesService, etc.
13
+ const propertyName = "propertyName" ; // This is the name of the method/property from the resolved module
14
+ const expectedResults : any [ ] = [
15
+ "string result" ,
16
+ 1 ,
17
+ { a : 1 , b : "2" } ,
18
+ [ "string 1" , "string2" ] ,
19
+ true ,
20
+ undefined ,
21
+ null
22
+ ] ;
11
23
12
24
beforeEach ( ( ) => {
13
25
$injector = new Yok ( ) ;
26
+ $injector . register ( "performanceService" , stubs . PerformanceService ) ;
14
27
} ) ;
15
28
16
29
after ( ( ) => {
@@ -19,15 +32,6 @@ describe("decorators", () => {
19
32
} ) ;
20
33
21
34
describe ( "exported" , ( ) => {
22
- const expectedResults : any [ ] = [
23
- "string result" ,
24
- 1 ,
25
- { a : 1 , b : "2" } ,
26
- [ "string 1" , "string2" ] ,
27
- true ,
28
- undefined ,
29
- null
30
- ] ;
31
35
32
36
const generatePublicApiFromExportedDecorator = ( ) => {
33
37
assert . deepEqual ( $injector . publicApi . __modules__ [ moduleName ] , undefined ) ;
@@ -358,4 +362,128 @@ describe("decorators", () => {
358
362
} ) ;
359
363
} ) ;
360
364
} ) ;
365
+
366
+ describe ( "performanceLog" , ( ) => {
367
+ const testErrorMessage = "testError" ;
368
+ let testInjector : IInjector ;
369
+ let sandbox : sinon . SinonSandbox ;
370
+ interface ITestInterface {
371
+ testMethod ( arg : any ) : any ;
372
+ throwMethod ?( ) : void ;
373
+ testAsyncMehtod ( arg : any ) : Promise < any > ;
374
+ rejectMethod ?( ) : Promise < any > ;
375
+ }
376
+ let testInstance : ITestInterface ;
377
+ let undecoratedTestInstance : ITestInterface ;
378
+
379
+ function createTestInjector ( ) : IInjector {
380
+ testInjector = new Yok ( ) ;
381
+ testInjector . register ( "performanceService" , PerformanceService ) ;
382
+ testInjector . register ( "options" , { } ) ;
383
+ testInjector . register ( "fs" , stubs . FileSystemStub ) ;
384
+ testInjector . register ( "logger" , stubs . LoggerStub ) ;
385
+ testInjector . register ( "analyticsService" , {
386
+ trackEventActionInGoogleAnalytics : ( ) => { return Promise . resolve ( ) ; }
387
+ } ) ;
388
+
389
+ return testInjector ;
390
+ }
391
+
392
+ beforeEach ( ( ) => {
393
+ sandbox = sinon . sandbox . create ( ) ;
394
+ testInjector = createTestInjector ( ) ;
395
+
396
+ class TestClass implements ITestInterface {
397
+ @decoratorsLib . performanceLog ( testInjector )
398
+ testMethod ( arg : any ) {
399
+ return arg ;
400
+ }
401
+
402
+ @decoratorsLib . performanceLog ( testInjector )
403
+ throwMethod ( ) {
404
+ throw new Error ( "testErrorMessage" ) ;
405
+ }
406
+
407
+ @decoratorsLib . performanceLog ( testInjector )
408
+ async testAsyncMehtod ( arg : any ) {
409
+ return Promise . resolve ( arg ) ;
410
+ }
411
+
412
+ rejectMethod ( ) {
413
+ return Promise . reject ( testErrorMessage ) ;
414
+ }
415
+ }
416
+
417
+ class UndecoratedTestClass implements ITestInterface {
418
+ testMethod ( arg : any ) {
419
+ return arg ;
420
+ }
421
+
422
+ async testAsyncMehtod ( arg : any ) {
423
+ return Promise . resolve ( arg ) ;
424
+ }
425
+ }
426
+
427
+ undecoratedTestInstance = new UndecoratedTestClass ( ) ;
428
+ testInstance = new TestClass ( ) ;
429
+ } ) ;
430
+
431
+ afterEach ( ( ) => {
432
+ sandbox . restore ( ) ;
433
+ } ) ;
434
+
435
+ _ . each ( expectedResults , ( expectedResult : any ) => {
436
+ it ( "returns proper result" , ( ) => {
437
+ const actualResult = testInstance . testMethod ( expectedResult ) ;
438
+ assert . deepEqual ( actualResult , expectedResult ) ;
439
+ } ) ;
440
+
441
+ it ( "returns proper result when async" , ( ) => {
442
+ const promise = testInstance . testAsyncMehtod ( expectedResult ) ;
443
+
444
+ assert . notDeepEqual ( promise . then , undefined ) ;
445
+
446
+ return promise . then ( ( actualResult : any ) => {
447
+ assert . deepEqual ( actualResult , expectedResult ) ;
448
+ } ) ;
449
+ } ) ;
450
+ } ) ;
451
+
452
+ it ( "method has same toString" , ( ) => {
453
+ assert . equal ( testInstance . testMethod . toString ( ) , undecoratedTestInstance . testMethod . toString ( ) ) ;
454
+ } ) ;
455
+
456
+ it ( "method has same name" , ( ) => {
457
+ assert . equal ( testInstance . testMethod . name , undecoratedTestInstance . testMethod . name ) ;
458
+ } ) ;
459
+
460
+ it ( "does not eat errors" , ( ) => {
461
+ assert . throws ( testInstance . throwMethod , testErrorMessage ) ;
462
+ assert . isRejected ( testInstance . rejectMethod ( ) , testErrorMessage ) ;
463
+ } ) ;
464
+
465
+ it ( "calls performance service on method call" , async ( ) => {
466
+ const performanceService = testInjector . resolve ( "performanceService" ) ;
467
+ const processExecutionDataStub : sinon . SinonStub = sinon . stub ( performanceService , "processExecutionData" ) ;
468
+
469
+ const checkSubCall = ( call : sinon . SinonSpyCall , methodData : string ) => {
470
+ const callArgs = call . args ;
471
+ const methodInfo = callArgs [ 0 ] ;
472
+ const startTime = callArgs [ 1 ] ;
473
+ const endTime = callArgs [ 2 ] ;
474
+
475
+ assert ( methodInfo === methodData ) ;
476
+ assert . isNumber ( startTime ) ;
477
+ assert . isNumber ( endTime ) ;
478
+ assert . isTrue ( endTime > startTime ) ;
479
+ assert . isDefined ( callArgs [ 3 ] [ 0 ] === "test" ) ;
480
+ } ;
481
+
482
+ testInstance . testMethod ( "test" ) ;
483
+ await testInstance . testAsyncMehtod ( "test" ) ;
484
+
485
+ checkSubCall ( processExecutionDataStub . firstCall , "TestClass__testMethod" ) ;
486
+ checkSubCall ( processExecutionDataStub . secondCall , "TestClass__testAsyncMehtod" ) ;
487
+ } ) ;
488
+ } ) ;
361
489
} ) ;
0 commit comments