Skip to content

Commit

Permalink
Merge pull request #195 from trentmwillis/order-test
Browse files Browse the repository at this point in the history
Add test to verify execution order of hooks in async handlers
  • Loading branch information
rwjblue committed Oct 18, 2016
2 parents f3ff656 + 0b0df7a commit 7759a11
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions test/tests/async_get_handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ QUnit.module('Async Get Handler', {
});
});

var testEnvironment = this;
this.router.getHandler = function(name) {
return new Promise(function(resolve) {
setTimeout(function() {
var handlers = testEnvironment.handlers;
resolve(handlers[name] || (handlers[name] = {}));
}, 1);
});
};
this.router.updateURL = function() {};
},

Expand All @@ -36,6 +27,16 @@ QUnit.module('Async Get Handler', {
});

QUnit.asyncTest('can transition to lazily-resolved routes', function(assert) {
var testEnvironment = this;
this.router.getHandler = function(name) {
return new Promise(function(resolve) {
setTimeout(function() {
var handlers = testEnvironment.handlers;
resolve(handlers[name] || (handlers[name] = {}));
}, 1);
});
};

var fooCalled = false;
var fooBarCalled = false;

Expand All @@ -55,3 +56,40 @@ QUnit.asyncTest('can transition to lazily-resolved routes', function(assert) {
assert.ok(!fooCalled, 'foo is not called synchronously');
assert.ok(!fooBarCalled, 'fooBar is not called synchronously');
});

QUnit.asyncTest('calls hooks of lazily-resolved routes in order', function(assert) {
var operations = [];

var testEnvironment = this;
this.router.getHandler = function(name) {
operations.push('get handler ' + name);
return new Promise(function(resolve) {
var timeoutLength = name === 'foo' ? 100 : 1;
setTimeout(function() {
var handlers = testEnvironment.handlers;
operations.push('resolved ' + name);
resolve(handlers[name] || (handlers[name] = {}));
}, timeoutLength);
});
};


this.handlers.foo = {
model: function() { operations.push('model foo'); }
};
this.handlers.fooBar = {
model: function() { operations.push('model fooBar'); }
};

this.router.transitionTo('/foo/bar').then(function() {
assert.deepEqual(operations, [
'get handler foo',
'get handler fooBar',
'resolved fooBar',
'resolved foo',
'model foo',
'model fooBar'
], 'order of operations is correct');
QUnit.start();
});
});

0 comments on commit 7759a11

Please sign in to comment.