jspipe
Version:
JS/Pipe - coordinating asynchronous code without callbacks or chained functions
81 lines (62 loc) • 2.46 kB
JavaScript
/*globals describe, it, expect, JSPipe */
describe('job', function() {
describe('fn argument', function() {
it('it is a generator function that is executed when "job" is invoked', function() {
var expected = 'didRun',
actual;
JSPipe.job(wrapGenerator.mark(function() {
return wrapGenerator(function($ctx) {
while (1) switch ($ctx.next) {
case 0:
actual = expected;
case 1:
case "end":
return $ctx.stop();
}
}, this);
}));
expect(actual).toEqual(expected);
});
it('can yield functions that are invoked by "job"', function() {
var expected = 15,
actual;
JSPipe.job(wrapGenerator.mark(function() {
return wrapGenerator(function($ctx) {
while (1) switch ($ctx.next) {
case 0:
$ctx.next = 2;
return function() { actual = expected; }
case 2:
case "end":
return $ctx.stop();
}
}, this);
}));
expect(actual).toEqual(expected);
});
describe('yielded function', function() {
it('is passed a function that, when called, invokes the next iteration of the generator function', function() {
var expected = [1, 2],
actual = [];
JSPipe.job(wrapGenerator.mark(function() {
return wrapGenerator(function($ctx) {
while (1) switch ($ctx.next) {
case 0:
$ctx.next = 2;
return function(resume) {
actual.push(1);
resume();
}
case 2:
actual.push(2);
case 3:
case "end":
return $ctx.stop();
}
}, this);
}));
expect(actual).toEqual(expected);
});
});
});
});