Commit d09034e0 by Andy Armstrong

Improve team testing of routers

parent d8d49f75
define([ // jshint ignore:line define(["backbone"],
], function(Backbone) {
function() { 'use strict';
'use strict'; var getLocationHash, preventBackboneChangingUrl;
var getLocationHash;
/**
* Helper method that returns url hash.
* @return {String} Returns anchor part of current url.
*/
getLocationHash = function() {
return window.location.hash;
};
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
};
});
define(['jquery', 'backbone', 'teams/js/teams_tab_factory', define(['jquery', 'backbone', 'teams/js/teams_tab_factory',
'teams/js/spec_helpers/team_spec_helpers'], 'common/js/spec_helpers/page_helpers', 'teams/js/spec_helpers/team_spec_helpers'],
function($, Backbone, TeamsTabFactory, TeamSpecHelpers) { function($, Backbone, TeamsTabFactory, PageHelpers, TeamSpecHelpers) {
'use strict'; 'use strict';
describe("Teams Tab Factory", function() { describe("Teams Tab Factory", function() {
...@@ -10,6 +10,7 @@ define(['jquery', 'backbone', 'teams/js/teams_tab_factory', ...@@ -10,6 +10,7 @@ define(['jquery', 'backbone', 'teams/js/teams_tab_factory',
beforeEach(function() { beforeEach(function() {
setFixtures('<section class="teams-content"></section>'); setFixtures('<section class="teams-content"></section>');
PageHelpers.preventBackboneChangingUrl();
}); });
afterEach(function() { afterEach(function() {
...@@ -17,11 +18,6 @@ define(['jquery', 'backbone', 'teams/js/teams_tab_factory', ...@@ -17,11 +18,6 @@ define(['jquery', 'backbone', 'teams/js/teams_tab_factory',
}); });
it('can render the "Teams" tab', function() { it('can render the "Teams" tab', function() {
// Hack to make sure the URL fragments from earlier
// tests don't interfere with Backbone routing by the
// teams tab view
document.location.hash = '';
initializeTeamsTabFactory(); initializeTeamsTabFactory();
expect($('.teams-content').text()).toContain('See all teams in your course, organized by topic'); expect($('.teams-content').text()).toContain('See all teams in your course, organized by topic');
}); });
......
...@@ -3,10 +3,11 @@ define([ ...@@ -3,10 +3,11 @@ define([
'underscore', 'underscore',
'backbone', 'backbone',
'common/js/spec_helpers/ajax_helpers', 'common/js/spec_helpers/ajax_helpers',
'common/js/spec_helpers/page_helpers',
'teams/js/views/edit_team', 'teams/js/views/edit_team',
'teams/js/models/team', 'teams/js/models/team',
'teams/js/spec_helpers/team_spec_helpers' 'teams/js/spec_helpers/team_spec_helpers'
], function ($, _, Backbone, AjaxHelpers, TeamEditView, TeamModel, TeamSpecHelpers) { ], function ($, _, Backbone, AjaxHelpers, PageHelpers, TeamEditView, TeamModel, TeamSpecHelpers) {
'use strict'; 'use strict';
describe('CreateEditTeam', function() { describe('CreateEditTeam', function() {
...@@ -90,6 +91,7 @@ define([ ...@@ -90,6 +91,7 @@ define([
beforeEach(function () { beforeEach(function () {
setFixtures('<div class="teams-content"></div>'); setFixtures('<div class="teams-content"></div>');
PageHelpers.preventBackboneChangingUrl();
spyOn(Backbone.history, 'navigate'); spyOn(Backbone.history, 'navigate');
}); });
......
...@@ -6,8 +6,9 @@ define([ ...@@ -6,8 +6,9 @@ define([
'teams/js/views/instructor_tools', 'teams/js/views/instructor_tools',
'teams/js/views/team_utils', 'teams/js/views/team_utils',
'teams/js/spec_helpers/team_spec_helpers', 'teams/js/spec_helpers/team_spec_helpers',
'common/js/spec_helpers/ajax_helpers' 'common/js/spec_helpers/ajax_helpers',
], function ($, Backbone, _, Team, InstructorToolsView, TeamUtils, TeamSpecHelpers, AjaxHelpers) { 'common/js/spec_helpers/page_helpers'
], function ($, Backbone, _, Team, InstructorToolsView, TeamUtils, TeamSpecHelpers, AjaxHelpers, PageHelpers) {
'use strict'; 'use strict';
describe('Instructor Tools', function () { describe('Instructor Tools', function () {
...@@ -37,6 +38,7 @@ define([ ...@@ -37,6 +38,7 @@ define([
beforeEach(function () { beforeEach(function () {
setFixtures('<div id="page-prompt"></div>'); setFixtures('<div id="page-prompt"></div>');
PageHelpers.preventBackboneChangingUrl();
spyOn(Backbone.history, 'navigate'); spyOn(Backbone.history, 'navigate');
spyOn(TeamUtils, 'showMessage'); spyOn(TeamUtils, 'showMessage');
view = createInstructorTools().render(); view = createInstructorTools().render();
......
...@@ -4,8 +4,10 @@ define([ ...@@ -4,8 +4,10 @@ define([
'teams/js/collections/team_membership', 'teams/js/collections/team_membership',
'teams/js/views/topic_teams', 'teams/js/views/topic_teams',
'teams/js/spec_helpers/team_spec_helpers', 'teams/js/spec_helpers/team_spec_helpers',
'common/js/spec_helpers/ajax_helpers' 'common/js/spec_helpers/ajax_helpers',
], function (Backbone, TeamCollection, TeamMembershipCollection, TopicTeamsView, TeamSpecHelpers, AjaxHelpers) { 'common/js/spec_helpers/page_helpers'
], function (Backbone, TeamCollection, TeamMembershipCollection, TopicTeamsView, TeamSpecHelpers,
AjaxHelpers, PageHelpers) {
'use strict'; 'use strict';
describe('Topic Teams View', function () { describe('Topic Teams View', function () {
var createTopicTeamsView = function(options) { var createTopicTeamsView = function(options) {
...@@ -39,6 +41,7 @@ define([ ...@@ -39,6 +41,7 @@ define([
beforeEach(function () { beforeEach(function () {
setFixtures('<div class="teams-container"></div>'); setFixtures('<div class="teams-container"></div>');
PageHelpers.preventBackboneChangingUrl();
}); });
it('can render itself', function () { it('can render itself', function () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment