Created
May 17, 2014 17:14
-
-
Save mikefrey/5b0debb0d9a4e1411f72 to your computer and use it in GitHub Desktop.
Task Runner Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('TaskRunner', function () { | |
var scope | |
var TR | |
var $q | |
var success | |
var error | |
function MockTasks() { | |
this.tasks = [] | |
this.dfds = [] | |
} | |
MockTasks.prototype = { | |
getTask: function() { | |
var mock = this | |
var dfd = $q.defer() | |
mock.dfds.push(dfd) | |
var task = jasmine.createSpy().and.callFake(function() { | |
console.log('task called') | |
dfd.promise.then(function() { | |
console.log('mock promise then') | |
}) | |
return dfd.promise | |
}) | |
this.tasks.push(task) | |
return task | |
} | |
} | |
beforeEach(module('sportAdmin')) | |
beforeEach(inject(function($injector) { | |
scope = $injector.get('$rootScope') | |
TR = $injector.get('TaskRunner') | |
$q = $injector.get('$q') | |
})) | |
describe('.auto()', function () { | |
beforeEach(function() { | |
console.log("\n----------------\n") | |
success = jasmine.createSpy("success") | |
error = jasmine.createSpy("error") | |
}) | |
it('should run success if no tasks are provided', function() { | |
console.log('NO TASKS') | |
TR.auto().then( | |
expect.bind(true), | |
expect.bind(false, "Error handler called.") | |
) | |
}) | |
it('should run success if a task does not return a promise', function() { | |
console.log('NO PROMISE') | |
TR.auto({ | |
task: function(){ return 'FOO' } | |
}) | |
.then( | |
expect.bind(true), | |
expect.bind(false, "Error handler called.") | |
) | |
}) | |
it('should run non-dependent tasks in parallel', function() { | |
console.log('TWO TASKS') | |
var mock = new MockTasks() | |
var tasks = { | |
task1: mock.getTask(), | |
task2: mock.getTask() | |
} | |
var doTasks = TR.auto(tasks) | |
var _expect = expect | |
expect(mock.tasks[0]).toHaveBeenCalled() | |
expect(mock.tasks[1]).toHaveBeenCalled() | |
}) | |
it('should run dependent tasks in series', function() { | |
console.log('SERIES') | |
var mock = new MockTasks() | |
var tasks = { | |
task1: mock.getTask(), | |
task2: ['task1', mock.getTask()] | |
} | |
var doTasks = TR.auto(tasks) | |
doTasks.then(success) | |
expect(mock.tasks[0]).toHaveBeenCalled() | |
expect(mock.tasks[1]).not.toHaveBeenCalled() | |
mock.dfds[0].resolve() | |
scope.$root.$digest() | |
expect(mock.tasks[1]).toHaveBeenCalled() | |
mock.dfds[1].resolve() | |
expect(success).toHaveBeenCalled() | |
// expect(mock.tasksCompleted).toEqual(1) | |
// expect(mock.tasksPending).toEqual(1) | |
// expect(success).not.toHaveBeenCalled() | |
// mock.tasks[1].resolve() | |
// expect(mock.tasksCompleted).toEqual(2) | |
// expect(success).toHaveBeenCalled() | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment