From 1f886478755afb546550a9e82f058138b32820d1 Mon Sep 17 00:00:00 2001 From: Alexandre Pretto Nunes Date: Wed, 22 Oct 2014 15:14:01 -0200 Subject: Update JS unittests to use a deferred to mock ajax calls --- .../test/spec/mail_view/data/mail_sender.spec.js | 12 ++-- web-ui/test/spec/services/mail_service.spec.js | 75 +++++++++++++--------- web-ui/test/spec/tags/data/tags.spec.js | 21 +++--- 3 files changed, 65 insertions(+), 43 deletions(-) (limited to 'web-ui') diff --git a/web-ui/test/spec/mail_view/data/mail_sender.spec.js b/web-ui/test/spec/mail_view/data/mail_sender.spec.js index 3f6a2eaa..48d46fde 100644 --- a/web-ui/test/spec/mail_view/data/mail_sender.spec.js +++ b/web-ui/test/spec/mail_view/data/mail_sender.spec.js @@ -14,13 +14,13 @@ describeComponent('mail_view/data/mail_sender', function () { it('sends mail data with a POST to the server when asked to send email', function() { var mailSentEventSpy = spyOnEvent(document, Pixelated.events.mail.sent); - var g; + var deferred = $.Deferred(); - spyOn($, 'ajax').and.returnValue({done: function(f) { g = f; return {fail: function(){}};}}); + spyOn($, 'ajax').and.returnValue(deferred); this.component.trigger(Pixelated.events.mail.send, mail); - g(); + deferred.resolve(); expect(mailSentEventSpy).toHaveBeenTriggeredOn(document); @@ -32,14 +32,14 @@ describeComponent('mail_view/data/mail_sender', function () { it('save draft data with a PUT to the server', function() { var draftSavedEventSpy = spyOnEvent(document, Pixelated.events.mail.draftSaved); - var g; + var deferred = $.Deferred(); - spyOn($, 'ajax').and.returnValue({done: function(f) { g = f; return {fail: function(){}};}}); + spyOn($, 'ajax').and.returnValue(deferred); mail.ident = 0; this.component.trigger(Pixelated.events.mail.saveDraft, mail); - g(); + deferred.resolve(); expect(draftSavedEventSpy).toHaveBeenTriggeredOn(document); diff --git a/web-ui/test/spec/services/mail_service.spec.js b/web-ui/test/spec/services/mail_service.spec.js index eba81ef3..cdd9ad7d 100644 --- a/web-ui/test/spec/services/mail_service.spec.js +++ b/web-ui/test/spec/services/mail_service.spec.js @@ -21,19 +21,21 @@ describeComponent('services/mail_service', function () { }); describe('when marks many emails as read', function () { - var readRequest, checkedMails, uncheckedEmailsEvent, setCheckAllEvent, doneMarkAsRead; + var readRequest, checkedMails, uncheckedEmailsEvent, setCheckAllEvent, deferred; beforeEach(function () { - readRequest = spyOn($, 'ajax').and.returnValue({done: function(f) { doneMarkAsRead = f; return {fail: function() {}};}}); - uncheckedEmailsEvent = spyOnEvent(document, Pixelated.events.ui.mail.unchecked); - setCheckAllEvent = spyOnEvent(document, Pixelated.events.ui.mails.hasMailsChecked); - spyOn(this.component, 'refreshResults'); - checkedMails = { 1: {ident: 1}, 2: {ident: 2} }; + deferred = $.Deferred(); + readRequest = spyOn($, 'ajax').and.returnValue(deferred); + + uncheckedEmailsEvent = spyOnEvent(document, Pixelated.events.ui.mail.unchecked); + setCheckAllEvent = spyOnEvent(document, Pixelated.events.ui.mails.hasMailsChecked); + spyOn(this.component, 'refreshResults'); + this.component.trigger(Pixelated.events.mail.read, {checkedMails: checkedMails}); }); @@ -43,46 +45,53 @@ describeComponent('services/mail_service', function () { }); it('will trigger that a message has been deleted when it is done deleting', function() { - doneMarkAsRead({mails: checkedMails}); + deferred.resolve(checkedMails); expect(this.component.refreshResults).toHaveBeenCalled(); }); it('unchecks read emails', function () { - doneMarkAsRead({mails: checkedMails}); + deferred.resolve(checkedMails); expect(uncheckedEmailsEvent).toHaveBeenTriggeredOnAndWith(document, {mails: checkedMails}); }); it('clears the check all checkbox', function () { - doneMarkAsRead({mails: checkedMails}); + deferred.resolve(checkedMails); expect(setCheckAllEvent).toHaveBeenTriggeredOnAndWith(document, false); }); }); it('fetches a single email', function () { var me = {}; - var spyAjax = spyOn($, 'ajax').and.returnValue({done: function(f) { f(email1); return {fail: function() {}};}}); + var deferred = $.Deferred(); + var spyAjax = spyOn($, 'ajax').and.returnValue(deferred); var mailHereEvent = spyOnEvent(me, Pixelated.events.mail.here); this.component.trigger(Pixelated.events.mail.want, { caller: me, mail: email1.ident }); + deferred.resolve(); + expect(mailHereEvent).toHaveBeenTriggeredOn(me); expect(spyAjax.calls.mostRecent().args[0]).toEqual('/mail/' + email1.ident); }); it('answers mail:notFound if mail returned from server is null', function () { var me = {}; - var spyAjax = spyOn($, 'ajax').and.returnValue({done: function(f) { f(null); return {fail: function() {}};}}); + var deferred = $.Deferred(); + var spyAjax = spyOn($, 'ajax').and.returnValue(deferred); var mailNotFound = spyOnEvent(me, Pixelated.events.mail.notFound); this.component.trigger(Pixelated.events.mail.want, { caller: me, mail: email1.ident }); + deferred.resolve(null); + expect(mailNotFound).toHaveBeenTriggeredOn(me); }); it('updates the tags of the desired message', function () { spyOn(this.component, 'refreshResults'); var updateTagsReturnValue = { tags: ['website'], mailbox: 'inbox'} - var spyAjax = spyOn($, 'ajax').and.returnValue({done: function(f) { f(updateTagsReturnValue); return {fail: function() {}};}}); + var deferred = $.Deferred(); + var spyAjax = spyOn($, 'ajax').and.returnValue(deferred); var spyEvent = spyOnEvent(document, Pixelated.events.mail.tags.updated); var component = jasmine.createSpyObj('component',['successUpdateTags']); @@ -90,6 +99,8 @@ describeComponent('services/mail_service', function () { this.component.trigger(Pixelated.events.mail.tags.update, { ident: email1.ident, tags: email1.tags }); + deferred.resolve(updateTagsReturnValue); + expect(spyEvent).toHaveBeenTriggeredOn(document); expect(spyAjax.calls.all()[0].args[0]).toEqual('/mail/1/tags'); expect(spyAjax.calls.all()[0].args[1].data).toEqual(JSON.stringify({ newtags: email1.tags } )); @@ -97,20 +108,23 @@ describeComponent('services/mail_service', function () { }); it('triggers an error message when it can\'t update the tags', function () { - var spyAjax = spyOn($, 'ajax').and.returnValue({done: function() { return {fail: function(f) {f({status:500});}};}}); + var deferred = $.Deferred(); + var spyAjax = spyOn($, 'ajax').and.returnValue(deferred); var spyEvent = spyOnEvent(document, Pixelated.events.ui.userAlerts.displayMessage); var component = jasmine.createSpyObj('component',['failureUpdateTags']); this.component.trigger(Pixelated.events.mail.tags.update, { ident: email1.ident, tags: email1.tags }); + deferred.reject({}, 500); + expect(spyEvent).toHaveBeenTriggeredOn(document); expect(spyAjax.calls.mostRecent().args[0]).toEqual('/mail/1/tags'); expect(spyAjax.calls.mostRecent().args[1].data).toEqual(JSON.stringify({ newtags: email1.tags } )); }); it('will try to delete a message when requested to', function() { - var spyAjax = spyOn($, 'ajax').and.returnValue({done: function() { return {fail: function(f) {}};}}); + var spyAjax = spyOn($, 'ajax').and.returnValue($.Deferred()); this.component.trigger(Pixelated.events.mail.delete, {mail: {ident: '43'}}); expect(spyAjax).toHaveBeenCalled(); expect(spyAjax.calls.mostRecent().args[0]).toEqual('/mail/43'); @@ -155,29 +169,33 @@ describeComponent('services/mail_service', function () { }); it('will trigger an error message when a message cannot be deleted', function() { - spyOn($, 'ajax').and.returnValue({done: function() { return {fail: function(f) { f(); }};}}); + var deferred = $.Deferred(); + var spyAjax = spyOn($, 'ajax').and.returnValue(deferred); var spyEvent = spyOnEvent(document, Pixelated.events.ui.userAlerts.displayMessage); this.component.trigger(Pixelated.events.mail.delete, {mail: {ident: '43'}}); + deferred.reject(); + expect(spyEvent).toHaveBeenTriggeredOnAndWith(document, {message: i18n('Could not delete email')} ); }); it('triggers mails:available with received mails and keeps that tag as the current tag', function() { - var g; var eventSpy = spyOnEvent(document, Pixelated.events.mails.available); - spyOn($, 'ajax').and.returnValue({done: function(f) { g = f; return {fail: function(){}};}}); + var deferred = $.Deferred(); + var spyAjax = spyOn($, 'ajax').and.returnValue(deferred); this.component.trigger(Pixelated.events.ui.mails.fetchByTag, {tag: 'inbox'}); - g({stats: {}, mails: [email1]}); + deferred.resolve({stats: {}, mails: [email1]}); + expect(eventSpy.mostRecentCall.data.stats).toEqual({}); expect(eventSpy.mostRecentCall.data.tag).toEqual('inbox'); expect(this.component.attr.currentTag).toEqual('inbox'); }); it('wraps the tag in quotes before fetching by tag (to support tags with spaces)', function () { - spyOn($, 'ajax').and.returnValue({done: function(f) { return {fail: function(){}};}}); + spyOn($, 'ajax').and.returnValue($.Deferred()); this.component.trigger(Pixelated.events.ui.mails.fetchByTag, {tag: 'new tag'}); @@ -185,28 +203,27 @@ describeComponent('services/mail_service', function () { }); it('sends the previous tag when mails:refresh is called without a tag (this happens when the refresher calls it)', function () { - var g; var eventSpy = spyOnEvent(document, Pixelated.events.mails.availableForRefresh); this.component.attr.currentTag = 'sent'; - spyOn($, 'ajax').and.returnValue({done: function(f) { g = f; return {fail: function(){}};}}); + var deferred = $.Deferred(); + var spyAjax = spyOn($, 'ajax').and.returnValue(deferred); this.component.trigger(Pixelated.events.ui.mails.refresh); - g({stats: {}, mails: [email1]}); + deferred.resolve({stats: {}, mails: [email1]}); + expect(eventSpy.mostRecentCall.data.tag).toEqual('sent'); expect(eventSpy.mostRecentCall.data.stats).toEqual({}); }); describe('pagination', function() { var pageChangedEvent; - var g; + var deferred; beforeEach(function () { pageChangedEvent = spyOnEvent(document, Pixelated.events.ui.page.changed); - spyOn($, 'ajax').and.returnValue({done: function(f) { - g = f; - return {fail: function(){}}; - }}); + deferred = $.Deferred(); + var spyAjax = spyOn($, 'ajax').and.returnValue(deferred); spyOn(this.component, 'fetchMail').and.callThrough(); }); @@ -288,7 +305,7 @@ describeComponent('services/mail_service', function () { this.component.trigger(Pixelated.events.ui.mails.fetchByTag, {tag: 'another tag'}); - g(mailSetData); + deferred.resolve(mailSetData); expect(this.component.attr.numPages).toBe(5); }); @@ -299,7 +316,7 @@ describeComponent('services/mail_service', function () { this.component.trigger(Pixelated.events.ui.mails.fetchByTag, {tag: 'another tag'}); - g(mailSetData); + deferred.resolve(mailSetData); expect(this.component.attr.numPages).toBe(6); }); }); diff --git a/web-ui/test/spec/tags/data/tags.spec.js b/web-ui/test/spec/tags/data/tags.spec.js index e42a7752..adb279ac 100644 --- a/web-ui/test/spec/tags/data/tags.spec.js +++ b/web-ui/test/spec/tags/data/tags.spec.js @@ -8,7 +8,7 @@ describeComponent('tags/data/tags', function () { }); it('asks the server for tags when receiving the tags:want event', function() { - spyOn($, 'ajax').and.returnValue({done: function() {}}); + spyOn($, 'ajax').and.returnValue($.Deferred()); this.component.trigger(Pixelated.events.tags.want); @@ -16,25 +16,30 @@ describeComponent('tags/data/tags', function () { }); it('triggers an event on the initial sender after receiving tags', function() { - var f; - spyOn($, 'ajax').and.returnValue({done: function(d) { f = d; }}); + var deferred = $.Deferred(); + + spyOn($, 'ajax').and.returnValue(deferred); var me = {}; var eventSpy = spyOnEvent(me, Pixelated.events.tags.received); this.component.trigger(Pixelated.events.tags.want, { caller: me}); - f(['foo', 'bar', 'quux/bar']); + deferred.resolve(['foo', 'bar', 'quux/bar']); + expect(eventSpy).toHaveBeenTriggeredOn(me); }); it('triggers an event containing the returned tags', function() { - var f; - spyOn($, 'ajax').and.returnValue({done: function(d) { f = d; }}); + var tags = ['foo', 'bar', 'quux/bar']; + var deferred = $.Deferred(); + + spyOn($, 'ajax').and.returnValue(deferred); var me = {}; var eventSpy = spyOnEvent(me, Pixelated.events.tags.received); this.component.trigger(Pixelated.events.tags.want, { caller: me }); - var tags = ['foo', 'bar', 'quux/bar']; - f(tags); + + deferred.resolve(tags); + tags.push(this.component.all); expect(eventSpy.mostRecentCall.data).toEqual(jasmine.objectContaining({tags: tags})); }); -- cgit v1.2.3