UNPKG

miter

Version:

A typescript web framework based on ExpressJs based loosely on SailsJs

107 lines 6.11 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const sinon = require("sinon"); const sinonChai = require("sinon-chai"); chai_1.use(sinonChai); const transaction_service_1 = require("../transaction.service"); const fake_sequelize_1 = require("../../orm/test/fake-sequelize"); const logger_core_1 = require("../logger-core"); const logger_1 = require("../logger"); const cls_namespace_service_1 = require("../cls-namespace.service"); describe('TransactionService', () => { let transactService; beforeEach(() => { let loggerCore = new logger_core_1.LoggerCore('abc', 'error', false); let clsNamespace = new cls_namespace_service_1.ClsNamespaceService({ name: 'abc' }); let fakeSql = new fake_sequelize_1.FakeSequelize(loggerCore, clsNamespace); transactService = new transaction_service_1.TransactionService(fakeSql, logger_1.Logger.fromSubsystem(loggerCore, 'transactions'), clsNamespace); }); it('should start with no transaction', () => { chai_1.expect(transactService.current).to.be.undefined; }); describe('.run', () => { it('should not have a transaction after the asynchronous task completes', () => __awaiter(this, void 0, void 0, function* () { chai_1.expect(transactService.current).to.be.undefined; yield transactService.run(`test0`, () => __awaiter(this, void 0, void 0, function* () { chai_1.expect(transactService.current).not.to.be.undefined; })); chai_1.expect(transactService.current).to.be.undefined; })); it('should nest transactions by default', () => __awaiter(this, void 0, void 0, function* () { yield transactService.run(`test0`, () => __awaiter(this, void 0, void 0, function* () { chai_1.expect(transactService.current.fullName).to.match(/test0/i); yield transactService.run(`test1`, () => __awaiter(this, void 0, void 0, function* () { chai_1.expect(transactService.current.fullName).to.match(/test0.*test1/i); })); })); })); it('should not nest transactions by if detach = true', () => __awaiter(this, void 0, void 0, function* () { yield transactService.run(`test0`, () => __awaiter(this, void 0, void 0, function* () { chai_1.expect(transactService.current.fullName).to.match(/test0/i); yield transactService.run(`test1`, true, () => __awaiter(this, void 0, void 0, function* () { chai_1.expect(transactService.current.fullName).not.to.match(/test0.*test1/i); })); })); })); it('should persist a transaction after an asynchronous operation', (done) => { transactService.run(`test0`, () => __awaiter(this, void 0, void 0, function* () { let transact = transactService.current; chai_1.expect(transact).not.to.be.undefined; setTimeout(() => { chai_1.expect(transact).to.eql(transact); done(); }, 10); })); }); it('should return a promise that resolves to the result of the function', () => __awaiter(this, void 0, void 0, function* () { let result = yield transactService.run(`test0`, () => __awaiter(this, void 0, void 0, function* () { return 42; })); chai_1.expect(result).to.eql(42); })); it('should commit the transaction when the task completes successfully', () => __awaiter(this, void 0, void 0, function* () { let transact = void (0); let stubbed = {}; yield transactService.run(`test0`, () => __awaiter(this, void 0, void 0, function* () { transact = transactService.current; chai_1.expect(transact.isComplete).to.be.false; stubbed.commit = sinon.stub(transact, 'commit').callThrough(); stubbed.rollback = sinon.stub(transact, 'rollback').callThrough(); })); chai_1.expect(transact.isComplete).to.be.true; chai_1.expect(transact.commit).to.have.been.calledOnce; chai_1.expect(transact.rollback).not.to.have.been.called; stubbed.commit.restore(); stubbed.rollback.restore(); })); it('should rollback the transaction when the task throws an exception', () => __awaiter(this, void 0, void 0, function* () { let transact = void (0); let stubbed = {}; try { yield transactService.run(`test0`, () => __awaiter(this, void 0, void 0, function* () { transact = transactService.current; chai_1.expect(transact.isComplete).to.be.false; stubbed.commit = sinon.stub(transact, 'commit').callThrough(); stubbed.rollback = sinon.stub(transact, 'rollback').callThrough(); throw new Error('Forced!'); })); } catch (e) { } chai_1.expect(transact.isComplete).to.be.true; chai_1.expect(transact.commit).not.to.have.been.called; chai_1.expect(transact.rollback).to.have.been.calledOnce; stubbed.commit.restore(); stubbed.rollback.restore(); })); }); }); //# sourceMappingURL=transaction.service.spec.js.map