Skip to content

Add Mocha+RSVP based unit tests #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions tests/assets/firefox-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
var Promise = RSVP.Promise;
var spawn = task.spawn;

Promise.resolve = RSVP.resolve;
Promise.reject = RSVP.reject;
Promise.wait = function (ms, value) {
return new Promise(function (resolve) {
setTimeout(function () {
resolve(value);
}, ms);
});
};

// wrapper on top of Mocha's async support to trap assertion errors
// that would otherwise be caught by Task.js
function it(name, test) {
if (test.length) {
specify(name, function (done) {
test.call(this, function (fn) {
if (typeof fn === 'function') {
try {
fn();
done();
} catch (err) {
done(err);
}
} else {
done(fn);
}
});
});
} else {
specify(name, test);
}
}

describe('task', function () {
describe('spawn', function () {
it('should return a new Task', function () {
expect(spawn(function () {})).to.be.a(task.Task);
});
});
describe('yield', function () {
it('should retrieve a promise\'s value', function (done) {
var expected = 'hello world';

spawn(function () {
var actual = yield Promise.resolve(expected);

done(function () {
expect(actual).to.equal(expected);
});
});
});
it('should wait for promises to resolve', function (done) {
var expected = 'hello world';

spawn(function () {
var actual = yield Promise.wait(50, expected);

done(function () {
expect(actual).to.equal(expected);
});
});
});
it('should allow for catching rejected promises', function (done) {
var expected = new Error('failed');

spawn(function () {
try {
yield Promise.reject(expected);
} catch (error) {
done(function () {
expect(error).to.equal(expected);
});
}
});
});
it('should wait for multiple yields', function (done) {
spawn(function () {
var count = yield Promise.resolve(1);
count = yield Promise.resolve(count + 2);
count = yield Promise.resolve(count + 3);
done(function () {
expect(count).to.equal(6);
});
});
});
});
describe('Task as a promise', function () {
it('should be a promise', function () {
expect(spawn(function () {}).then).to.be.a('function');
});
it('should resolve to undefined', function (done) {
spawn(function () {
yield Promise.resolve(5);
}).then(function (value) {
done(function () {
expect(value).to.be(undefined);
});
});
});
it('should be rejected when a non-generator function is passed', function (done) {
spawn(function () {}).then(null, function (error) {
done(function () {
expect(error).to.be.an(Error);
});
});
});
it('should throw when passed non-generator function that throws', function () {
var expected = new Error('failed');

try {
spawn(function () {
throw expected;
});
} catch (error) {
expect(error).to.equal(expected);
}
});
it('should be rejected when an error is thrown in the generator function', function (done) {
var expected = new Error('failed');

spawn(function () {
var foo = yield Promise.resolve(5);
throw expected;
}).then(null, function (error) {
done(function () {
expect(error).to.equal(expected);
});
});
});
it('should be rejected when a yielded rejected promise is uncaught', function (done) {
var expected = new Error('failed');

spawn(function () {
yield Promise.reject(expected);
}).then(null, function (error) {
done(function () {
expect(error).to.equal(expected);
});
});
});
});
});
24 changes: 24 additions & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Task.js tests</title>
<link rel="stylesheet" href="vendor/mocha/mocha.css">
</head>
<body>
<div id="mocha"></div>
<script src="../lib/task.js"></script>
<script src="vendor/mocha/mocha.js"></script>
<script src="vendor/expect/expect.js"></script>
<script src="vendor/rsvp/rsvp.js"></script>
<script>
mocha.setup('bdd');
</script>
<script src="assets/firefox-tests.js" type="application/javascript;version=1.8"></script>
<script>
mocha.checkLeaks();
mocha.globals(['task', 'spawn', 'RSVP', 'Promise']);
mocha.run();
</script>
</body>
</html>
Loading