Merge pull request #9885 from edx/andya/teams-router-testing

Improve team testing of routers
This commit is contained in:
Andy Armstrong
2015-09-23 19:53:30 -04:00
7 changed files with 758 additions and 721 deletions

View File

@@ -1,18 +1,49 @@
define([ // jshint ignore:line
],
function() {
'use strict';
var getLocationHash;
/**
* Helper method that returns url hash.
* @return {String} Returns anchor part of current url.
*/
getLocationHash = function() {
return window.location.hash;
};
define(["backbone"],
function(Backbone) {
'use strict';
var getLocationHash, preventBackboneChangingUrl;
return {
'getLocationHash': getLocationHash
};
/**
* Helper method that returns url hash.
* @return {String} Returns anchor part of current url.
*/
getLocationHash = function() {
return window.location.hash;
};
});
/**
* Prevent Backbone tests from changing the browser's URL.
*
* This function modifies Backbone so that tests can navigate
* without modifying the browser's URL. It works be adding
* stub versions of Backbone's hash functions so that updating
* the hash doesn't change the URL but instead updates a
* local object. The router's callbacks are still invoked
* so that to the test it appears that navigation is behaving
* as expected.
*
* Note: it is important that tests don't update the browser's
* URL because subsequent tests could find themselves in an
* unexpected navigation state.
*/
preventBackboneChangingUrl = function() {
var history = {
currentFragment: ''
};
// Stub out the Backbone router so that the browser doesn't actually navigate
spyOn(Backbone.history, '_updateHash').andCallFake(function (location, fragment, replace) {
history.currentFragment = fragment;
});
// Stub out getHash so that Backbone thinks that the browser has navigated
spyOn(Backbone.history, 'getHash').andCallFake(function () {
return history.currentFragment;
});
};
return {
'getLocationHash': getLocationHash,
'preventBackboneChangingUrl': preventBackboneChangingUrl
};
});