Commit fd00b4aa by Stephen Sanchez

Fix for TIM-474. Disable submit buttons during transaction.

parent f2334ad9
/**
Tests for OpenAssessment Peer view.
**/
describe("OpenAssessment.PeerView", function() {
// Stub server
var StubServer = function() {
var successPromise = $.Deferred(
function(defer) {
defer.resolve();
}
).promise();
this.peerAssess = function(optionsSelected, feedback) {
return $.Deferred(function(defer) { defer.resolve(); }).promise();
};
this.render = function(step) {
return successPromise;
};
};
// Stub runtime
var runtime = {};
// Stubs
var server = null;
// View under test
var view = null;
beforeEach(function() {
// Load the DOM fixture
jasmine.getFixtures().fixturesPath = 'base/fixtures';
loadFixtures('oa_peer_assessment.html');
// Create a new stub server
server = new StubServer();
// Create the object under test
var el = $("#openassessment").get(0);
view = new OpenAssessment.BaseView(runtime, el, server);
});
it("re-enables the peer assess button on error", function() {
// Simulate a server error
spyOn(server, 'peerAssess').andCallFake(function() {
expect(view.peerSubmitEnabled()).toBe(false);
return $.Deferred(function(defer) {
defer.rejectWith(this, ['ENOUNKNOWN', 'Error occurred!']);
}).promise();
});
view.peerAssess();
// Expect the submit button to have been re-enabled
expect(view.peerSubmitEnabled()).toBe(true);
});
});
/**
Tests for OpenAssessment Self view.
**/
describe("OpenAssessment.SelfView", function() {
// Stub server
var StubServer = function() {
var successPromise = $.Deferred(
function(defer) {
defer.resolve();
}
).promise();
this.selfAssess = function(optionsSelected) {
return $.Deferred(function(defer) { defer.resolve(); }).promise();
};
this.render = function(step) {
return successPromise;
};
};
// Stub base view
var StubBaseView = function() {
this.showLoadError = function(msg) {};
this.toggleActionError = function(msg, step) {};
this.setUpCollapseExpand = function(sel) {};
};
// Stub runtime
var runtime = {};
// Stubs
var server = null;
// View under test
var view = null;
beforeEach(function() {
// Load the DOM fixture
jasmine.getFixtures().fixturesPath = 'base/fixtures';
loadFixtures('oa_self_assessment.html');
// Create a new stub server
server = new StubServer();
// Create the object under test
var el = $("#openassessment").get(0);
view = new OpenAssessment.BaseView(runtime, el, server);
});
it("re-enables the self assess button on error", function() {
// Simulate a server error
spyOn(server, 'selfAssess').andCallFake(function() {
expect(view.selfSubmitEnabled()).toBe(false);
return $.Deferred(function(defer) {
defer.rejectWith(this, ['ENOUNKNOWN', 'Error occurred!']);
}).promise();
});
view.selfAssess();
// Expect the submit button to have been re-enabled
expect(view.selfSubmitEnabled()).toBe(true);
});
});
......@@ -216,6 +216,54 @@ OpenAssessment.BaseView.prototype = {
},
/**
Enable/disable the peer assess button button.
Check that whether the peer assess button is enabled.
Args:
enabled (bool): If specified, set the state of the button.
Returns:
bool: Whether the button is enabled.
Examples:
>> view.peerSubmitEnabled(true); // enable the button
>> view.peerSubmitEnabled(); // check whether the button is enabled
>> true
**/
peerSubmitEnabled: function(enabled) {
var button = $('#peer-assessment--001__assessment__submit', this.element);
if (typeof enabled === 'undefined') {
return !button.hasClass('is--disabled');
} else {
button.toggleClass('is--disabled', !enabled)
}
},
/**
Enable/disable the self assess button.
Check that whether the self assess button is enabled.
Args:
enabled (bool): If specified, set the state of the button.
Returns:
bool: Whether the button is enabled.
Examples:
>> view.selfSubmitEnabled(true); // enable the button
>> view.selfSubmitEnabled(); // check whether the button is enabled
>> true
**/
selfSubmitEnabled: function(enabled) {
var button = $('#self-assessment--001__assessment__submit', this.element);
if (typeof enabled === 'undefined') {
return !button.hasClass('is--disabled');
} else {
button.toggleClass('is--disabled', !enabled)
}
},
/**
Send an assessment to the server and update the view.
**/
peerAssess: function() {
......@@ -261,11 +309,14 @@ OpenAssessment.BaseView.prototype = {
// Send the assessment to the server
var view = this;
this.toggleActionError('peer', null);
view.toggleActionError('peer', null);
view.peerSubmitEnabled(false);
this.server.peerAssess(optionsSelected, feedback).done(
successFunction
).fail(function(errMsg) {
view.toggleActionError('peer', errMsg);
view.peerSubmitEnabled(true);
});
},
......@@ -283,7 +334,9 @@ OpenAssessment.BaseView.prototype = {
// Send the assessment to the server
var view = this;
this.toggleActionError('self', null);
view.toggleActionError('self', null);
view.selfSubmitEnabled(false);
this.server.selfAssess(optionsSelected).done(
function() {
view.renderPeerAssessmentStep();
......@@ -293,6 +346,7 @@ OpenAssessment.BaseView.prototype = {
}
).fail(function(errMsg) {
view.toggleActionError('self', errMsg);
view.selfSubmitEnabled(true);
});
},
......
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