@studyportals/sp-r2d2
Version:
A framework that contains various components used when developing projects that will be deployed via AWS λ.
151 lines • 8.28 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 environment_1 = require("../../../environment");
let WarmUpEventHandlerDecoratorTest = class WarmUpEventHandlerDecoratorTest {
constructor() {
this.event = 'SomeEvent';
}
get eventHandler() {
return this.eventHandlerMock.object;
}
get responseSender() {
return this.responseSenderMock.object;
}
get testInstance() {
return this.testInstanceMock.object;
}
before() {
this.eventHandlerMock = Moq.Mock.ofType();
this.responseSenderMock = Moq.Mock.ofType();
this.testInstanceMock = Moq.Mock.ofType(environment_1.WarmUpEventHandlerDecorator);
this.testInstanceMock.callBase = true;
this.testInstanceMock.setup((_) => _['decoratedEventHandler']).returns(() => this.eventHandler);
}
handle_WamUpEvent_EventIgnoredAfterTimeout() {
return __awaiter(this, void 0, void 0, function* () {
let awaited = false;
this.testInstanceMock.setup((_) => _.isWarmUpEvent(Moq.It.isAny())).returns(() => __awaiter(this, void 0, void 0, function* () { return true; }));
this.testInstanceMock
.setup((_) => _.sendEventIgnoredAfterTimeout(Moq.It.isAny(), Moq.It.isAny(), Moq.It.isAny()))
.returns(() => new Promise((_) => setImmediate(() => {
awaited = true;
_();
})));
yield this.testInstance.handle(this.event, this.responseSender);
this.testInstanceMock.verify((_) => _.sendEventIgnoredAfterTimeout(this.responseSender, this.event, 75), Moq.Times.once());
this.testInstanceMock.verify((_) => _.isWarmUpEvent(this.event), Moq.Times.once());
chai_1.assert.isTrue(awaited);
});
}
handle_NotWamUpEvent_EventNotIgnored() {
return __awaiter(this, void 0, void 0, function* () {
this.testInstanceMock.setup((_) => _.isWarmUpEvent(Moq.It.isAny())).returns(() => __awaiter(this, void 0, void 0, function* () { return false; }));
this.testInstanceMock.setup((_) => _.sendEventIgnoredAfterTimeout(Moq.It.isAny(), Moq.It.isAny(), Moq.It.isAny()));
yield this.testInstance.handle(this.event, this.responseSender);
this.testInstanceMock.verify((_) => _.sendEventIgnoredAfterTimeout(Moq.It.isAny(), Moq.It.isAny(), Moq.It.isAny()), Moq.Times.never());
});
}
handle_NotWamUpEvent_HandlingPassedToDecoratedHandler() {
return __awaiter(this, void 0, void 0, function* () {
let awaited = false;
this.testInstanceMock.setup((_) => _.isWarmUpEvent(Moq.It.isAny())).returns(() => __awaiter(this, void 0, void 0, function* () { return false; }));
this.testInstanceMock.setup((_) => _.sendEventIgnoredAfterTimeout(Moq.It.isAny(), Moq.It.isAny(), Moq.It.isAny()));
this.eventHandlerMock
.setup((_) => _.handle(Moq.It.isAny(), Moq.It.isAny()))
.returns(() => new Promise((_) => setImmediate(() => {
awaited = true;
_();
})));
yield this.testInstance.handle(this.event, this.responseSender);
this.eventHandlerMock.verify((_) => _.handle(this.event, this.responseSender), Moq.Times.once());
chai_1.assert.isTrue(awaited);
});
}
handle_WamUpEvent_HandlingNotPassedToDecoratedHandler() {
return __awaiter(this, void 0, void 0, function* () {
this.testInstanceMock.setup((_) => _.isWarmUpEvent(Moq.It.isAny())).returns(() => __awaiter(this, void 0, void 0, function* () { return true; }));
this.testInstanceMock.setup((_) => _.sendEventIgnoredAfterTimeout(Moq.It.isAny(), Moq.It.isAny(), Moq.It.isAny()));
yield this.testInstance.handle(this.event, this.responseSender);
this.eventHandlerMock.verify((_) => _.handle(Moq.It.isAny(), Moq.It.isAny()), Moq.Times.never());
});
}
isWarmUpEvent_WarmUpEvent_True() {
return __awaiter(this, void 0, void 0, function* () {
chai_1.assert.isTrue(yield this.testInstance.isWarmUpEvent(this.createWarmUpEvent()));
});
}
isWarmUpEvent_NotWarmUpEvent_False() {
return __awaiter(this, void 0, void 0, function* () {
const pingEvent = this.createWarmUpEvent();
pingEvent.source = 'SomeOtherSource';
chai_1.assert.isFalse(yield this.testInstance.isWarmUpEvent(pingEvent));
});
}
sendEventIgnoredAfterTimeout__EventIgnoredSent() {
return __awaiter(this, void 0, void 0, function* () {
yield this.testInstance.sendEventIgnoredAfterTimeout(this.responseSender, this.event, 0);
this.responseSenderMock.verify((_) => _.sendEventIgnored(this.event), Moq.Times.once());
});
}
sendEventIgnoredAfterTimeout__EventIgnoredSentAfterTimeout() {
return __awaiter(this, void 0, void 0, function* () {
const timeout = 100;
const beforeTimeStamp = new Date();
yield this.testInstance.sendEventIgnoredAfterTimeout(this.responseSender, this.event, timeout);
const afterTimeStamp = new Date();
const elapsedMilliseconds = afterTimeStamp.getTime() - beforeTimeStamp.getTime();
chai_1.assert.isTrue(elapsedMilliseconds >= timeout);
});
}
createWarmUpEvent() {
return {
source: 'serverless-plugin-warmup',
};
}
};
__decorate([
mocha_1.test
], WarmUpEventHandlerDecoratorTest.prototype, "handle_WamUpEvent_EventIgnoredAfterTimeout", null);
__decorate([
mocha_1.test
], WarmUpEventHandlerDecoratorTest.prototype, "handle_NotWamUpEvent_EventNotIgnored", null);
__decorate([
mocha_1.test
], WarmUpEventHandlerDecoratorTest.prototype, "handle_NotWamUpEvent_HandlingPassedToDecoratedHandler", null);
__decorate([
mocha_1.test
], WarmUpEventHandlerDecoratorTest.prototype, "handle_WamUpEvent_HandlingNotPassedToDecoratedHandler", null);
__decorate([
mocha_1.test
], WarmUpEventHandlerDecoratorTest.prototype, "isWarmUpEvent_WarmUpEvent_True", null);
__decorate([
mocha_1.test
], WarmUpEventHandlerDecoratorTest.prototype, "isWarmUpEvent_NotWarmUpEvent_False", null);
__decorate([
mocha_1.test
], WarmUpEventHandlerDecoratorTest.prototype, "sendEventIgnoredAfterTimeout__EventIgnoredSent", null);
__decorate([
mocha_1.test
], WarmUpEventHandlerDecoratorTest.prototype, "sendEventIgnoredAfterTimeout__EventIgnoredSentAfterTimeout", null);
WarmUpEventHandlerDecoratorTest = __decorate([
(0, mocha_1.suite)()
], WarmUpEventHandlerDecoratorTest);
//# sourceMappingURL=warm-up-event-handler-decorator.test.js.map