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