Skip to content

Commit

Permalink
Convert LMS features to use safe templates
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-armstrong committed May 31, 2016
1 parent 9cb660a commit 9e34399
Show file tree
Hide file tree
Showing 41 changed files with 987 additions and 834 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ define([
'teams/js/spec_helpers/team_spec_helpers',
'common/js/spec_helpers/ajax_helpers',
'common/js/spec_helpers/page_helpers'
], function ($, Backbone, _, Team, InstructorToolsView, TeamUtils, TeamSpecHelpers, AjaxHelpers, PageHelpers) {
], function($, Backbone, _, Team, InstructorToolsView, TeamUtils, TeamSpecHelpers, AjaxHelpers, PageHelpers) {
'use strict';

describe('Instructor Tools', function () {
describe('Instructor Tools', function() {
var view,
createInstructorTools = function () {
pageFixture = '<section class="teams-content">' +
'<div id="teams-message" class="wrapper-msg is-incontext urgency-low is-hidden">' +
'<div class="msg"><div class="msg-content"><div class="copy"></div></div></div>' +
'</div>' +
'<div id="page-prompt"></div>' +
'</section>',
createInstructorTools = function() {
return new InstructorToolsView({
team: new Team(TeamSpecHelpers.createMockTeamData(1, 1)[0]),
teamEvents: TeamSpecHelpers.teamEvents
});
},
deleteTeam = function (view, confirm) {
deleteTeam = function(view, confirm) {
view.$('.action-delete').click();
// Confirm delete dialog
if (confirm) {
Expand All @@ -29,29 +35,28 @@ define([
$('.action-secondary').click();
}
},
expectSuccessMessage = function (team) {
expect(TeamUtils.showMessage).toHaveBeenCalledWith(
'Team "' + team.get('name') + '" successfully deleted.',
'success'
expectSuccessMessage = function(view) {
var team = view.team;
expect($('.msg-content .copy').text().trim()).toContain(
'Team "' + team.get('name') + '" successfully deleted.'
);
};

beforeEach(function () {
setFixtures('<div id="page-prompt"></div>');
beforeEach(function() {
setFixtures(pageFixture);
PageHelpers.preventBackboneChangingUrl();
spyOn(Backbone.history, 'navigate');
spyOn(TeamUtils, 'showMessage');
view = createInstructorTools().render();
spyOn(view.teamEvents, 'trigger');
});

it('can render itself', function () {
it('can render itself', function() {
expect(_.strip(view.$('.action-delete').text())).toEqual('Delete Team');
expect(_.strip(view.$('.action-edit-members').text())).toEqual('Edit Membership');
expect(view.$el.text()).toContain('Instructor tools');
});

it('can delete a team and shows a success message', function () {
it('can delete a team and shows a success message', function() {
var requests = AjaxHelpers.requests(this);
deleteTeam(view, true);
AjaxHelpers.expectJsonRequest(requests, 'DELETE', view.team.url, null);
Expand All @@ -66,27 +71,27 @@ define([
team: view.team
}
);
expectSuccessMessage(view.team);
expectSuccessMessage(view);
});

it('can cancel team deletion', function () {
it('can cancel team deletion', function() {
var requests = AjaxHelpers.requests(this);
deleteTeam(view, false);
AjaxHelpers.expectNoRequests(requests);
expect(Backbone.history.navigate).not.toHaveBeenCalled();
});

it('shows a success message after receiving a 404', function () {
it('shows a success message after receiving a 404', function() {
var requests = AjaxHelpers.requests(this);
deleteTeam(view, true);
AjaxHelpers.respondWithError(requests, 404);
expectSuccessMessage(view.team);
});
it('can trigger the edit membership view', function () {
expectSuccessMessage(view);
});

it('can trigger the edit membership view', function() {
view.$('.action-edit-members').click();
expect(Backbone.history.navigate).toHaveBeenCalledWith(
'teams/' + view.team.get('topic_id') + "/" + view.team.id + "/edit-team/manage-members",
'teams/' + view.team.get('topic_id') + '/' + view.team.id + '/edit-team/manage-members',
{trigger: true}
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
define([
'underscore', 'common/js/spec_helpers/ajax_helpers', 'teams/js/views/team_discussion',
'teams/js/spec_helpers/team_spec_helpers',
'xmodule_js/common_static/coffee/spec/discussion/discussion_spec_helper'
], function (_, AjaxHelpers, TeamDiscussionView, TeamSpecHelpers, DiscussionSpecHelper) {
'xmodule_js/common_static/coffee/spec/discussion/discussion_spec_helper',
'edx-ui-toolkit/js/utils/string-utils'
], function(_, AjaxHelpers, TeamDiscussionView, TeamSpecHelpers, DiscussionSpecHelper, StringUtils) {
'use strict';
xdescribe('TeamDiscussionView', function() {
var discussionView, createDiscussionView, createPost, expandReplies, postReply;
Expand All @@ -23,40 +24,37 @@ define([
discussionView.render();
AjaxHelpers.expectRequest(
requests, 'GET',
interpolate(
'/courses/%(courseID)s/discussion/forum/%(discussionID)s/inline?page=1&ajax=1',
StringUtils.interpolate(
'/courses/{courseID}/discussion/forum/{discussionID}/inline?page=1&ajax=1',
{
courseID: TeamSpecHelpers.testCourseID,
discussionID: TeamSpecHelpers.testTeamDiscussionID
},
true

}
)
);
AjaxHelpers.respondWithJson(requests, TeamSpecHelpers.createMockDiscussionResponse(threads));
return discussionView;
};

createPost = function(requests, view, title, body, threadID) {
title = title || "Test title";
body = body || "Test body";
threadID = threadID || "999";
title = title || 'Test title';
body = body || 'Test body';
threadID = threadID || '999';
view.$('.new-post-button').click();
view.$('.js-post-title').val(title);
view.$('.js-post-body textarea').val(body);
view.$('.submit').click();
AjaxHelpers.expectRequest(
requests, 'POST',
interpolate(
'/courses/%(courseID)s/discussion/%(discussionID)s/threads/create?ajax=1',
StringUtils.interpolate(
'/courses/{courseID}/discussion/{discussionID}/threads/create?ajax=1',
{
courseID: TeamSpecHelpers.testCourseID,
discussionID: TeamSpecHelpers.testTeamDiscussionID
},
true
}
),
interpolate(
'thread_type=discussion&title=%(title)s&body=%(body)s&anonymous=false&anonymous_to_peers=false&auto_subscribe=true',
StringUtils.interpolate(
'thread_type=discussion&title={title}&body={body}&anonymous=false&anonymous_to_peers=false&auto_subscribe=true', // jshint ignore:line
{
title: title.replace(/ /g, '+'),
body: body.replace(/ /g, '+')
Expand All @@ -78,14 +76,13 @@ define([
view.$('.forum-thread-expand').first().click();
AjaxHelpers.expectRequest(
requests, 'GET',
interpolate(
'/courses/%(courseID)s/discussion/forum/%(discussionID)s/threads/%(threadID)s?ajax=1&resp_skip=0&resp_limit=25',
StringUtils.interpolate(
'/courses/{courseID}/discussion/forum/{discussionID}/threads/{threadID}?ajax=1&resp_skip=0&resp_limit=25', // jshint ignore:line
{
courseID: TeamSpecHelpers.testCourseID,
discussionID: TeamSpecHelpers.testTeamDiscussionID,
threadID: threadID || "999"
},
true
threadID: threadID || '999'
}
)
);
AjaxHelpers.respondWithJson(requests, {
Expand All @@ -100,13 +97,12 @@ define([
replyForm.find('.discussion-submit-post').click();
AjaxHelpers.expectRequest(
requests, 'POST',
interpolate(
'/courses/%(courseID)s/discussion/threads/%(threadID)s/reply?ajax=1',
StringUtils.interpolate(
'/courses/{courseID}/discussion/threads/{threadID}/reply?ajax=1',
{
courseID: TeamSpecHelpers.testCourseID,
threadID: threadID || "999"
},
true
threadID: threadID || '999'
}
),
'body=' + reply.replace(/ /g, '+')
);
Expand All @@ -115,7 +111,7 @@ define([
body: reply,
comments_count: 1
}),
"annotated_content_info": TeamSpecHelpers.createAnnotatedContentInfo()
annotated_content_info: TeamSpecHelpers.createAnnotatedContentInfo()
});
};

Expand Down Expand Up @@ -143,8 +139,8 @@ define([
it('can post a reply', function() {
var requests = AjaxHelpers.requests(this),
view = createDiscussionView(requests),
testReply = "Test reply",
testThreadID = "1";
testReply = 'Test reply',
testThreadID = '1';
expandReplies(requests, view, testThreadID);
postReply(requests, view, testReply, testThreadID);
expect(view.$('.discussion-response .response-body').text().trim()).toBe(testReply);
Expand All @@ -153,7 +149,7 @@ define([
it('can post a reply to a new post', function() {
var requests = AjaxHelpers.requests(this),
view = createDiscussionView(requests, []),
testReply = "Test reply";
testReply = 'Test reply';
createPost(requests, view);
expandReplies(requests, view);
postReply(requests, view, testReply);
Expand All @@ -166,7 +162,7 @@ define([
postTopicButton, updatedThreadElement,
updatedTitle = 'Updated title',
updatedBody = 'Updated body',
testThreadID = "1";
testThreadID = '1';
expandReplies(requests, view, testThreadID);
view.$('.action-more .icon').first().click();
view.$('.action-edit').first().click();
Expand All @@ -177,19 +173,18 @@ define([
view.$('.submit').click();
AjaxHelpers.expectRequest(
requests, 'POST',
interpolate(
'/courses/%(courseID)s/discussion/%(discussionID)s/threads/create?ajax=1',
StringUtils.interpolate(
'/courses/{courseID}/discussion/{discussionID}/threads/create?ajax=1',
{
courseID: TeamSpecHelpers.testCourseID,
discussionID: TeamSpecHelpers.testTeamDiscussionID
},
true
}
),
'thread_type=discussion&title=&body=Updated+body&anonymous=false&anonymous_to_peers=false&auto_subscribe=true'
'thread_type=discussion&title=&body=Updated+body&anonymous=false&anonymous_to_peers=false&auto_subscribe=true' // jshint ignore:line
);
AjaxHelpers.respondWithJson(requests, {
content: TeamSpecHelpers.createMockPostResponse({
id: "999", title: updatedTitle, body: updatedBody
id: '999', title: updatedTitle, body: updatedBody
}),
annotated_content_info: TeamSpecHelpers.createAnnotatedContentInfo()
});
Expand All @@ -202,8 +197,7 @@ define([

it('cannot move a new thread to a different topic', function() {
var requests = AjaxHelpers.requests(this),
view = createDiscussionView(requests),
postTopicButton;
view = createDiscussionView(requests);
createPost(requests, view);
expandReplies(requests, view);
view.$('.action-more .icon').first().click();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
define([
'underscore', 'common/js/spec_helpers/ajax_helpers', 'teams/js/models/team',
'teams/js/views/team_profile', 'teams/js/spec_helpers/team_spec_helpers',
'xmodule_js/common_static/coffee/spec/discussion/discussion_spec_helper'
], function (_, AjaxHelpers, TeamModel, TeamProfileView, TeamSpecHelpers, DiscussionSpecHelper) {
'xmodule_js/common_static/coffee/spec/discussion/discussion_spec_helper',
'edx-ui-toolkit/js/utils/string-utils'
], function(_, AjaxHelpers, TeamModel, TeamProfileView, TeamSpecHelpers, DiscussionSpecHelper, StringUtils) {
'use strict';
describe('TeamProfileView', function () {
describe('TeamProfileView', function() {
var profileView, createTeamProfileView, createTeamModelData, clickLeaveTeam,
teamModel,
leaveTeamLinkSelector = '.leave-team-link',
Expand All @@ -20,17 +21,17 @@ define([
}
];

beforeEach(function () {
beforeEach(function() {
setFixtures('<div id="page-prompt"></div>' +
'<div class="teams-content"><div class="msg-content"><div class="copy"></div></div></div>' +
'<div class="profile-view"></div>');
DiscussionSpecHelper.setUnderscoreFixtures();
});

createTeamModelData = function (options) {
createTeamModelData = function(options) {
return {
id: "test-team",
name: "Test Team",
id: 'test-team',
name: 'Test Team',
discussion_topic_id: TeamSpecHelpers.testTeamDiscussionID,
country: options.country || '',
language: options.language || '',
Expand All @@ -40,7 +41,7 @@ define([
};

createTeamProfileView = function(requests, options) {
teamModel = new TeamModel(createTeamModelData(options), { parse: true });
teamModel = new TeamModel(createTeamModelData(options), {parse: true});
profileView = new TeamProfileView({
el: $('.profile-view'),
teamEvents: TeamSpecHelpers.teamEvents,
Expand All @@ -55,13 +56,12 @@ define([
AjaxHelpers.expectRequest(
requests,
'GET',
interpolate(
'/courses/%(courseID)s/discussion/forum/%(topicID)s/inline?page=1&ajax=1',
StringUtils.interpolate(
'/courses/{courseID}/discussion/forum/{topicID}/inline?page=1&ajax=1',
{
courseID: TeamSpecHelpers.testCourseID,
topicID: TeamSpecHelpers.testTeamDiscussionID
},
true
}
)
);
AjaxHelpers.respondWithJson(requests, TeamSpecHelpers.createMockDiscussionResponse());
Expand Down Expand Up @@ -95,13 +95,13 @@ define([
};

describe('DiscussionsView', function() {
it('can render itself', function () {
it('can render itself', function() {
var requests = AjaxHelpers.requests(this),
view = createTeamProfileView(requests, {});
expect(view.$('.discussion-thread').length).toEqual(3);
});

it('shows New Post button when user joins a team', function () {
it('shows New Post button when user joins a team', function() {
var requests = AjaxHelpers.requests(this),
view = createTeamProfileView(requests, {});

Expand Down Expand Up @@ -187,7 +187,7 @@ define([
assertTeamDetails(view, 0, false);
});

it("wouldn't do anything if user click on Cancel button on dialog", function() {
it('should not do anything if user clicks on Cancel button on dialog', function() {
var requests = AjaxHelpers.requests(this);

var view = createTeamProfileView(
Expand All @@ -198,10 +198,10 @@ define([
assertTeamDetails(view, 1, true);
});

it('shows correct error messages', function () {
it('shows correct error messages', function() {
var requests = AjaxHelpers.requests(this);

var verifyErrorMessage = function (requests, errorMessage, expectedMessage) {
var verifyErrorMessage = function(requests, errorMessage, expectedMessage) {
var view = createTeamProfileView(
requests, {country: 'US', language: 'en', membership: DEFAULT_MEMBERSHIP}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ define([
userInfo: TeamSpecHelpers.createMockUserInfo({staff: true})
});
teamsTabView.teamsCollection = TeamSpecHelpers.createMockTeams();
if (expectedEvent.page_name === 'search-teams') {
teamsTabView.teamsCollection.searchString = 'test search string';
}
teamsTabView.router.navigate(url, {trigger: true});
if (requests.length > requests.currentIndex) {
AjaxHelpers.respondWithJson(requests, {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ define([

var createMockTeamData = function (startIndex, stopIndex) {
return _.map(_.range(startIndex, stopIndex + 1), function (i) {
var id = "id" + i;
var id = 'id' + i;
return {
name: "team " + i,
name: 'Team <' + i + '>',
id: id,
language: testLanguages[i%4][0],
country: testCountries[i%4][0],
Expand Down

0 comments on commit 9e34399

Please sign in to comment.