Skip to content

Commit

Permalink
Test case for promises in hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
louy committed Nov 16, 2015
1 parent 0db5f34 commit bd0858e
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/Hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface TestDocument {

let hookEmitter = new Events.EventEmitter();

let shouldReject = 0;
class Test extends Iridium.Instance<TestDocument, Test> {
static collection = 'test';
static schema: Iridium.Schema = {
Expand All @@ -21,18 +22,22 @@ class Test extends Iridium.Instance<TestDocument, Test> {
answer: number;

static onCreating(document: TestDocument) {
if (shouldReject === 1) return Promise.reject('test rejection');
hookEmitter.emit('creating', document);
}

static onReady(instance: Test) {
if (shouldReject === 2) return Promise.reject('test rejection');
hookEmitter.emit('ready', instance);
}

static onRetrieved(document: TestDocument) {
if (shouldReject === 3) return Promise.reject('test rejection');
hookEmitter.emit('retrieved', document);
}

static onSaving(instance: Test, changes: any) {
if (shouldReject === 4) return Promise.reject('test rejection');
hookEmitter.emit('saving', instance, changes);
}
}
Expand All @@ -43,6 +48,7 @@ describe("Hooks", function () {
let core = new Iridium.Core({ database: 'test' });
let model = new Iridium.Model<TestDocument, Test>(core, Test);

beforeEach(() => shouldReject = 0);
beforeEach(() => core.connect().then(() => model.remove()).then(() => model.insert({ answer: 10 })));
afterEach(() => model.remove());
after(() => core.close());
Expand All @@ -64,6 +70,11 @@ describe("Hooks", function () {

return model.insert({ answer: 11 }).then(() => chai.expect(result).to.exist).then(() => result);
});

it("should accept promises",(done) => {
shouldReject = 1;
model.insert({ answer: 11 }).then(null, (err) => chai.expect(err).to.equal('test rejection') && done());
});
});

describe("ready",() => {
Expand All @@ -88,6 +99,11 @@ describe("Hooks", function () {

return model.get().then(() => chai.expect(result).to.exist).then(() => result);
});

it("should accept promises",(done) => {
shouldReject = 2;
model.get().then(null, (err) => chai.expect(err).to.equal('test rejection') && done());
});
});

describe("retreived",() => {
Expand All @@ -112,6 +128,11 @@ describe("Hooks", function () {

return model.get().then(() => chai.expect(result).to.exist).then(() => result);
});

it("should accept promises",(done) => {
shouldReject = 3;
model.get().then(null, (err) => chai.expect(err).to.equal('test rejection') && done());
});
});

describe("saving",() => {
Expand Down Expand Up @@ -159,5 +180,13 @@ describe("Hooks", function () {
return instance.save();
}).then(() => chai.expect(result).to.exist).then(() => result);
});

it("should accept promises",(done) => {
shouldReject = 4;
model.get().then((instance) => {
instance.answer++;
return instance.save();
}).then(null, (err) => chai.expect(err).to.equal('test rejection') && done());
});
});
});

0 comments on commit bd0858e

Please sign in to comment.