UNPKG

@studyportals/sp-r2d2

Version:

A framework that contains various components used when developing projects that will be deployed via AWS λ.

199 lines 11.2 kB
"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 application_1 = require("../../../application"); const environment_1 = require("../../../environment"); let ApplicationEventHandlerTest = class ApplicationEventHandlerTest { constructor() { this.event = 'SomeEvent'; } get request() { return this.requestMock.object; } get requestHandler() { return this.requestHandlerMock.object; } get eventTranslator() { return this.eventTranslatorMock.object; } get responseSender() { return this.responseSenderMock.object; } get eventTranslatorFactory() { return this.eventTranslatorFactoryMock.object; } get requestHandlerFactory() { return this.requestHandlerFactoryMock.object; } get testInstance() { return this.testInstanceMock.object; } before() { this.eventTranslatorFactoryMock = Moq.Mock.ofType(); this.requestHandlerFactoryMock = Moq.Mock.ofType(); this.requestMock = Moq.Mock.ofType(); this.requestHandlerMock = Moq.Mock.ofType(); this.responseSenderMock = Moq.Mock.ofType(); this.eventTranslatorMock = Moq.Mock.ofType(); this.requestMock.setup((_) => _.then).returns(() => undefined); this.eventTranslatorFactoryMock.setup((_) => _.create()).returns(() => this.eventTranslator); this.eventTranslatorMock.setup((_) => _.translate(Moq.It.isAny())).returns(() => __awaiter(this, void 0, void 0, function* () { return this.request; })); this.requestHandlerFactoryMock.setup((_) => _.create()).returns(() => this.requestHandler); this.testInstanceMock = Moq.Mock.ofType(environment_1.ApplicationEventHandler); this.testInstanceMock.callBase = true; this.testInstanceMock.setup((_) => _['eventTranslatorFactory']).returns(() => this.eventTranslatorFactory); this.testInstanceMock.setup((_) => _['requestHandlerFactory']).returns(() => this.requestHandlerFactory); } handle__EventCorrectlyTranslated() { return __awaiter(this, void 0, void 0, function* () { yield this.testInstance.handle(this.event, this.responseSender); this.eventTranslatorMock.verify((_) => _.translate(this.event), Moq.Times.once()); }); } handle__RequestCorrectlyHandled() { return __awaiter(this, void 0, void 0, function* () { yield this.testInstance.handle(this.event, this.responseSender); this.requestHandlerMock.verify((_) => _.handle(this.request), Moq.Times.once()); }); } handle_SuccessfulExecution_OutcomeCorrectlySent() { return __awaiter(this, void 0, void 0, function* () { const result = 'SomeResult'; let awaited = false; this.requestHandlerMock.setup((_) => _.handle(Moq.It.isAny())).returns(() => __awaiter(this, void 0, void 0, function* () { return result; })); this.responseSenderMock .setup((_) => _.sendExecutionOutcome(Moq.It.isAny())) .returns(() => new Promise((_) => setImmediate(() => { awaited = true; _(); }))); yield this.testInstance.handle(this.event, this.responseSender); this.responseSenderMock.verify((_) => _.sendExecutionOutcome(result), Moq.Times.once()); chai_1.assert.isTrue(awaited); this.responseSenderMock.verify((_) => _.sendRequestValidationError(Moq.It.isAny()), Moq.Times.never()); this.responseSenderMock.verify((_) => _.sendRequestAuthorizationError(Moq.It.isAny()), Moq.Times.never()); this.responseSenderMock.verify((_) => _.sendRequestExecutionError(Moq.It.isAny()), Moq.Times.never()); this.responseSenderMock.verify((_) => _.sendUncaughtError(Moq.It.isAny()), Moq.Times.never()); }); } handle_ValidationError_ErrorCorrectlySent() { return __awaiter(this, void 0, void 0, function* () { const error = new application_1.ValidationError(); let awaited = false; this.requestHandlerMock.setup((_) => _.handle(Moq.It.isAny())).throws(error); this.responseSenderMock .setup((_) => _.sendRequestValidationError(Moq.It.isAny())) .returns(() => new Promise((_) => setImmediate(() => { awaited = true; _(); }))); yield this.testInstance.handle(this.event, this.responseSender); this.responseSenderMock.verify((_) => _.sendRequestValidationError(error), Moq.Times.once()); chai_1.assert.isTrue(awaited); this.responseSenderMock.verify((_) => _.sendExecutionOutcome(Moq.It.isAny()), Moq.Times.never()); this.responseSenderMock.verify((_) => _.sendRequestAuthorizationError(Moq.It.isAny()), Moq.Times.never()); this.responseSenderMock.verify((_) => _.sendRequestExecutionError(Moq.It.isAny()), Moq.Times.never()); this.responseSenderMock.verify((_) => _.sendUncaughtError(Moq.It.isAny()), Moq.Times.never()); }); } handle_AuthorizationError_ErrorCorrectlySent() { return __awaiter(this, void 0, void 0, function* () { const error = new application_1.AuthorizationError(); let awaited = false; this.requestHandlerMock.setup((_) => _.handle(Moq.It.isAny())).throws(error); this.responseSenderMock .setup((_) => _.sendRequestAuthorizationError(Moq.It.isAny())) .returns(() => new Promise((_) => setImmediate(() => { awaited = true; _(); }))); yield this.testInstance.handle(this.event, this.responseSender); this.responseSenderMock.verify((_) => _.sendRequestAuthorizationError(error), Moq.Times.once()); chai_1.assert.isTrue(awaited); this.responseSenderMock.verify((_) => _.sendExecutionOutcome(Moq.It.isAny()), Moq.Times.never()); this.responseSenderMock.verify((_) => _.sendRequestValidationError(Moq.It.isAny()), Moq.Times.never()); this.responseSenderMock.verify((_) => _.sendRequestExecutionError(Moq.It.isAny()), Moq.Times.never()); this.responseSenderMock.verify((_) => _.sendUncaughtError(Moq.It.isAny()), Moq.Times.never()); }); } handle_ExecutionError_ErrorCorrectlySent() { return __awaiter(this, void 0, void 0, function* () { const error = new application_1.ExecutionError(); let awaited = false; this.requestHandlerMock.setup((_) => _.handle(Moq.It.isAny())).throws(error); this.responseSenderMock .setup((_) => _.sendRequestExecutionError(Moq.It.isAny())) .returns(() => new Promise((_) => setImmediate(() => { awaited = true; _(); }))); yield this.testInstance.handle(this.event, this.responseSender); this.responseSenderMock.verify((_) => _.sendRequestExecutionError(error), Moq.Times.once()); chai_1.assert.isTrue(awaited); this.responseSenderMock.verify((_) => _.sendExecutionOutcome(Moq.It.isAny()), Moq.Times.never()); this.responseSenderMock.verify((_) => _.sendRequestValidationError(Moq.It.isAny()), Moq.Times.never()); this.responseSenderMock.verify((_) => _.sendRequestAuthorizationError(Moq.It.isAny()), Moq.Times.never()); this.responseSenderMock.verify((_) => _.sendUncaughtError(Moq.It.isAny()), Moq.Times.never()); }); } handle_Error_ErrorThrown() { return __awaiter(this, void 0, void 0, function* () { const error = new Error(); let errorThrown; this.requestHandlerMock.setup((_) => _.handle(Moq.It.isAny())).throws(error); try { yield this.testInstance.handle(this.event, this.responseSender); } catch (e) { errorThrown = e; } chai_1.assert.equal(errorThrown, error); this.responseSenderMock.verify((_) => _.sendExecutionOutcome(Moq.It.isAny()), Moq.Times.never()); this.responseSenderMock.verify((_) => _.sendRequestValidationError(Moq.It.isAny()), Moq.Times.never()); this.responseSenderMock.verify((_) => _.sendRequestAuthorizationError(Moq.It.isAny()), Moq.Times.never()); this.responseSenderMock.verify((_) => _.sendRequestExecutionError(Moq.It.isAny()), Moq.Times.never()); }); } }; __decorate([ mocha_1.test ], ApplicationEventHandlerTest.prototype, "handle__EventCorrectlyTranslated", null); __decorate([ mocha_1.test ], ApplicationEventHandlerTest.prototype, "handle__RequestCorrectlyHandled", null); __decorate([ mocha_1.test ], ApplicationEventHandlerTest.prototype, "handle_SuccessfulExecution_OutcomeCorrectlySent", null); __decorate([ mocha_1.test ], ApplicationEventHandlerTest.prototype, "handle_ValidationError_ErrorCorrectlySent", null); __decorate([ mocha_1.test ], ApplicationEventHandlerTest.prototype, "handle_AuthorizationError_ErrorCorrectlySent", null); __decorate([ mocha_1.test ], ApplicationEventHandlerTest.prototype, "handle_ExecutionError_ErrorCorrectlySent", null); __decorate([ mocha_1.test ], ApplicationEventHandlerTest.prototype, "handle_Error_ErrorThrown", null); ApplicationEventHandlerTest = __decorate([ (0, mocha_1.suite)() ], ApplicationEventHandlerTest); //# sourceMappingURL=application-event-handler.test.js.map