Skip to content

Commit

Permalink
Create tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartozzz committed Mar 31, 2018
1 parent 2e29ae5 commit 23711db
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
1 change: 1 addition & 0 deletions .eslintignore
@@ -1 +1,2 @@
dist
test
11 changes: 7 additions & 4 deletions package.json
Expand Up @@ -10,7 +10,8 @@
"promise-queue"
],
"description": "A simple and small library for promise-based queues",
"author": "Łaniewski Bartosz <laniewski.bartozzz@gmail.com> (http://laniewski.me/)",
"author":
"Łaniewski Bartosz <laniewski.bartozzz@gmail.com> (http://laniewski.me/)",
"license": "MIT",
"main": "dist/index.js",
"repository": {
Expand Down Expand Up @@ -44,11 +45,13 @@
"prettier": "^1.11.1"
},
"scripts": {
"flow": "flow",
"lint": "node_modules/.bin/eslint --fix src",
"test": "npm run test:eslint && npm run test:flow && npm run test:mocha",
"test:flow": "flow",
"test:mocha": "mocha --require babel-core/register",
"test:eslint": "eslint --fix src",
"clean": "rm -rf dist",
"build": "babel src -d dist",
"watch": "babel src -d dist -w",
"prepare": "npm run clean && npm run lint && npm run flow && npm run build"
"prepare": "npm run clean && npm run test && npm run build"
}
}
62 changes: 62 additions & 0 deletions test/index.js
@@ -0,0 +1,62 @@
import chai from "chai";
import Queue from "../src/";

describe("queue-promise", function () {
const expect = chai.expect;

let count = 0;
let queue = new Queue({
concurrency: 1,
interval: 500
});

const reject = () => new Promise((resolve, reject) => reject("Error"));
const resolve = () => new Promise((resolve, reject) => resolve("Success"));

describe("add(asyncTask)", function () {
it("should add a new task if valid", function () {
queue.add(reject);
queue.add(resolve);

expect(queue.collection.size).to.equal(2);
});

it("should reject a new task if not valid", function () {
try {
expect(queue.add(true)).to.throw();
} catch (err) {
expect(err).to.be.an.instanceof(Error);
expect(err.message).to.equal("You must provide a function, not boolean.");
}
});
});

describe("remove(key)", function () {
it("should remove a registered task", function () {
queue.remove(0);
queue.remove(1);

expect(queue.collection.size).to.equal(0);
});
});

describe("on(event, callback)", function () {
it("should handle events", function () {
queue.on("resolve", function(data){count += 1});
queue.on("reject", function(error){count += 1});
});
});

describe("start()", function () {
it("should handle events", function (done) {
queue.add(reject);
queue.add(resolve);
queue.start();

setTimeout(function () {
expect(count).to.equal(2);
done();
}, 1250);
});
});
});

0 comments on commit 23711db

Please sign in to comment.