Skip to content

Commit 5265bba

Browse files
committed
test: add tests for performaceLog decorator
1 parent 7d636c0 commit 5265bba

File tree

1 file changed

+139
-13
lines changed

1 file changed

+139
-13
lines changed

lib/common/test/unit-tests/decorators.ts

+139-13
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,26 @@ import { assert } from "chai";
44
import { CacheDecoratorsTest } from "./mocks/decorators-cache";
55
import { InvokeBeforeDecoratorsTest } from "./mocks/decorators-invoke-before";
66
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";
810

911
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+
];
1223

1324
beforeEach(() => {
1425
$injector = new Yok();
15-
$injector.register("performanceService", PerformanceService);
26+
$injector.register("performanceService", stubs.PerformanceService);
1627
});
1728

1829
after(() => {
@@ -21,15 +32,6 @@ describe("decorators", () => {
2132
});
2233

2334
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-
];
3335

3436
const generatePublicApiFromExportedDecorator = () => {
3537
assert.deepEqual($injector.publicApi.__modules__[moduleName], undefined);
@@ -360,4 +362,128 @@ describe("decorators", () => {
360362
});
361363
});
362364
});
365+
366+
describe.only("performanceLog", () => {
367+
const testErrorMessage = "testError";
368+
let testInjector: IInjector;
369+
let sandbox: sinon.SinonSandbox;
370+
interface testInterface {
371+
testMethod(arg: any): any
372+
throwMethod?(): void
373+
testAsyncMehtod(arg: any): Promise<any>
374+
rejectMethod?(): Promise<any>
375+
};
376+
let testInstance: testInterface;
377+
let undecoratedTestInstance: testInterface;
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 testInterface {
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 testInterface {
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+
});
363489
});

0 commit comments

Comments
 (0)