Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions cms/static/cms/js/spec/main_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
'use strict';

// eslint-disable-next-line global-require
require(['jquery', 'backbone', 'cms/js/main', 'edx-ui-toolkit/js/utils/spec-helpers/ajax-helpers', 'jquery.cookie'],
function($, Backbone, main, AjaxHelpers) {
require(['jquery', 'backbone', 'sinon', 'cms/js/main', 'edx-ui-toolkit/js/utils/spec-helpers/ajax-helpers', 'jquery.cookie'],
function($, Backbone, sinon, main, AjaxHelpers) {
describe('CMS', function() {
it('should initialize URL', function() {
expect(window.CMS.URL).toBeDefined();
Expand Down Expand Up @@ -34,44 +34,32 @@
});
describe('AJAX Errors', function() {
var server;
server = null;
beforeEach(function() {
server = sinon.fakeServer.create();
appendSetFixtures(sandbox({
id: 'page-notification'
}));
});
afterEach(function() {
return server && server.restore();
server.restore();
});
it('successful AJAX request does not pop an error notification', function() {
server = AjaxHelpers.server([
200, {
'Content-Type': 'application/json'
}, '{}'
]);
server.respondWith([200, {'Content-Type': 'application/json'}, '{}']);
expect($('#page-notification')).toBeEmpty();
$.ajax('/test');
expect($('#page-notification')).toBeEmpty();
server.respond();
expect($('#page-notification')).toBeEmpty();
});
it('AJAX request with error should pop an error notification', function() {
server = AjaxHelpers.server([
500, {
'Content-Type': 'application/json'
}, '{}'
]);
server.respondWith([500, {'Content-Type': 'application/json'}, '{}']);
$.ajax('/test');
server.respond();
expect($('#page-notification')).not.toBeEmpty();
expect($('#page-notification')).toContainElement('div.wrapper-notification-error');
});
it('can override AJAX request with error so it does not pop an error notification', function() {
server = AjaxHelpers.server([
500, {
'Content-Type': 'application/json'
}, '{}'
]);
server.respondWith([500, {'Content-Type': 'application/json'}, '{}']);
$.ajax({
url: '/test',
notifyOnError: false
Expand Down
14 changes: 11 additions & 3 deletions cms/static/js/certificates/spec/views/certificate_details_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

define([
'underscore',
'sinon',
'js/models/course',
'js/certificates/collections/certificates',
'js/certificates/models/certificate',
Expand All @@ -14,10 +15,12 @@ define([
'js/spec_helpers/validation_helpers',
'js/certificates/spec/custom_matchers'
],
function(_, Course, CertificatesCollection, CertificateModel, CertificateDetailsView, CertificatePreview,
function(_, sinon, Course, CertificatesCollection, CertificateModel, CertificateDetailsView, CertificatePreview,
Notification, AjaxHelpers, TemplateHelpers, ViewHelpers, ValidationHelpers, CustomMatchers) {
'use strict';

var requests, xhrFactory;

var SELECTORS = {
detailsView: '.certificate-details',
editView: '.certificate-edit',
Expand Down Expand Up @@ -58,6 +61,11 @@ function(_, Course, CertificatesCollection, CertificateModel, CertificateDetails
};

beforeEach(function() {
xhrFactory = sinon.useFakeXMLHttpRequest();
requests = [];
requests.currentIndex = 0;
requests.restore = function() { xhrFactory.restore(); };
xhrFactory.onCreate = function(req) { requests.push(req); };
window.course = new Course({
id: '5',
name: 'Course Name',
Expand Down Expand Up @@ -108,6 +116,7 @@ function(_, Course, CertificatesCollection, CertificateModel, CertificateDetails
});

afterEach(function() {
requests.restore();
delete window.course;
delete window.certWebPreview;
delete window.CMS.User;
Expand Down Expand Up @@ -228,8 +237,7 @@ function(_, Course, CertificatesCollection, CertificateModel, CertificateDetails
});

it('correctly persists changes made during in-line signatory editing', function() {
var requests = AjaxHelpers.requests(this),
notificationSpy = ViewHelpers.createNotificationSpy();
var notificationSpy = ViewHelpers.createNotificationSpy();

this.view.$(SELECTORS.edit_signatory).click();

Expand Down
22 changes: 13 additions & 9 deletions cms/static/js/certificates/spec/views/certificate_editor_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

define([
'underscore',
'sinon',
'js/models/course',
'js/certificates/models/certificate',
'js/certificates/models/signatory',
Expand All @@ -14,11 +15,12 @@ define([
'js/spec_helpers/validation_helpers',
'js/certificates/spec/custom_matchers'
],
function(_, Course, CertificateModel, SignatoryModel, CertificatesCollection, CertificateEditorView,
function(_, sinon, Course, CertificateModel, SignatoryModel, CertificatesCollection, CertificateEditorView,
Notification, AjaxHelpers, TemplateHelpers, ViewHelpers, ValidationHelpers, CustomMatchers) {
'use strict';

var MAX_SIGNATORIES_LIMIT = 10;
var requests, xhrFactory;
var SELECTORS = {
detailsView: '.certificate-details',
editView: '.certificate-edit',
Expand All @@ -45,8 +47,7 @@ function(_, Course, CertificateModel, SignatoryModel, CertificatesCollection, Ce
};

var clickDeleteItem = function(that, promptText, element, url) {
var requests = AjaxHelpers.requests(that),
promptSpy = ViewHelpers.createPromptSpy(),
var promptSpy = ViewHelpers.createPromptSpy(),
notificationSpy = ViewHelpers.createNotificationSpy();
that.view.$(element).click();

Expand Down Expand Up @@ -129,6 +130,11 @@ function(_, Course, CertificateModel, SignatoryModel, CertificatesCollection, Ce

describe('Basic', function() {
beforeEach(function() {
xhrFactory = sinon.useFakeXMLHttpRequest();
requests = [];
requests.currentIndex = 0;
requests.restore = function() { xhrFactory.restore(); };
xhrFactory.onCreate = function(req) { requests.push(req); };
appendSetFixtures(
$('<script>', {id: 'basic-modal-tpl', type: 'text/template'}).text(basicModalTpl)
);
Expand All @@ -141,6 +147,7 @@ function(_, Course, CertificateModel, SignatoryModel, CertificatesCollection, Ce
});

afterEach(function() {
requests.restore();
$('.wrapper-modal-window-assetupload').remove();
});

Expand All @@ -157,8 +164,7 @@ function(_, Course, CertificateModel, SignatoryModel, CertificatesCollection, Ce
});

it('should save properly', function() {
var requests = AjaxHelpers.requests(this),
notificationSpy = ViewHelpers.createNotificationSpy();
var notificationSpy = ViewHelpers.createNotificationSpy();
this.view.$('.action-add').click();

setValuesToInputs(this.view, {
Expand All @@ -174,8 +180,7 @@ function(_, Course, CertificateModel, SignatoryModel, CertificatesCollection, Ce
});

it('does not hide saving message if failure', function() {
var requests = AjaxHelpers.requests(this),
notificationSpy = ViewHelpers.createNotificationSpy();
var notificationSpy = ViewHelpers.createNotificationSpy();
this.view.$(SELECTORS.inputCertificateName).val('New Test Name');
this.view.$(SELECTORS.inputCertificateDescription).val('New Test Description');
ViewHelpers.submitAndVerifyFormError(this.view, requests, notificationSpy);
Expand Down Expand Up @@ -293,8 +298,7 @@ function(_, Course, CertificateModel, SignatoryModel, CertificatesCollection, Ce
});

it('signatories should save properly', function() {
var requests = AjaxHelpers.requests(this),
notificationSpy = ViewHelpers.createNotificationSpy();
var notificationSpy = ViewHelpers.createNotificationSpy();
this.view.$('.action-add').click();

setValuesToInputs(this.view, {
Expand Down
17 changes: 12 additions & 5 deletions cms/static/js/certificates/spec/views/certificate_preview_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
define([
'underscore',
'jquery',
'sinon',
'js/models/course',
'js/certificates/views/certificate_preview',
'common/js/spec_helpers/template_helpers',
'common/js/spec_helpers/view_helpers',
'edx-ui-toolkit/js/utils/spec-helpers/ajax-helpers'
],
function(_, $, Course, CertificatePreview, TemplateHelpers, ViewHelpers, AjaxHelpers) {
function(_, $, sinon, Course, CertificatePreview, TemplateHelpers, ViewHelpers, AjaxHelpers) {
'use strict';

var requests, xhrFactory;

var SELECTORS = {
course_modes: '#course-modes',
activate_certificate: '.activate-cert',
Expand All @@ -27,6 +30,11 @@ function(_, $, Course, CertificatePreview, TemplateHelpers, ViewHelpers, AjaxHel
};

beforeEach(function() {
xhrFactory = sinon.useFakeXMLHttpRequest();
requests = [];
requests.currentIndex = 0;
requests.restore = function() { xhrFactory.restore(); };
xhrFactory.onCreate = function(req) { requests.push(req); };
window.course = new Course({
id: '5',
name: 'Course Name',
Expand All @@ -50,6 +58,7 @@ function(_, $, Course, CertificatePreview, TemplateHelpers, ViewHelpers, AjaxHel
});

afterEach(function() {
requests.restore();
delete window.course;
delete window.CMS.User;
});
Expand Down Expand Up @@ -84,8 +93,7 @@ function(_, $, Course, CertificatePreview, TemplateHelpers, ViewHelpers, AjaxHel
});

it('certificate deactivation works fine', function() {
var requests = AjaxHelpers.requests(this),
notificationSpy = ViewHelpers.createNotificationSpy();
var notificationSpy = ViewHelpers.createNotificationSpy();
this.view.$(SELECTORS.activate_certificate).click();
AjaxHelpers.expectJsonRequest(requests, 'POST', '/certificates/activation/' + window.course.id, {
is_active: false
Expand All @@ -94,8 +102,7 @@ function(_, $, Course, CertificatePreview, TemplateHelpers, ViewHelpers, AjaxHel
});

it('certificate activation works fine', function() {
var requests = AjaxHelpers.requests(this),
notificationSpy = ViewHelpers.createNotificationSpy();
var notificationSpy = ViewHelpers.createNotificationSpy();
this.view.is_active = false;
this.view.$(SELECTORS.activate_certificate).click();
AjaxHelpers.expectJsonRequest(requests, 'POST', '/certificates/activation/' + window.course.id, {
Expand Down
14 changes: 9 additions & 5 deletions cms/static/js/spec/models/section_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
define(["js/models/section", "edx-ui-toolkit/js/utils/spec-helpers/ajax-helpers", "js/utils/module"], (Section, AjaxHelpers, ModuleUtils) =>
define(["js/models/section", "sinon", "edx-ui-toolkit/js/utils/spec-helpers/ajax-helpers", "js/utils/module"], (Section, sinon, AjaxHelpers, ModuleUtils) =>
describe("Section", function() {
describe("basic", function() {
beforeEach(function() {
Expand Down Expand Up @@ -36,7 +36,9 @@ define(["js/models/section", "edx-ui-toolkit/js/utils/spec-helpers/ajax-helpers"
});

describe("XHR", function() {
var server;
beforeEach(function() {
server = sinon.fakeServer.create();
spyOn(Section.prototype, 'showNotification');
spyOn(Section.prototype, 'hideNotification');
this.model = new Section({
Expand All @@ -45,9 +47,12 @@ define(["js/models/section", "edx-ui-toolkit/js/utils/spec-helpers/ajax-helpers"
});
});

it("show/hide a notification when it saves to the server", function() {
const server = AjaxHelpers.server([200, {"Content-Type": "application/json"}, "{}"]);
afterEach(function() {
server.restore();
});

it("show/hide a notification when it saves to the server", function() {
server.respondWith([200, {"Content-Type": "application/json"}, "{}"]);
this.model.save();
expect(Section.prototype.showNotification).toHaveBeenCalled();
server.respond();
Expand All @@ -56,8 +61,7 @@ define(["js/models/section", "edx-ui-toolkit/js/utils/spec-helpers/ajax-helpers"

it("don't hide notification when saving fails", function() {
// this is handled by the global AJAX error handler
const server = AjaxHelpers.server([500, {"Content-Type": "application/json"}, "{}"]);

server.respondWith([500, {"Content-Type": "application/json"}, "{}"]);
this.model.save();
server.respond();
expect(Section.prototype.hideNotification).not.toHaveBeenCalled();
Expand Down
13 changes: 9 additions & 4 deletions cms/static/js/spec/utils/drag_and_drop_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,18 +303,24 @@ function(sinon, ContentDragger, Notification, AjaxHelpers, $, _) {
});
});
describe('AJAX', function() {
var requests, xhrFactory;
beforeEach(function() {
xhrFactory = sinon.useFakeXMLHttpRequest();
requests = [];
requests.currentIndex = 0;
requests.restore = function() { xhrFactory.restore(); };
xhrFactory.onCreate = function(req) { requests.push(req); };
this.savingSpies = jasmine.stealth.spyOnConstructor(Notification, 'Mini', ['show', 'hide']);
this.savingSpies.show.and.returnValue(this.savingSpies);
this.clock = sinon.useFakeTimers();
});
afterEach(function() {
requests.restore();
this.clock.restore();
jasmine.stealth.clearSpies();
});
it('should send an update on reorder from one parent to another', function() {
var requests, request, savingOptions;
requests = AjaxHelpers.requests(this);
var request, savingOptions;
ContentDragger.dragState.dropDestination = $('#unit-4');
ContentDragger.dragState.attachMethod = 'after';
ContentDragger.dragState.parentList = $('#subsection-2');
Expand Down Expand Up @@ -345,8 +351,7 @@ function(sinon, ContentDragger, Notification, AjaxHelpers, $, _) {
expect($('#subsection-2').data('refresh')).toHaveBeenCalled();
});
it('should send an update on reorder within the same parent', function() {
var requests = AjaxHelpers.requests(this),
request;
var request;
ContentDragger.dragState.dropDestination = $('#unit-2');
ContentDragger.dragState.attachMethod = 'after';
ContentDragger.dragState.parentList = $('#subsection-1');
Expand Down
Loading
Loading