Commit 139f91eb by Jeff LaJoie

TNL-5619: removes enable/disable all button methods to save button state and…

TNL-5619: removes enable/disable all button methods to save button state and disable active, modifies tests for new functions
parent 1c0a60eb
...@@ -236,56 +236,26 @@ describe 'Problem', -> ...@@ -236,56 +236,26 @@ describe 'Problem', ->
expect(@problem.el).toHaveHtml contents expect(@problem.el).toHaveHtml contents
expect(window.SR.readTexts).toHaveBeenCalledWith ['no, try again'] expect(window.SR.readTexts).toHaveBeenCalledWith ['no, try again']
it 'tests if all the capa buttons are disabled while submitting', (done)-> it 'tests if the submit button is disabled while submitting and the text changes on the button', ->
deferred = $.Deferred()
self = this self = this
curr_html = @problem.el.html()
runs = -> spyOn($, 'postWithPrefix').and.callFake (url, answers, callback) ->
spyOn($, 'postWithPrefix').and.callFake (url, answers, callback) -> # At this point enableButtons should have been called, making the submit button disabled with text 'submitting'
promise = undefined expect(self.problem.submitButton).toHaveAttr('disabled');
callback expect(self.problem.submitButtonLabel.text()).toBe('Submitting');
success: 'incorrect' callback
contents: 'Incorrect' success: 'incorrect' # does not matter if correct or incorrect here
promise = contents: curr_html
always: (callable) -> promise =
callable() always: (callable) -> callable()
done: (callable) -> done: (callable) -> callable()
callable() # Make sure the submit button is enabled before submitting
spyOn @problem, 'enableAllButtons' $('#input_example_1').val('test').trigger('input')
@problem.submit() expect(@problem.submitButton).not.toHaveAttr('disabled')
expect(@problem.enableAllButtons).toHaveBeenCalledWith false, true @problem.submit()
if jQuery.active == 0 # After submit, the button should not be disabled and should have text as 'Submit'
deferred.resolve() expect(@problem.submitButtonLabel.text()).toBe('Submit')
deferred.promise() expect(@problem.submitButton).not.toHaveAttr('disabled')
runs.call(self).then(->
expect(self.problem.enableAllButtons).toHaveBeenCalledWith true, true
return
).always done
it 'tests the expected change in text of submit button', (done) ->
deferred = $.Deferred()
self = this
runs = ->
spyOn($, 'postWithPrefix').and.callFake (url, answers, callback) ->
promise = undefined
promise =
always: (callable) ->
callable()
done: (callable) ->
callable()
spyOn @problem.submitButtonLabel, 'text'
@problem.submit()
expect(@problem.submitButtonLabel.text).toHaveBeenCalledWith 'Submitting'
if jQuery.active == 0
deferred.resolve()
deferred.promise()
runs.call(self).then(->
expect(self.problem.submitButtonLabel.text).toHaveBeenCalledWith 'Submit'
return
).always done
describe 'submit button on problems', -> describe 'submit button on problems', ->
beforeEach -> beforeEach ->
...@@ -424,27 +394,22 @@ describe 'Problem', -> ...@@ -424,27 +394,22 @@ describe 'Problem', ->
@problem.reset() @problem.reset()
expect($('.notification-gentle-alert .notification-message').text()).toEqual("Error on reset.") expect($('.notification-gentle-alert .notification-message').text()).toEqual("Error on reset.")
it 'tests if all the buttons are disabled and the text of submit button remains same while resetting', (done) -> it 'tests that reset does not enable submit or modify the text while resetting', ->
deferred = $.Deferred()
self = this self = this
curr_html = @problem.el.html()
runs = -> spyOn($, 'postWithPrefix').and.callFake (url, answers, callback) ->
spyOn($, 'postWithPrefix').and.callFake (url, answers, callback) -> # enableButtons should have been called at this point to set them to all disabled
promise = undefined expect(self.problem.submitButton).toHaveAttr('disabled')
promise = always: (callable) -> expect(self.problem.submitButtonLabel.text()).toBe('Submit')
callable() callback(success: 'correct', html: curr_html)
spyOn @problem, 'enableAllButtons' promise =
@problem.reset() always: (callable) -> callable()
expect(@problem.enableAllButtons).toHaveBeenCalledWith false, false # Submit should be disabled
expect(@problem.submitButtonLabel).toHaveText 'Submit' expect(@problem.submitButton).toHaveAttr('disabled')
if jQuery.active == 0 @problem.reset()
deferred.resolve() # Submit should remain disabled
deferred.promise() expect(self.problem.submitButton).toHaveAttr('disabled')
expect(self.problem.submitButtonLabel.text()).toBe('Submit')
runs.call(self).then(->
expect(self.problem.enableAllButtons).toHaveBeenCalledWith true, false
expect(self.problem.submitButtonLabel).toHaveText 'Submit'
).always done
describe 'show', -> describe 'show', ->
beforeEach -> beforeEach ->
...@@ -483,22 +448,14 @@ describe 'Problem', -> ...@@ -483,22 +448,14 @@ describe 'Problem', ->
@problem.show() @problem.show()
expect(@problem.el.find('.show').attr('disabled')).toEqual('disabled') expect(@problem.el.find('.show').attr('disabled')).toEqual('disabled')
it 'sends a SR message when answer is present', (done) -> it 'sends a SR message when answer is present', ->
deferred = $.Deferred()
runs = -> spyOn($, 'postWithPrefix').and.callFake (url, callback) ->
spyOn($, 'postWithPrefix').and.callFake (url, callback) -> callback answers:
callback answers: '1_1': 'answers'
'1_1': 'answers' @problem.show()
@problem.show()
if jQuery.active == 0
deferred.resolve()
deferred.promise()
runs.call(this).then(-> expect(window.SR.readText).toHaveBeenCalledWith 'Answers to this problem are now shown. Navigate through the problem to review it with answers inline.'
expect(window.SR.readText).toHaveBeenCalledWith 'Answers to this problem are now shown. Navigate through the problem to review it with answers inline.'
return
).always done
describe 'multiple choice question', -> describe 'multiple choice question', ->
beforeEach -> beforeEach ->
...@@ -723,28 +680,42 @@ describe 'Problem', -> ...@@ -723,28 +680,42 @@ describe 'Problem', ->
expect($.postWithPrefix).toHaveBeenCalledWith '/problem/Problem1/problem_save', expect($.postWithPrefix).toHaveBeenCalledWith '/problem/Problem1/problem_save',
'foo=1&bar=2', jasmine.any(Function) 'foo=1&bar=2', jasmine.any(Function)
it 'tests if all the buttons are disabled and the text of submit button does not change while saving.', (done) -> it 'tests that save does not enable the submit button or change the text when submit is originally disabled', ->
deferred = $.Deferred()
self = this self = this
curr_html = @problem.el.html() curr_html = @problem.el.html()
runs = -> spyOn($, 'postWithPrefix').and.callFake (url, answers, callback) ->
spyOn($, 'postWithPrefix').and.callFake (url, answers, callback) -> # enableButtons should have been called at this point and the submit button should be unaffected
promise = undefined expect(self.problem.submitButton).toHaveAttr('disabled')
callback(success: 'correct', html: curr_html) expect(self.problem.submitButtonLabel.text()).toBe('Submit')
promise = always: (callable) -> callback(success: 'correct', html: curr_html)
callable() promise =
spyOn @problem, 'enableAllButtons' always: (callable) -> callable()
@problem.save() # Expect submit to be disabled and labeled properly at the start
expect(@problem.enableAllButtons).toHaveBeenCalledWith false, false expect(@problem.submitButton).toHaveAttr('disabled')
expect(@problem.submitButtonLabel).toHaveText 'Submit' expect(@problem.submitButtonLabel.text()).toBe('Submit')
if jQuery.active == 0 @problem.save()
deferred.resolve() # Submit button should have the same state after save has completed
deferred.promise() expect(@problem.submitButton).toHaveAttr('disabled')
expect(@problem.submitButtonLabel.text()).toBe('Submit')
runs.call(self).then(->
expect(self.problem.enableAllButtons).toHaveBeenCalledWith true, false it 'tests that save does not disable the submit button or change the text when submit is originally enabled', ->
expect(self.problem.submitButtonLabel).toHaveText 'Submit' self = this
).always done curr_html = @problem.el.html()
spyOn($, 'postWithPrefix').and.callFake (url, answers, callback) ->
# enableButtons should have been called at this point, and the submit button should be disabled while submitting
expect(self.problem.submitButton).toHaveAttr('disabled')
expect(self.problem.submitButtonLabel.text()).toBe('Submit')
callback(success: 'correct', html: curr_html)
promise =
always: (callable) -> callable()
# Expect submit to be enabled and labeled properly at the start after adding an input
$('#input_example_1').val('test').trigger('input')
expect(@problem.submitButton).not.toHaveAttr('disabled')
expect(@problem.submitButtonLabel.text()).toBe('Submit')
@problem.save()
# Submit button should have the same state after save has completed
expect(@problem.submitButton).not.toHaveAttr('disabled')
expect(@problem.submitButtonLabel.text()).toBe('Submit')
describe 'refreshMath', -> describe 'refreshMath', ->
beforeEach -> beforeEach ->
......
...@@ -36,9 +36,6 @@ ...@@ -36,9 +36,6 @@
} }
return Problem.prototype.enableSubmitButton.apply(that, arguments); return Problem.prototype.enableSubmitButton.apply(that, arguments);
}; };
this.enableAllButtons = function(enable, isFromCheckOperation) { // eslint-disable-line no-unused-vars
return Problem.prototype.enableAllButtons.apply(that, arguments);
};
this.disableAllButtonsWhileRunning = function( this.disableAllButtonsWhileRunning = function(
operationCallback, isFromCheckOperation // eslint-disable-line no-unused-vars operationCallback, isFromCheckOperation // eslint-disable-line no-unused-vars
) { ) {
...@@ -1157,32 +1154,38 @@ ...@@ -1157,32 +1154,38 @@
*/ */
Problem.prototype.disableAllButtonsWhileRunning = function(operationCallback, isFromCheckOperation) { Problem.prototype.disableAllButtonsWhileRunning = function(operationCallback, isFromCheckOperation) {
var that = this; var that = this;
this.enableAllButtons(false, isFromCheckOperation); var allButtons = [this.resetButton, this.saveButton, this.showButton, this.hintButton, this.submitButton];
var initiallyEnabledButtons = allButtons.filter(function(button) {
return !button.attr('disabled');
});
this.enableButtons(initiallyEnabledButtons, false, isFromCheckOperation);
return operationCallback().always(function() { return operationCallback().always(function() {
return that.enableAllButtons(true, isFromCheckOperation); return that.enableButtons(initiallyEnabledButtons, true, isFromCheckOperation);
}); });
}; };
/** /**
* Used to enable/disable all buttons in problem. * Enables/disables buttons by removing/adding the disabled attribute. The submit button is checked
* separately due to the changing text it contains.
* *
* params: * params:
* 'enable' is a boolean to determine enabling/disabling of buttons. * 'buttons' is an array of buttons that will have their 'disabled' attribute modified
* 'isFromCheckOperation' is a boolean to keep track if operation was initiated * 'enable' a boolean to either enable or disable the buttons passed in the first parameter
* 'changeSubmitButtonText' is a boolean to keep track if operation was initiated
* from submit so that text of submit button will also be changed while disabling/enabling * from submit so that text of submit button will also be changed while disabling/enabling
* the submit button. * the submit button.
*/ */
Problem.prototype.enableAllButtons = function(enable, isFromCheckOperation) { Problem.prototype.enableButtons = function(buttons, enable, changeSubmitButtonText) {
// Called by disableAllButtonsWhileRunning to automatically disable all buttons while check,reset, or var that = this;
// save internal are running. Then enable all the buttons again after it is done. buttons.forEach(function(button) {
if (enable) { if (button.hasClass('submit')) {
this.resetButton.add(this.saveButton).add(this.hintButton).add(this.showButton). that.enableSubmitButton(enable, changeSubmitButtonText);
removeAttr('disabled'); } else if (enable) {
} else { button.removeAttr('disabled');
this.resetButton.add(this.saveButton).add(this.hintButton).add(this.showButton). } else {
attr({disabled: 'disabled'}); button.attr({disabled: 'disabled'});
} }
return this.enableSubmitButton(enable, isFromCheckOperation); });
}; };
/** /**
......
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