miter
Version:
A typescript web framework based on ExpressJs based loosely on SailsJs
72 lines • 3.47 kB
JavaScript
;
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 sinonChai = require("sinon-chai");
chai_1.use(sinonChai);
const cls_namespace_service_1 = require("../cls-namespace.service");
const server_1 = require("../../metadata/server/server");
const testServerMeta = new server_1.ServerMetadata({ name: 'abc-xyz' });
function delay(millis) {
return new Promise((resolve, reject) => {
setTimeout(resolve, millis);
});
}
describe('ClsNamespaceService', () => {
let instance;
before(() => instance = new cls_namespace_service_1.ClsNamespaceService(testServerMeta));
describe('.name', () => {
it('should include the server name in its namespace name', () => {
chai_1.expect(instance.name).to.contain(testServerMeta.name);
});
});
describe('.runAndReturn', () => __awaiter(this, void 0, void 0, function* () {
it('should create a local storage that is accessable asynchronously', () => __awaiter(this, void 0, void 0, function* () {
chai_1.expect(instance.get('key')).not.to.be.ok;
yield instance.runAndReturn(() => __awaiter(this, void 0, void 0, function* () {
instance.set('key', 'value1');
chai_1.expect(instance.get('key')).to.eq('value1');
yield delay(20);
chai_1.expect(instance.get('key')).to.eq('value1');
}));
chai_1.expect(instance.get('key')).not.to.be.ok;
}));
it('should return the result of running the function', () => __awaiter(this, void 0, void 0, function* () {
let result = yield instance.runAndReturn(() => __awaiter(this, void 0, void 0, function* () {
yield delay(20);
return 42;
}));
chai_1.expect(result).to.eq(42);
}));
}));
describe('.run', () => {
it('should return the local storage context immediately', () => {
chai_1.expect(instance.activeContext).not.to.be.ok;
let context = instance.run(() => {
instance.set('key', 'value');
});
chai_1.expect(context).to.be.ok;
chai_1.expect(instance.activeContext).not.to.be.ok;
chai_1.expect(context.key).to.eq('value');
});
});
describe('.bind', () => {
it('should wrap the method with the asynchronous local storage context', () => {
let context = instance.run(() => {
instance.set('key', 'value');
});
let fn = () => chai_1.expect(instance.get('key')).to.eq('value');
let boundFn = instance.bind(fn, context);
chai_1.expect(instance.get('key')).not.to.be.ok;
boundFn();
});
});
});
//# sourceMappingURL=cls-namespace.service.spec.js.map