disposable-cls
Version:
Provides disposable continuation local storage for Node.js.
89 lines (88 loc) • 3.1 kB
JavaScript
;
var chai_1 = require("chai");
var index_1 = require("../src/index");
/**
* Represents a simple object that is accessible through a static property.
*/
var MockContext = (function () {
/**
* Initializes a new instance of the mock context object.
*/
function MockContext() {
this._id = Math.floor(Math.random());
this._isDisposed = false;
}
Object.defineProperty(MockContext.prototype, "id", {
/**
* Gets the identifier if the current mock object.
*
* @returns A number that uniquely identifies this mock object.
*/
get: function () {
return this._id;
},
enumerable: true,
configurable: true
});
Object.defineProperty(MockContext.prototype, "isDisposed", {
/**
* Gets a flag indicating whether the object has been disposed.
*
* @returns 'true' if the object has been diposed; 'false' otherwise.
*/
get: function () {
return this._isDisposed;
},
enumerable: true,
configurable: true
});
/**
* Disposes of the current object.
*/
MockContext.prototype.dispose = function () {
this._isDisposed = true;
};
Object.defineProperty(MockContext, "current", {
/**
* Retrieves the current mock object from the current scope.
*
* @returns The current mock object; or 'undefined' if a mock object is not present
* in the current scope.
*/
get: function () {
return index_1.getCurrentObject(MockContext);
},
enumerable: true,
configurable: true
});
return MockContext;
}());
describe("Ambient Context", function () {
describe("with single scope block", function () {
it("should allow retrieval of a context item", function (done) {
var mockObject = new MockContext();
index_1.using([mockObject], function () {
chai_1.expect(MockContext.current).to.not.be.undefined;
chai_1.expect(MockContext.current.id).to.be.equal(mockObject.id);
done();
});
});
it("should dispose of the context item when the scope block goes out of scope", function (done) {
var mockObject = new MockContext();
var doneFlag = false;
var expectationsOnCompleteFunc = function () {
if (doneFlag) {
chai_1.expect(mockObject.isDisposed).to.equal(true);
return done();
}
setImmediate(expectationsOnCompleteFunc);
};
chai_1.expect(mockObject.isDisposed).to.equal(false);
index_1.using([mockObject], function () {
chai_1.expect(mockObject.isDisposed).to.equal(false);
doneFlag = true;
});
setImmediate(expectationsOnCompleteFunc);
});
});
});