diff --git a/cms/static/cms/js/spec/main_spec.js b/cms/static/cms/js/spec/main_spec.js index 8b9f27d0bd73..ec92021fab54 100644 --- a/cms/static/cms/js/spec/main_spec.js +++ b/cms/static/cms/js/spec/main_spec.js @@ -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(); @@ -34,21 +34,17 @@ }); 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(); @@ -56,22 +52,14 @@ 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 diff --git a/cms/static/js/certificates/spec/views/certificate_details_spec.js b/cms/static/js/certificates/spec/views/certificate_details_spec.js index b803bd0cbe76..ed93e3942f81 100644 --- a/cms/static/js/certificates/spec/views/certificate_details_spec.js +++ b/cms/static/js/certificates/spec/views/certificate_details_spec.js @@ -2,6 +2,7 @@ define([ 'underscore', + 'sinon', 'js/models/course', 'js/certificates/collections/certificates', 'js/certificates/models/certificate', @@ -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', @@ -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', @@ -108,6 +116,7 @@ function(_, Course, CertificatesCollection, CertificateModel, CertificateDetails }); afterEach(function() { + requests.restore(); delete window.course; delete window.certWebPreview; delete window.CMS.User; @@ -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(); diff --git a/cms/static/js/certificates/spec/views/certificate_editor_spec.js b/cms/static/js/certificates/spec/views/certificate_editor_spec.js index 64ef492c82ca..bace47fe0b4d 100644 --- a/cms/static/js/certificates/spec/views/certificate_editor_spec.js +++ b/cms/static/js/certificates/spec/views/certificate_editor_spec.js @@ -2,6 +2,7 @@ define([ 'underscore', + 'sinon', 'js/models/course', 'js/certificates/models/certificate', 'js/certificates/models/signatory', @@ -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', @@ -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(); @@ -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( $('