taskbuffer/test/test.js
2016-05-15 15:52:59 +02:00

83 lines
10 KiB
JavaScript

"use strict";
/// <reference path="../ts/typings/main.d.ts" />
var taskbuffer = require("../dist/index");
var should = require("should");
var plugins = {
q: require("q")
};
// setup some testData to work with
var testTask;
var testTaskFunction = function () {
var done = plugins.q.defer();
console.log("main function executed!");
done.resolve();
return done.promise;
};
var testPreTask = new taskbuffer.Task({
taskFunction: function () {
console.log("preTask executed");
},
preTask: testTask
});
describe("taskbuffer", function () {
describe(".Task", function () {
it("new Task() should return a new task", function () {
testTask = new taskbuffer.Task({ taskFunction: testTaskFunction, preTask: testPreTask });
});
it("testTask should be and instance of Task", function () {
testTask.should.be.instanceof(taskbuffer.Task);
});
it("testTask.idle is true", function () {
if (!testTask.idle) {
throw new Error("testTask.idle is not true");
}
});
it("testTask.running is type boolean and initially false", function () {
testTask.running.should.be.type("boolean");
testTask.running.should.be.false();
});
it("testTask.trigger() should return Promise", function () {
testTask.trigger().should.be.Promise();
});
it("testTask.trigger() returned Promise should be fullfilled", function (done) {
testTask.trigger()
.then(done);
});
it("should run a task without pre and afterTask", function (done) {
var localTestTask = new taskbuffer.Task({ taskFunction: testTaskFunction });
localTestTask.trigger().then(done);
});
});
describe("Taskchain", function () {
var testTaskchain;
var testTaskArray = [
new taskbuffer.Task({
name: "task1",
taskFunction: function () {
var done = plugins.q.defer();
setTimeout(done.resolve, 2000);
return done.promise;
}
}),
new taskbuffer.Task({
name: "task2",
taskFunction: function () {
var done = plugins.q.defer();
setTimeout(done.resolve, 2000);
return done.promise;
}
}),
];
it("should run tasks in sequence", function (done) {
this.timeout(5000);
testTaskchain = new taskbuffer.Taskchain({
name: "Taskchain1",
taskArray: testTaskArray
});
testTaskchain.trigger().then(done);
});
});
});
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLGdEQUFnRDtBQUNo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VwiLGZ1bmN0aW9uKCl7XHJcbiAgICAgICAgICAgIHRlc3RUYXNrLnRyaWdnZXIoKS5zaG91bGQuYmUuUHJvbWlzZSgpO1xyXG4gICAgICAgIH0pO1xyXG4gICAgICAgIGl0KFwidGVzdFRhc2sudHJpZ2dlcigpIHJldHVybmVkIFByb21pc2Ugc2hvdWxkIGJlIGZ1bGxmaWxsZWRcIixmdW5jdGlvbihkb25lKXtcclxuICAgICAgICAgICAgdGVzdFRhc2sudHJpZ2dlcigpXHJcbiAgICAgICAgICAgICAgICAudGhlbihkb25lKTtcclxuICAgICAgICB9KTtcclxuICAgICAgICBpdChcInNob3VsZCBydW4gYSB0YXNrIHdpdGhvdXQgcHJlIGFuZCBhZnRlclRhc2tcIixmdW5jdGlvbihkb25lKXtcclxuICAgICAgICAgICAgbGV0IGxvY2FsVGVzdFRhc2sgPSBuZXcgdGFza2J1ZmZlci5UYXNrKHt0YXNrRnVuY3Rpb246dGVzdFRhc2tGdW5jdGlvbn0pO1xyXG4gICAgICAgICAgICBsb2NhbFRlc3RUYXNrLnRyaWdnZXIoKS50aGVuKGRvbmUpO1xyXG4gICAgICAgIH0pO1xyXG4gICAgfSk7XHJcbiAgICBkZXNjcmliZShcIlRhc2tjaGFpblwiLGZ1bmN0aW9uKCl7XHJcbiAgICAgICAgbGV0IHRlc3RUYXNrY2hhaW47XHJcbiAgICAgICAgbGV0IHRlc3RUYXNrQXJyYXkgPSBbXHJcbiAgICAgICAgICAgIG5ldyB0YXNrYnVmZmVyLlRhc2soe1xyXG4gICAgICAgICAgICAgICAgbmFtZTpcInRhc2sxXCIsXHJcbiAgICAgICAgICAgICAgICB0YXNrRnVuY3Rpb246ZnVuY3Rpb24oKXtcclxuICAgICAgICAgICAgICAgICAgICBsZXQgZG9uZSA9IHBsdWdpbnMucS5kZWZlcigpO1xyXG4gICAgICAgICAgICAgICAgICAgIHNldFRpbWVvdXQoZG9uZS5yZXNvbHZlLDIwMDApO1xyXG4gICAgICAgICAgICAgICAgICAgIHJldHVybiBkb25lLnByb21pc2U7XHJcbiAgICAgICAgICAgICAgICB9XHJcbiAgICAgICAgICAgIH0pLFxyXG4gICAgICAgICAgICBuZXcgdGFza2J1ZmZlci5UYXNrKHtcclxuICAgICAgICAgICAgICAgIG5hbWU6XCJ0YXNrMlwiLFxyXG4gICAgICAgICAgICAgICAgdGFza0Z1bmN0aW9uOiBmdW5jdGlvbigpe1xyXG4gICAgICAgICAgICAgICAgICAgIGxldCBkb25lID0gcGx1Z2lucy5xLmRlZmVyKCk7XHJcbiAgICAgICAgICAgICAgICAgICAgc2V0VGltZW91dChkb25lLnJlc29sdmUsMjAwMCk7XHJcbiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIGRvbmUucHJvbWlzZTtcclxuICAgICAgICAgICAgICAgIH1cclxuICAgICAgICAgICAgfSksXHJcbiAgICAgICAgXTtcclxuICAgICAgICBpdChcInNob3VsZCBydW4gdGFza3MgaW4gc2VxdWVuY2VcIixmdW5jdGlvbihkb25lKXtcclxuICAgICAgICAgICAgdGhpcy50aW1lb3V0KDUwMDApO1xyXG4gICAgICAgICAgICB0ZXN0VGFza2NoYWluID0gbmV3IHRhc2tidWZmZXIuVGFza2NoYWluKHtcclxuICAgICAgICAgICAgICAgIG5hbWU6XCJUYXNrY2hhaW4xXCIsXHJcbiAgICAgICAgICAgICAgICB0YXNrQXJyYXk6dGVzdFRhc2tBcnJheVxyXG4gICAgICAgICAgICB9KTtcclxuICAgICAgICAgICAgdGVzdFRhc2tjaGFpbi50cmlnZ2VyKCkudGhlbihkb25lKTtcclxuICAgICAgICAgICAgXHJcbiAgICAgICAgfSk7XHJcbiAgICB9KTtcclxufSk7Il19