@studyportals/sp-r2d2
Version:
A framework that contains various components used when developing projects that will be deployed via AWS λ.
246 lines • 12.9 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const mocha_1 = require("@testdeck/mocha");
const Moq = require("typemoq");
const chai_1 = require("chai");
const lambda_handler_class_1 = require("../../environment/lambda/lambda-handler.class");
const environment_1 = require("../../environment");
const application_event_handler_class_1 = require("../../environment/core/event-handling/application-event-handler.class");
let LambdaHandlerTest = class LambdaHandlerTest {
constructor() {
this.event = 'SomeEvent';
}
get responseSender() {
return this.responseSenderMock.object;
}
get requestHandlerFactory() {
return this.requestHandlerFactoryMock.object;
}
get context() {
return this.contextMock.object;
}
get callback() {
return this.callbackMock.object;
}
get bootstrapper() {
return this.bootstrapperMock.object;
}
get eventTranslatorFactory() {
return this.eventTranslatorFactoryMock.object;
}
get bootstrapperFactory() {
return this.bootstrapperFactoryMock.object;
}
get responseSenderFactory() {
return this.responseSenderFactoryMock.object;
}
get eventHandler() {
return this.eventHandlerMock.object;
}
get testInstance() {
return this.testInstanceMock.object;
}
before() {
this.requestHandlerFactoryMock = Moq.Mock.ofType();
this.contextMock = Moq.Mock.ofType();
this.callbackMock = Moq.Mock.ofType();
this.responseSenderFactoryMock = Moq.Mock.ofType();
this.bootstrapperFactoryMock = Moq.Mock.ofType();
this.eventTranslatorFactoryMock = Moq.Mock.ofType();
this.responseSenderMock = Moq.Mock.ofType();
this.bootstrapperMock = Moq.Mock.ofType();
this.eventHandlerMock = Moq.Mock.ofType();
this.bootstrapperFactoryMock.setup((_) => _.create(Moq.It.isAny())).returns(() => this.bootstrapper);
this.responseSenderFactoryMock.setup((_) => _.create(Moq.It.isAny())).returns(() => this.responseSender);
this.testInstanceMock = Moq.Mock.ofType(lambda_handler_class_1.LambdaHandler);
this.testInstanceMock.callBase = true;
this.testInstanceMock.setup((_) => _['bootstrapperFactory']).returns(() => this.bootstrapperFactory);
this.testInstanceMock.setup((_) => _['eventTranslatorFactory']).returns(() => this.eventTranslatorFactory);
this.testInstanceMock.setup((_) => _['responseSenderFactory']).returns(() => this.responseSenderFactory);
this.testInstanceMock.setup((_) => _['requestHandlerFactory']).returns(() => this.requestHandlerFactory);
}
toFunction__CorrectlyRunsAndAwaitsTheHandler() {
return __awaiter(this, void 0, void 0, function* () {
let wasAwaited = false;
this.testInstanceMock
.setup((_) => _.run(Moq.It.isAny(), Moq.It.isAny(), Moq.It.isAny()))
.returns(() => new Promise((res) => {
setTimeout(() => {
wasAwaited = true;
res();
}, 10);
}));
const result = this.testInstance.toFunction();
yield result(this.event, this.context, this.callback);
this.testInstanceMock.verify((_) => _.run(this.event, this.context, this.callback), Moq.Times.once());
chai_1.assert.isTrue(wasAwaited);
});
}
run__ResponseSenderCorrectlyCreated() {
return __awaiter(this, void 0, void 0, function* () {
this.testInstanceMock.setup((_) => _['createEventHandler']()).returns(() => this.eventHandler);
yield this.testInstance.run(this.event, this.context, this.callback);
this.responseSenderFactoryMock.verify((_) => _.create(this.callback), Moq.Times.once());
});
}
run__BootstrapperCorrectlyCreated() {
return __awaiter(this, void 0, void 0, function* () {
this.testInstanceMock.setup((_) => _['createEventHandler']()).returns(() => this.eventHandler);
yield this.testInstance.run(this.event, this.context, this.callback);
this.bootstrapperFactoryMock.verify((_) => _.create(this.responseSender), Moq.Times.once());
});
}
run__CorrectlyBootstrapped() {
return __awaiter(this, void 0, void 0, function* () {
let awaited = false;
this.testInstanceMock.setup((_) => _['createEventHandler']()).returns(() => this.eventHandler);
this.bootstrapperMock
.setup((_) => _.bootstrap())
.returns(() => new Promise((_) => setImmediate(() => {
awaited = true;
_();
})));
yield this.testInstance.run(this.event, this.context, this.callback);
this.bootstrapperMock.verify((_) => _.bootstrap(), Moq.Times.once());
chai_1.assert.isTrue(awaited);
});
}
run__CorrectlyHandledTheEvent() {
return __awaiter(this, void 0, void 0, function* () {
let awaited = false;
this.testInstanceMock.setup((_) => _['createEventHandler']()).returns(() => this.eventHandler);
this.eventHandlerMock
.setup((_) => _.handle(Moq.It.isAny(), Moq.It.isAny()))
.returns(() => new Promise((_) => setImmediate(() => {
awaited = true;
_();
})));
yield this.testInstance.run(this.event, this.context, this.callback);
this.eventHandlerMock.verify((_) => _.handle(this.event, this.responseSender), Moq.Times.once());
chai_1.assert.isTrue(awaited);
});
}
run__CorrectlyCleanup() {
return __awaiter(this, void 0, void 0, function* () {
let awaited = false;
this.testInstanceMock.setup((_) => _['createEventHandler']()).returns(() => this.eventHandler);
this.bootstrapperMock
.setup((_) => _.cleanup())
.returns(() => new Promise((_) => setImmediate(() => {
awaited = true;
_();
})));
yield this.testInstance.run(this.event, this.context, this.callback);
this.bootstrapperMock.verify((_) => _.cleanup(), Moq.Times.once());
chai_1.assert.isTrue(awaited);
});
}
run__BootstrappedBeforeHandlingTheEvent() {
return __awaiter(this, void 0, void 0, function* () {
let callCounter = 0;
let handleEventCallNumber = 0;
let bootstrapCallNumber = 0;
this.testInstanceMock.setup((_) => _['createEventHandler']()).returns(() => this.eventHandler);
this.bootstrapperMock.setup((_) => _.bootstrap()).callback(() => (bootstrapCallNumber = ++callCounter));
this.eventHandlerMock
.setup((_) => _.handle(Moq.It.isAny(), Moq.It.isAny()))
.callback(() => (handleEventCallNumber = ++callCounter));
yield this.testInstance.run(this.event, this.context, this.callback);
chai_1.assert.isTrue(handleEventCallNumber >= bootstrapCallNumber);
});
}
run__CleanedUpAfterHandlingTheEvent() {
return __awaiter(this, void 0, void 0, function* () {
let callCounter = 0;
let handleEventCallNumber = 0;
let cleanupCallNumber = 0;
this.testInstanceMock.setup((_) => _['createEventHandler']()).returns(() => this.eventHandler);
this.bootstrapperMock.setup((_) => _.cleanup()).callback(() => (cleanupCallNumber = ++callCounter));
this.eventHandlerMock
.setup((_) => _.handle(Moq.It.isAny(), Moq.It.isAny()))
.callback(() => (handleEventCallNumber = ++callCounter));
yield this.testInstance.run(this.event, this.context, this.callback);
chai_1.assert.isTrue(cleanupCallNumber >= handleEventCallNumber);
});
}
run_HandleEventThrowsError_SendAndAwaitsError_StillExecutesCleanup() {
return __awaiter(this, void 0, void 0, function* () {
const error = new Error();
let wasAwaited = false;
this.testInstanceMock.setup((_) => _['createEventHandler']()).returns(() => this.eventHandler);
this.eventHandlerMock.setup((_) => _.handle(Moq.It.isAny(), Moq.It.isAny())).throws(error);
this.responseSenderMock
.setup((x) => x.sendUncaughtError(Moq.It.isAny()))
.returns(() => new Promise((res) => {
setTimeout(() => {
wasAwaited = true;
res();
}, 10);
}));
yield this.testInstance.run(this.event, this.context, this.callback);
this.responseSenderMock.verify((x) => x.sendUncaughtError(error), Moq.Times.once());
this.bootstrapperMock.verify((_) => _.cleanup(), Moq.Times.once());
chai_1.assert.isTrue(wasAwaited);
});
}
createEventHandler__EventHandlerCorrecltyInitialized() {
const testInstance = new lambda_handler_class_1.LambdaHandler(this.responseSenderFactory, this.bootstrapperFactory, this.eventTranslatorFactory, this.requestHandlerFactory);
const result = testInstance['createEventHandler']();
chai_1.assert.instanceOf(result, environment_1.WarmUpEventHandlerDecorator, 'WarmUpEventHandlerDecorator');
const pingDecoratorEventHandler = result['decoratedEventHandler'];
chai_1.assert.instanceOf(pingDecoratorEventHandler, environment_1.PingEventHandlerDecorator, 'PingEventHandlerDecorator');
const applicationEventHandler = pingDecoratorEventHandler['decoratedEventHandler'];
chai_1.assert.instanceOf(applicationEventHandler, application_event_handler_class_1.ApplicationEventHandler, 'ApplicationEventHandler');
chai_1.assert.equal(applicationEventHandler['eventTranslatorFactory'], this.eventTranslatorFactory);
chai_1.assert.equal(applicationEventHandler['requestHandlerFactory'], this.requestHandlerFactory);
}
};
__decorate([
mocha_1.test
], LambdaHandlerTest.prototype, "toFunction__CorrectlyRunsAndAwaitsTheHandler", null);
__decorate([
mocha_1.test
], LambdaHandlerTest.prototype, "run__ResponseSenderCorrectlyCreated", null);
__decorate([
mocha_1.test
], LambdaHandlerTest.prototype, "run__BootstrapperCorrectlyCreated", null);
__decorate([
mocha_1.test
], LambdaHandlerTest.prototype, "run__CorrectlyBootstrapped", null);
__decorate([
mocha_1.test
], LambdaHandlerTest.prototype, "run__CorrectlyHandledTheEvent", null);
__decorate([
mocha_1.test
], LambdaHandlerTest.prototype, "run__CorrectlyCleanup", null);
__decorate([
mocha_1.test
], LambdaHandlerTest.prototype, "run__BootstrappedBeforeHandlingTheEvent", null);
__decorate([
mocha_1.test
], LambdaHandlerTest.prototype, "run__CleanedUpAfterHandlingTheEvent", null);
__decorate([
mocha_1.test
], LambdaHandlerTest.prototype, "run_HandleEventThrowsError_SendAndAwaitsError_StillExecutesCleanup", null);
__decorate([
mocha_1.test
], LambdaHandlerTest.prototype, "createEventHandler__EventHandlerCorrecltyInitialized", null);
LambdaHandlerTest = __decorate([
(0, mocha_1.suite)()
], LambdaHandlerTest);
//# sourceMappingURL=lambda-handler.test.js.map