Commit 7280a587 by Andy Armstrong

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

Improve team testing of routers
parents 5e2290f9 d09034e0
define([ // jshint ignore:line define(["backbone"],
], function(Backbone) {
function() {
'use strict'; 'use strict';
var getLocationHash; var getLocationHash, preventBackboneChangingUrl;
/** /**
* Helper method that returns url hash. * Helper method that returns url hash.
* @return {String} Returns anchor part of current url. * @return {String} Returns anchor part of current url.
...@@ -11,8 +11,39 @@ function() { ...@@ -11,8 +11,39 @@ function() {
return window.location.hash; return window.location.hash;
}; };
return { /**
'getLocationHash': getLocationHash * 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();
......
...@@ -3,13 +3,16 @@ define([ ...@@ -3,13 +3,16 @@ define([
'backbone', 'backbone',
'logger', 'logger',
'common/js/spec_helpers/ajax_helpers', 'common/js/spec_helpers/ajax_helpers',
'common/js/spec_helpers/page_helpers',
'common/js/spec_helpers/spec_helpers', 'common/js/spec_helpers/spec_helpers',
'teams/js/views/teams_tab', 'teams/js/views/teams_tab',
'teams/js/spec_helpers/team_spec_helpers' 'teams/js/spec_helpers/team_spec_helpers'
], function ($, Backbone, Logger, AjaxHelpers, SpecHelpers, TeamsTabView, TeamSpecHelpers) { ], function ($, Backbone, Logger, AjaxHelpers, PageHelpers, SpecHelpers, TeamsTabView, TeamSpecHelpers) {
'use strict'; 'use strict';
describe('TeamsTab', function() { describe('TeamsTab', function() {
var requests;
var expectError = function (teamsTabView, text) { var expectError = function (teamsTabView, text) {
expect(teamsTabView.$('.warning').text()).toContain(text); expect(teamsTabView.$('.warning').text()).toContain(text);
}; };
...@@ -18,13 +21,31 @@ define([ ...@@ -18,13 +21,31 @@ define([
expect(element.focus).toHaveBeenCalled(); expect(element.focus).toHaveBeenCalled();
}; };
var createTeamsTabView = function(options) { var verifyTeamsRequest = function(options) {
AjaxHelpers.expectRequestURL(requests, TeamSpecHelpers.testContext.teamsUrl,
_.extend(
{
topic_id: TeamSpecHelpers.testTopicID,
expand: 'user',
course_id: TeamSpecHelpers.testCourseID,
order_by: '',
page: '1',
page_size: '10',
text_search: ''
},
options
));
};
var createTeamsTabView = function(test, options) {
var teamsTabView = new TeamsTabView( var teamsTabView = new TeamsTabView(
{ {
el: $('.teams-content'), el: $('.teams-content'),
context: TeamSpecHelpers.createMockContext(options) context: TeamSpecHelpers.createMockContext(options)
} }
); );
requests = AjaxHelpers.requests(test);
PageHelpers.preventBackboneChangingUrl();
teamsTabView.start(); teamsTabView.start();
return teamsTabView; return teamsTabView;
}; };
...@@ -39,7 +60,7 @@ define([ ...@@ -39,7 +60,7 @@ define([
describe('Navigation', function () { describe('Navigation', function () {
it('does not render breadcrumbs for the top level tabs', function() { it('does not render breadcrumbs for the top level tabs', function() {
var teamsTabView = createTeamsTabView(); var teamsTabView = createTeamsTabView(this);
teamsTabView.router.navigate('#my-teams', {trigger: true}); teamsTabView.router.navigate('#my-teams', {trigger: true});
expect(teamsTabView.$('.breadcrumbs').length).toBe(0); expect(teamsTabView.$('.breadcrumbs').length).toBe(0);
teamsTabView.router.navigate('#browse', {trigger: true}); teamsTabView.router.navigate('#browse', {trigger: true});
...@@ -47,21 +68,20 @@ define([ ...@@ -47,21 +68,20 @@ define([
}); });
it('does not interfere with anchor links to #content', function () { it('does not interfere with anchor links to #content', function () {
var teamsTabView = createTeamsTabView(); var teamsTabView = createTeamsTabView(this);
teamsTabView.router.navigate('#content', {trigger: true}); teamsTabView.router.navigate('#content', {trigger: true});
expect(teamsTabView.$('.wrapper-msg')).toHaveClass('is-hidden'); expect(teamsTabView.$('.wrapper-msg')).toHaveClass('is-hidden');
}); });
it('displays and focuses an error message when trying to navigate to a nonexistent page', function () { it('displays and focuses an error message when trying to navigate to a nonexistent page', function () {
var teamsTabView = createTeamsTabView(); var teamsTabView = createTeamsTabView(this);
teamsTabView.router.navigate('no_such_page', {trigger: true}); teamsTabView.router.navigate('no_such_page', {trigger: true});
expectError(teamsTabView, 'The page "no_such_page" could not be found.'); expectError(teamsTabView, 'The page "no_such_page" could not be found.');
expectFocus(teamsTabView.$('.warning')); expectFocus(teamsTabView.$('.warning'));
}); });
it('displays and focuses an error message when trying to navigate to a nonexistent topic', function () { it('displays and focuses an error message when trying to navigate to a nonexistent topic', function () {
var requests = AjaxHelpers.requests(this), var teamsTabView = createTeamsTabView(this);
teamsTabView = createTeamsTabView();
teamsTabView.router.navigate('topics/no_such_topic', {trigger: true}); teamsTabView.router.navigate('topics/no_such_topic', {trigger: true});
AjaxHelpers.expectRequest(requests, 'GET', '/api/team/v0/topics/no_such_topic,course/1', null); AjaxHelpers.expectRequest(requests, 'GET', '/api/team/v0/topics/no_such_topic,course/1', null);
AjaxHelpers.respondWithError(requests, 404); AjaxHelpers.respondWithError(requests, 404);
...@@ -70,8 +90,7 @@ define([ ...@@ -70,8 +90,7 @@ define([
}); });
it('displays and focuses an error message when trying to navigate to a nonexistent team', function () { it('displays and focuses an error message when trying to navigate to a nonexistent team', function () {
var requests = AjaxHelpers.requests(this), var teamsTabView = createTeamsTabView(this);
teamsTabView = createTeamsTabView();
teamsTabView.router.navigate('teams/' + TeamSpecHelpers.testTopicID + '/no_such_team', {trigger: true}); teamsTabView.router.navigate('teams/' + TeamSpecHelpers.testTopicID + '/no_such_team', {trigger: true});
AjaxHelpers.expectRequest(requests, 'GET', '/api/team/v0/teams/no_such_team?expand=user', null); AjaxHelpers.expectRequest(requests, 'GET', '/api/team/v0/teams/no_such_team?expand=user', null);
AjaxHelpers.respondWithError(requests, 404); AjaxHelpers.respondWithError(requests, 404);
...@@ -80,8 +99,7 @@ define([ ...@@ -80,8 +99,7 @@ define([
}); });
it('displays and focuses an error message when it receives a 401 AJAX response', function () { it('displays and focuses an error message when it receives a 401 AJAX response', function () {
var requests = AjaxHelpers.requests(this), var teamsTabView = createTeamsTabView(this).render();
teamsTabView = createTeamsTabView().render();
teamsTabView.router.navigate('topics/' + TeamSpecHelpers.testTopicID, {trigger: true}); teamsTabView.router.navigate('topics/' + TeamSpecHelpers.testTopicID, {trigger: true});
AjaxHelpers.respondWithError(requests, 401); AjaxHelpers.respondWithError(requests, 401);
expectError(teamsTabView, "Your request could not be completed. Reload the page and try again."); expectError(teamsTabView, "Your request could not be completed. Reload the page and try again.");
...@@ -89,8 +107,7 @@ define([ ...@@ -89,8 +107,7 @@ define([
}); });
it('displays and focuses an error message when it receives a 500 AJAX response', function () { it('displays and focuses an error message when it receives a 500 AJAX response', function () {
var requests = AjaxHelpers.requests(this), var teamsTabView = createTeamsTabView(this).render();
teamsTabView = createTeamsTabView().render();
teamsTabView.router.navigate('topics/' + TeamSpecHelpers.testTopicID, {trigger: true}); teamsTabView.router.navigate('topics/' + TeamSpecHelpers.testTopicID, {trigger: true});
AjaxHelpers.respondWithError(requests, 500); AjaxHelpers.respondWithError(requests, 500);
expectError(teamsTabView, "Your request could not be completed due to a server problem. Reload the page and try again. If the issue persists, click the Help tab to report the problem."); expectError(teamsTabView, "Your request could not be completed due to a server problem. Reload the page and try again. If the issue persists, click the Help tab to report the problem.");
...@@ -98,7 +115,7 @@ define([ ...@@ -98,7 +115,7 @@ define([
}); });
it('does not navigate to the topics page when syncing its collection if not on the search page', function () { it('does not navigate to the topics page when syncing its collection if not on the search page', function () {
var teamsTabView = createTeamsTabView(), var teamsTabView = createTeamsTabView(this),
collection = TeamSpecHelpers.createMockTeams(); collection = TeamSpecHelpers.createMockTeams();
teamsTabView.createTeamsListView({ teamsTabView.createTeamsListView({
collection: collection, collection: collection,
...@@ -153,8 +170,7 @@ define([ ...@@ -153,8 +170,7 @@ define([
} }
] ]
}, function (url, expectedEvent) { }, function (url, expectedEvent) {
var requests = AjaxHelpers.requests(this), var teamsTabView = createTeamsTabView(this, {
teamsTabView = createTeamsTabView({
userInfo: TeamSpecHelpers.createMockUserInfo({staff: true}) userInfo: TeamSpecHelpers.createMockUserInfo({staff: true})
}); });
teamsTabView.router.navigate(url, {trigger: true}); teamsTabView.router.navigate(url, {trigger: true});
...@@ -167,7 +183,7 @@ define([ ...@@ -167,7 +183,7 @@ define([
describe('Discussion privileges', function () { describe('Discussion privileges', function () {
it('allows privileged access to any team', function () { it('allows privileged access to any team', function () {
var teamsTabView = createTeamsTabView({ var teamsTabView = createTeamsTabView(this, {
userInfo: TeamSpecHelpers.createMockUserInfo({privileged: true}) userInfo: TeamSpecHelpers.createMockUserInfo({privileged: true})
}); });
// Note: using `undefined` here to ensure that we // Note: using `undefined` here to ensure that we
...@@ -177,7 +193,7 @@ define([ ...@@ -177,7 +193,7 @@ define([
}); });
it('allows access to a team which an unprivileged user is a member of', function () { it('allows access to a team which an unprivileged user is a member of', function () {
var teamsTabView = createTeamsTabView({ var teamsTabView = createTeamsTabView(this, {
userInfo: TeamSpecHelpers.createMockUserInfo({ userInfo: TeamSpecHelpers.createMockUserInfo({
username: TeamSpecHelpers.testUser, username: TeamSpecHelpers.testUser,
privileged: false privileged: false
...@@ -195,7 +211,7 @@ define([ ...@@ -195,7 +211,7 @@ define([
}); });
it('does not allow access if the user is neither privileged nor a team member', function () { it('does not allow access if the user is neither privileged nor a team member', function () {
var teamsTabView = createTeamsTabView({ var teamsTabView = createTeamsTabView(this, {
userInfo: TeamSpecHelpers.createMockUserInfo({privileged: false, staff: true}) userInfo: TeamSpecHelpers.createMockUserInfo({privileged: false, staff: true})
}); });
expect(teamsTabView.readOnlyDiscussion({ expect(teamsTabView.readOnlyDiscussion({
...@@ -205,25 +221,10 @@ define([ ...@@ -205,25 +221,10 @@ define([
}); });
describe('Search', function () { describe('Search', function () {
var verifyTeamsRequest = function(requests, options) {
AjaxHelpers.expectRequestURL(requests, TeamSpecHelpers.testContext.teamsUrl,
_.extend(
{
topic_id: TeamSpecHelpers.testTopicID,
expand: 'user',
course_id: TeamSpecHelpers.testCourseID,
order_by: '',
page: '1',
page_size: '10',
text_search: ''
},
options
));
};
var performSearch = function(requests, teamsTabView) { var performSearch = function(requests, teamsTabView) {
teamsTabView.$('.search-field').val('foo'); teamsTabView.$('.search-field').val('foo');
teamsTabView.$('.action-search').click(); teamsTabView.$('.action-search').click();
verifyTeamsRequest(requests, { verifyTeamsRequest({
order_by: '', order_by: '',
text_search: 'foo' text_search: 'foo'
}); });
...@@ -231,11 +232,10 @@ define([ ...@@ -231,11 +232,10 @@ define([
}; };
it('can search teams', function () { it('can search teams', function () {
var requests = AjaxHelpers.requests(this), var teamsTabView = createTeamsTabView(this),
teamsTabView = createTeamsTabView(),
requestCountBeforeSearch; requestCountBeforeSearch;
teamsTabView.browseTopic(TeamSpecHelpers.testTopicID); teamsTabView.browseTopic(TeamSpecHelpers.testTopicID);
verifyTeamsRequest(requests, { verifyTeamsRequest({
order_by: 'last_activity_at', order_by: 'last_activity_at',
text_search: '' text_search: ''
}); });
...@@ -250,11 +250,9 @@ define([ ...@@ -250,11 +250,9 @@ define([
}); });
it('can clear a search', function () { it('can clear a search', function () {
var requests = AjaxHelpers.requests(this), var teamsTabView = createTeamsTabView(this);
teamsTabView = createTeamsTabView();
teamsTabView.browseTopic(TeamSpecHelpers.testTopicID); teamsTabView.browseTopic(TeamSpecHelpers.testTopicID);
AjaxHelpers.respondWithJson(requests, {}); AjaxHelpers.respondWithJson(requests, {});
performSearch(requests, teamsTabView);
// Perform a search // Perform a search
performSearch(requests, teamsTabView); performSearch(requests, teamsTabView);
...@@ -262,7 +260,7 @@ define([ ...@@ -262,7 +260,7 @@ define([
// Clear the search and submit it again // Clear the search and submit it again
teamsTabView.$('.search-field').val(''); teamsTabView.$('.search-field').val('');
teamsTabView.$('.action-search').click(); teamsTabView.$('.action-search').click();
verifyTeamsRequest(requests, { verifyTeamsRequest({
order_by: 'last_activity_at', order_by: 'last_activity_at',
text_search: '' text_search: ''
}); });
...@@ -272,8 +270,7 @@ define([ ...@@ -272,8 +270,7 @@ define([
}); });
it('can navigate back to all teams from a search', function () { it('can navigate back to all teams from a search', function () {
var requests = AjaxHelpers.requests(this), var teamsTabView = createTeamsTabView(this);
teamsTabView = createTeamsTabView();
teamsTabView.browseTopic(TeamSpecHelpers.testTopicID); teamsTabView.browseTopic(TeamSpecHelpers.testTopicID);
AjaxHelpers.respondWithJson(requests, {}); AjaxHelpers.respondWithJson(requests, {});
...@@ -283,7 +280,7 @@ define([ ...@@ -283,7 +280,7 @@ define([
// Verify the breadcrumbs have a link back to the teams list, and click on it // Verify the breadcrumbs have a link back to the teams list, and click on it
expect(teamsTabView.$('.breadcrumbs a').length).toBe(2); expect(teamsTabView.$('.breadcrumbs a').length).toBe(2);
teamsTabView.$('.breadcrumbs a').last().click(); teamsTabView.$('.breadcrumbs a').last().click();
verifyTeamsRequest(requests, { verifyTeamsRequest({
order_by: 'last_activity_at', order_by: 'last_activity_at',
text_search: '' text_search: ''
}); });
...@@ -293,8 +290,7 @@ define([ ...@@ -293,8 +290,7 @@ define([
}); });
it('does not switch to showing results when the search returns an error', function () { it('does not switch to showing results when the search returns an error', function () {
var requests = AjaxHelpers.requests(this), var teamsTabView = createTeamsTabView(this);
teamsTabView = createTeamsTabView();
teamsTabView.browseTopic(TeamSpecHelpers.testTopicID); teamsTabView.browseTopic(TeamSpecHelpers.testTopicID);
AjaxHelpers.respondWithJson(requests, {}); AjaxHelpers.respondWithJson(requests, {});
......
...@@ -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 () {
......
...@@ -3,6 +3,7 @@ define([ ...@@ -3,6 +3,7 @@ define([
'backbone', 'backbone',
'logger', 'logger',
'common/js/spec_helpers/ajax_helpers', 'common/js/spec_helpers/ajax_helpers',
'common/js/spec_helpers/page_helpers',
'common/js/spec_helpers/template_helpers', 'common/js/spec_helpers/template_helpers',
'js/search/base/models/search_result', 'js/search/base/models/search_result',
'js/search/base/collections/search_collection', 'js/search/base/collections/search_collection',
...@@ -20,6 +21,7 @@ define([ ...@@ -20,6 +21,7 @@ define([
Backbone, Backbone,
Logger, Logger,
AjaxHelpers, AjaxHelpers,
PageHelpers,
TemplateHelpers, TemplateHelpers,
SearchResult, SearchResult,
SearchCollection, SearchCollection,
...@@ -35,6 +37,10 @@ define([ ...@@ -35,6 +37,10 @@ define([
) { ) {
'use strict'; 'use strict';
describe('Search', function() {
beforeEach(function () {
PageHelpers.preventBackboneChangingUrl();
});
describe('SearchResult', function () { describe('SearchResult', function () {
...@@ -752,4 +758,5 @@ define([ ...@@ -752,4 +758,5 @@ define([
}); });
});
}); });
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