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
@wmdEditors: {}
......@@ -111,6 +98,16 @@ class @DiscussionUtil
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) ->
if $("#discussion-alert").length == 0
alertDiv = $("<div class='modal' role='alertdialog' id='discussion-alert' aria-describedby='discussion-alert-message'/>").css("display", "none")
......@@ -141,28 +138,28 @@ class @DiscussionUtil
return deferred.promise()
params["url"] = URI(params["url"]).addSearch ajax: 1
params["beforeSend"] = ->
params["beforeSend"] = =>
if $elem
$elem.attr("disabled", "disabled")
if params["$loading"]
if params["loadingCallback"]?
params["loadingCallback"].apply(params["$loading"])
else
params["$loading"].loading(params["takeFocus"])
@showLoadingIndicator($(params["$loading"]), params["takeFocus"])
if !params["error"]
params["error"] = =>
@discussionAlert(
gettext("Sorry"),
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
$elem.removeAttr("disabled")
if params["$loading"]
if params["loadedCallback"]?
params["loadedCallback"].apply(params["$loading"])
else
params["$loading"].loaded()
@hideLoadingIndicator()
return request
@updateWithUndo: (model, updates, safeAjaxParams, errorMsg) ->
......
......@@ -73,11 +73,9 @@
url: DiscussionUtil.urlFor('update_thread', this.model.id),
type: 'POST',
dataType: 'json',
async: false, // @TODO when the rest of the stuff below is made to work properly..
data: postData,
error: DiscussionUtil.formErrorHandler(this.$('.post-errors')),
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-body textarea').val('').attr('prev-text', '');
this.$('.wmd-preview p').html('');
......
......@@ -211,12 +211,10 @@ if Backbone?
url: url
type: "POST"
dataType: 'json'
async: false # TODO when the rest of the stuff below is made to work properly..
data:
body: newBody
error: DiscussionUtil.formErrorHandler(@$(".edit-post-form-errors"))
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.$(".wmd-preview p").html("")
......
......@@ -21,7 +21,9 @@ define([
];
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();
});
......@@ -40,6 +42,7 @@ define([
createTeamProfileView = function(requests, options) {
teamModel = new TeamModel(createTeamModelData(options), { parse: true });
profileView = new TeamProfileView({
el: $('.profile-view'),
teamEvents: TeamSpecHelpers.teamEvents,
courseID: TeamSpecHelpers.testCourseID,
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