Commit 8914aaf8 by Andy Armstrong

Refactor discussions code to address flaky test

TNL-3247

Removed remaining synchronous AJAX calls from discussions
Updated the profile view tests to use an attached view for testing
Removed one-time JQuery intiialization that could fail and break multiple tests
parent c1686a93
$ ->
if !window.$$contents
window.$$contents = {}
$.fn.extend
loading: (takeFocus) ->
@$_loading = $("<div class='loading-animation' tabindex='0'><span class='sr'>" + gettext("Loading content") + "</span></div>")
$(this).after(@$_loading)
if takeFocus
DiscussionUtil.makeFocusTrap(@$_loading)
@$_loading.focus()
loaded: ->
@$_loading.remove()
class @DiscussionUtil class @DiscussionUtil
@wmdEditors: {} @wmdEditors: {}
...@@ -111,6 +98,16 @@ class @DiscussionUtil ...@@ -111,6 +98,16 @@ class @DiscussionUtil
event.preventDefault() event.preventDefault()
) )
@showLoadingIndicator: (element, takeFocus) ->
@$_loading = $("<div class='loading-animation' tabindex='0'><span class='sr'>" + gettext("Loading content") + "</span></div>")
element.after(@$_loading)
if takeFocus
@makeFocusTrap(@$_loading)
@$_loading.focus()
@hideLoadingIndicator: () ->
@$_loading.remove()
@discussionAlert: (header, body) -> @discussionAlert: (header, body) ->
if $("#discussion-alert").length == 0 if $("#discussion-alert").length == 0
alertDiv = $("<div class='modal' role='alertdialog' id='discussion-alert' aria-describedby='discussion-alert-message'/>").css("display", "none") alertDiv = $("<div class='modal' role='alertdialog' id='discussion-alert' aria-describedby='discussion-alert-message'/>").css("display", "none")
...@@ -141,28 +138,28 @@ class @DiscussionUtil ...@@ -141,28 +138,28 @@ class @DiscussionUtil
return deferred.promise() return deferred.promise()
params["url"] = URI(params["url"]).addSearch ajax: 1 params["url"] = URI(params["url"]).addSearch ajax: 1
params["beforeSend"] = -> params["beforeSend"] = =>
if $elem if $elem
$elem.attr("disabled", "disabled") $elem.attr("disabled", "disabled")
if params["$loading"] if params["$loading"]
if params["loadingCallback"]? if params["loadingCallback"]?
params["loadingCallback"].apply(params["$loading"]) params["loadingCallback"].apply(params["$loading"])
else else
params["$loading"].loading(params["takeFocus"]) @showLoadingIndicator($(params["$loading"]), params["takeFocus"])
if !params["error"] if !params["error"]
params["error"] = => params["error"] = =>
@discussionAlert( @discussionAlert(
gettext("Sorry"), gettext("Sorry"),
gettext("We had some trouble processing your request. Please ensure you have copied any unsaved work and then reload the page.") gettext("We had some trouble processing your request. Please ensure you have copied any unsaved work and then reload the page.")
) )
request = $.ajax(params).always -> request = $.ajax(params).always =>
if $elem if $elem
$elem.removeAttr("disabled") $elem.removeAttr("disabled")
if params["$loading"] if params["$loading"]
if params["loadedCallback"]? if params["loadedCallback"]?
params["loadedCallback"].apply(params["$loading"]) params["loadedCallback"].apply(params["$loading"])
else else
params["$loading"].loaded() @hideLoadingIndicator()
return request return request
@updateWithUndo: (model, updates, safeAjaxParams, errorMsg) -> @updateWithUndo: (model, updates, safeAjaxParams, errorMsg) ->
......
...@@ -73,11 +73,9 @@ ...@@ -73,11 +73,9 @@
url: DiscussionUtil.urlFor('update_thread', this.model.id), url: DiscussionUtil.urlFor('update_thread', this.model.id),
type: 'POST', type: 'POST',
dataType: 'json', dataType: 'json',
async: false, // @TODO when the rest of the stuff below is made to work properly..
data: postData, data: postData,
error: DiscussionUtil.formErrorHandler(this.$('.post-errors')), error: DiscussionUtil.formErrorHandler(this.$('.post-errors')),
success: function() { success: function() {
// @TODO: Move this out of the callback, this makes it feel sluggish
this.$('.edit-post-title').val('').attr('prev-text', ''); this.$('.edit-post-title').val('').attr('prev-text', '');
this.$('.edit-post-body textarea').val('').attr('prev-text', ''); this.$('.edit-post-body textarea').val('').attr('prev-text', '');
this.$('.wmd-preview p').html(''); this.$('.wmd-preview p').html('');
......
...@@ -211,12 +211,10 @@ if Backbone? ...@@ -211,12 +211,10 @@ if Backbone?
url: url url: url
type: "POST" type: "POST"
dataType: 'json' dataType: 'json'
async: false # TODO when the rest of the stuff below is made to work properly..
data: data:
body: newBody body: newBody
error: DiscussionUtil.formErrorHandler(@$(".edit-post-form-errors")) error: DiscussionUtil.formErrorHandler(@$(".edit-post-form-errors"))
success: (response, textStatus) => success: (response, textStatus) =>
# TODO: Move this out of the callback, this makes it feel sluggish
@editView.$(".edit-post-body textarea").val("").attr("prev-text", "") @editView.$(".edit-post-body textarea").val("").attr("prev-text", "")
@editView.$(".wmd-preview p").html("") @editView.$(".wmd-preview p").html("")
......
...@@ -21,7 +21,9 @@ define([ ...@@ -21,7 +21,9 @@ 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>'); 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(); DiscussionSpecHelper.setUnderscoreFixtures();
}); });
...@@ -40,6 +42,7 @@ define([ ...@@ -40,6 +42,7 @@ define([
createTeamProfileView = function(requests, options) { createTeamProfileView = function(requests, options) {
teamModel = new TeamModel(createTeamModelData(options), { parse: true }); teamModel = new TeamModel(createTeamModelData(options), { parse: true });
profileView = new TeamProfileView({ profileView = new TeamProfileView({
el: $('.profile-view'),
teamEvents: TeamSpecHelpers.teamEvents, teamEvents: TeamSpecHelpers.teamEvents,
courseID: TeamSpecHelpers.testCourseID, courseID: TeamSpecHelpers.testCourseID,
context: TeamSpecHelpers.testContext, context: TeamSpecHelpers.testContext,
......
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