Skip to content

Commit

Permalink
test: Add tests for async hooks to ensure execution order is guaranteed
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Nov 17, 2015
1 parent 5538f28 commit 004733b
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion test/Hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ describe("Hooks", function () {
after(() => core.close());

describe("creating",() => {
after(() => {
// Not used again
Test.onCreating = null;
});

it("should be called when a document is being created",(done) => {
hookEmitter.once('creating',() => done());
model.insert({ answer: 11 });
Expand All @@ -64,9 +69,23 @@ describe("Hooks", function () {

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

it("should support blocking async calls", () => {
let result: boolean = false;
Test.onCreating = (document: TestDocument) => {
return Promise.delay(true, 50).then(() => result = true);
};

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

describe("ready",() => {
after(() => {
// Not used again
Test.onReady = null;
});

it("should be called when an instance is prepared",() => {
let result: Promise<void>;

Expand All @@ -88,9 +107,23 @@ describe("Hooks", function () {

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

it("should support blocking async calls", () => {
let result: boolean = false;
Test.onReady = (instance: Test) => {
return Promise.delay(true, 50).then(() => result = true);
};

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

describe("retreived",() => {
after(() => {
// Not used again
Test.onRetrieved = null;
});

it("should be called when a document is being retrieved",() => {
let result: Promise<void>;

Expand All @@ -112,9 +145,23 @@ describe("Hooks", function () {

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

it("should support blocking async calls", () => {
let result: boolean = false;
Test.onRetrieved = (document: TestDocument) => {
return Promise.delay(true, 50).then(() => result = true);
};

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

describe("saving",() => {
describe("saving", () => {
after(() => {
// Not used again
Test.onSaving = null;
});

it("should be triggered when save() is called on an instance",() => {
let result: Promise<void>;

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


it("should support blocking async calls", () => {
let result: boolean = false;
Test.onSaving = (instance: Test, changes: any) => {
return Promise.delay(true, 50).then(() => result = true);
};

return model.get().then((instance) => {
instance.answer++;
return instance.save();
}).then(() => chai.expect(result).to.be.true);
});
});
});

0 comments on commit 004733b

Please sign in to comment.