diff --git a/test/Hooks.js b/test/Hooks.js new file mode 100644 index 0000000..c95244e --- /dev/null +++ b/test/Hooks.js @@ -0,0 +1,126 @@ +var __extends = this.__extends || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + __.prototype = b.prototype; + d.prototype = new __(); +}; +/// +var Iridium = require('../index'); +var Events = require('events'); +var Promise = require('bluebird'); +var Test = (function (_super) { + __extends(Test, _super); + function Test() { + _super.apply(this, arguments); + } + return Test; +})(Iridium.Instance); +var hookEmitter = new Events.EventEmitter(); +describe("Hooks", function () { + this.timeout(500); + var core = new Iridium.Core({ database: 'test' }); + var model = new Iridium.Model(core, Test, 'test', { + id: false, + answer: Number + }, { + hooks: { + creating: function (document) { return hookEmitter.emit('creating', document); }, + ready: function (instance) { return hookEmitter.emit('ready', instance); }, + retrieved: function (document) { return hookEmitter.emit('retrieved', document); }, + saving: function (instance, changes) { return hookEmitter.emit('saving', instance, changes); } + } + }); + beforeEach(function () { return core.connect().then(function () { return model.remove(); }).then(function () { return model.insert({ answer: 10 }); }); }); + afterEach(function () { return model.remove(); }); + after(function () { return core.close(); }); + describe("creating", function () { + it("should be called when a document is being created", function (done) { + hookEmitter.once('creating', function () { return done(); }); + model.insert({ answer: 11 }); + }); + it("should be passed the document being created", function () { + var result; + hookEmitter.once('creating', function (document) { + result = Promise.resolve().then(function () { + chai.expect(document).to.eql({ answer: 11 }); + }); + }); + return model.insert({ answer: 11 }).then(function () { return chai.expect(result).to.exist; }).then(function () { return result; }); + }); + }); + describe("ready", function () { + it("should be called when an instance is prepared", function () { + var result; + hookEmitter.once('ready', function () { + result = Promise.resolve(); + }); + return model.get().then(function () { return chai.expect(result).to.exist; }).then(function () { return result; }); + }); + it("should be passed the instance which was created", function () { + var result; + hookEmitter.once('ready', function (instance) { + result = Promise.resolve().then(function () { + chai.expect(instance).to.be.an.instanceof(model.Instance); + }); + }); + return model.get().then(function () { return chai.expect(result).to.exist; }).then(function () { return result; }); + }); + }); + describe("retreived", function () { + it("should be called when a document is being retrieved", function () { + var result; + hookEmitter.once('retrieved', function () { + result = Promise.resolve(); + }); + return model.get().then(function () { return chai.expect(result).to.exist; }).then(function () { return result; }); + }); + it("should be passed the document being retrieved", function () { + var result; + hookEmitter.once('retrieved', function (document) { + result = Promise.resolve().then(function () { + chai.expect(document).to.have.property('answer', 10); + }); + }); + return model.get().then(function () { return chai.expect(result).to.exist; }).then(function () { return result; }); + }); + }); + describe("saving", function () { + it("should be triggered when save() is called on an instance", function () { + var result; + hookEmitter.once('saving', function () { + result = Promise.resolve(); + }); + return model.get().then(function (instance) { + instance.answer++; + return instance.save(); + }).then(function () { return chai.expect(result).to.exist; }).then(function () { return result; }); + }); + it("should be passed the instance being saved", function () { + var result; + hookEmitter.once('saving', function (instance) { + result = Promise.resolve().then(function () { + chai.expect(instance).to.be.an.instanceof(model.Instance); + }); + }); + return model.get().then(function (instance) { + instance.answer++; + return instance.save(); + }).then(function () { return chai.expect(result).to.exist; }).then(function () { return result; }); + }); + it("should be passed the changes being made to the instance", function () { + var result; + hookEmitter.once('saving', function (instance, changes) { + result = Promise.resolve().then(function () { + chai.expect(changes).to.eql({ + $set: { answer: instance.answer } + }); + }); + }); + return model.get().then(function (instance) { + instance.answer++; + return instance.save(); + }).then(function () { return chai.expect(result).to.exist; }).then(function () { return result; }); + }); + }); +}); +//# sourceMappingURL=Hooks.js.map \ No newline at end of file diff --git a/test/Hooks.js.map b/test/Hooks.js.map new file mode 100644 index 0000000..bf77256 --- /dev/null +++ b/test/Hooks.js.map @@ -0,0 +1 @@ +{"version":3,"file":"Hooks.js","sourceRoot":"","sources":["Hooks.ts"],"names":["Test","Test.constructor"],"mappings":";;;;;;AACA,AADA,4CAA4C;AAC5C,IAAO,OAAO,WAAW,UAAU,CAAC,CAAC;AACrC,IAAO,MAAM,WAAW,QAAQ,CAAC,CAAC;AAClC,IAAO,OAAO,WAAW,UAAU,CAAC,CAAC;AAOrC,IAAM,IAAI;IAAS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AAC,GAAG,EAAE,CAAC,IAAI,CAAC,UAAC,QAAQ;gBAC7B,QAAQ,CAAC,MAAM,EAAE,CAAC;gBAClB,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;YAC3B,CAAC,CAAC,CAAC,IAAI,CAAC,cAAM,OAAA,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,KAAK,EAA5B,CAA4B,CAAC,CAAC,IAAI,CAAC,cAAM,aAAM,EAAN,CAAM,CAAC,CAAC;QACnE,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/test/Hooks.ts b/test/Hooks.ts new file mode 100644 index 0000000..49d7063 --- /dev/null +++ b/test/Hooks.ts @@ -0,0 +1,151 @@ +/// +import Iridium = require('../index'); +import Events = require('events'); +import Promise = require('bluebird'); + +interface TestDocument { + id?: string; + answer: number; +} + +class Test extends Iridium.Instance { + id: string; + answer: number; +} + +var hookEmitter = new Events.EventEmitter(); + +describe("Hooks", function () { + this.timeout(500); + + var core = new Iridium.Core({ database: 'test' }); + var model = new Iridium.Model(core, Test, 'test', { + id: false, + answer: Number + }, { + hooks: { + creating: (document) => hookEmitter.emit('creating', document), + ready: (instance) => hookEmitter.emit('ready', instance), + retrieved: (document) => hookEmitter.emit('retrieved', document), + saving: (instance, changes) => hookEmitter.emit('saving', instance, changes) + } + }); + + beforeEach(() => core.connect().then(() => model.remove()).then(() => model.insert({ answer: 10 }))); + afterEach(() => model.remove()); + after(() => core.close()); + + describe("creating",() => { + it("should be called when a document is being created",(done) => { + hookEmitter.once('creating',() => done()); + model.insert({ answer: 11 }); + }); + + it("should be passed the document being created",() => { + var result: Promise; + + hookEmitter.once('creating',(document) => { + result = Promise.resolve().then(() => { + chai.expect(document).to.eql({ answer: 11 }); + }); + }); + + return model.insert({ answer: 11 }).then(() => chai.expect(result).to.exist).then(() => result); + }); + }); + + describe("ready",() => { + it("should be called when an instance is prepared",() => { + var result: Promise; + + hookEmitter.once('ready',() => { + result = Promise.resolve(); + }); + + return model.get().then(() => chai.expect(result).to.exist).then(() => result); + }); + + it("should be passed the instance which was created",() => { + var result: Promise; + + hookEmitter.once('ready',(instance) => { + result = Promise.resolve().then(() => { + chai.expect(instance).to.be.an.instanceof(model.Instance); + }); + }); + + return model.get().then(() => chai.expect(result).to.exist).then(() => result); + }); + }); + + describe("retreived",() => { + it("should be called when a document is being retrieved",() => { + var result: Promise; + + hookEmitter.once('retrieved',() => { + result = Promise.resolve(); + }); + + return model.get().then(() => chai.expect(result).to.exist).then(() => result); + }); + + it("should be passed the document being retrieved",() => { + var result: Promise; + + hookEmitter.once('retrieved',(document) => { + result = Promise.resolve().then(() => { + chai.expect(document).to.have.property('answer', 10); + }); + }); + + return model.get().then(() => chai.expect(result).to.exist).then(() => result); + }); + }); + + describe("saving",() => { + it("should be triggered when save() is called on an instance",() => { + var result: Promise; + + hookEmitter.once('saving',() => { + result = Promise.resolve(); + }); + + return model.get().then((instance) => { + instance.answer++; + return instance.save(); + }).then(() => chai.expect(result).to.exist).then(() => result); + }); + + it("should be passed the instance being saved",() => { + var result: Promise; + + hookEmitter.once('saving',(instance) => { + result = Promise.resolve().then(() => { + chai.expect(instance).to.be.an.instanceof(model.Instance); + }); + }); + + return model.get().then((instance) => { + instance.answer++; + return instance.save(); + }).then(() => chai.expect(result).to.exist).then(() => result); + }); + + it("should be passed the changes being made to the instance",() => { + var result: Promise; + + hookEmitter.once('saving',(instance, changes) => { + result = Promise.resolve().then(() => { + chai.expect(changes).to.eql({ + $set: { answer: instance.answer } + }); + }); + }); + + return model.get().then((instance) => { + instance.answer++; + return instance.save(); + }).then(() => chai.expect(result).to.exist).then(() => result); + }); + }); +}); \ No newline at end of file