diff options
author | Ola Bini <ola.bini@gmail.com> | 2014-07-31 19:29:33 -0300 |
---|---|---|
committer | Ola Bini <ola.bini@gmail.com> | 2014-07-31 19:29:33 -0300 |
commit | 04cf441c5ae18400c6b4865b0b37a71718dc9d46 (patch) | |
tree | dd0b0d049ec00389e2d4561b226c46eb1682b997 /web-ui/test/spec/services | |
parent | 639a663a4c37020003586438fdcd7ac529a00f10 (diff) |
Add web-ui based on previous code
Diffstat (limited to 'web-ui/test/spec/services')
-rw-r--r-- | web-ui/test/spec/services/delete_service.spec.js | 54 | ||||
-rw-r--r-- | web-ui/test/spec/services/mail_service.spec.js | 307 | ||||
-rw-r--r-- | web-ui/test/spec/services/model/mail.spec.js | 116 |
3 files changed, 477 insertions, 0 deletions
diff --git a/web-ui/test/spec/services/delete_service.spec.js b/web-ui/test/spec/services/delete_service.spec.js new file mode 100644 index 00000000..3e098877 --- /dev/null +++ b/web-ui/test/spec/services/delete_service.spec.js @@ -0,0 +1,54 @@ +/*global jasmine */ +/*global Smail */ + +describeComponent('services/delete_service', function () { + 'use strict'; + + var i18n; + + beforeEach( function () { + setupComponent(); + i18n = require('views/i18n'); + }); + + var mailWithoutTrashTag = { + ident: 42, + isInTrash: function() { return false; }, + tags: ['inbox', 'test'] + }; + + var mailWithTrashTag = { + ident: 34, + isInTrash: function() { return true; }, + tags: ['inbox', 'test', 'trash'] + }; + + it('add Trash tag when deleting an email that does not have it', function () { + var mailDeleteEvent = spyOnEvent(document, Smail.events.mail.delete); + var openNoMessageSelectedEvent = spyOnEvent(document, Smail.events.dispatchers.rightPane.openNoMessageSelected); + + this.component.trigger(document, Smail.events.ui.mail.delete, {mail: mailWithoutTrashTag}); + + var expectedDeleteEventData = { + mail: mailWithoutTrashTag, + successMessage: i18n('Your message was moved to trash!') + }; + + expect(mailDeleteEvent).toHaveBeenTriggeredOnAndWith(document, expectedDeleteEventData); + }); + + it('removes permanently email that has Trash tag', function(){ + var mailDeleteEvent = spyOnEvent(document, Smail.events.mail.delete); + var openNoMessageSelectedEvent = spyOnEvent(document, Smail.events.dispatchers.rightPane.openNoMessageSelected); + + this.component.trigger(document, Smail.events.ui.mail.delete, {mail: mailWithTrashTag}); + + var expectedDeleteEventData = { + mail: mailWithTrashTag, + successMessage: i18n('Your message was permanently deleted!') + }; + + expect(mailDeleteEvent).toHaveBeenTriggeredOnAndWith(document, expectedDeleteEventData ); + }); + +}); diff --git a/web-ui/test/spec/services/mail_service.spec.js b/web-ui/test/spec/services/mail_service.spec.js new file mode 100644 index 00000000..31e130fa --- /dev/null +++ b/web-ui/test/spec/services/mail_service.spec.js @@ -0,0 +1,307 @@ +/*global jasmine */ +/*global Smail */ +'use strict'; + +describeComponent('services/mail_service', function () { + + var email1, i18n; + + beforeEach( function () { + setupComponent(); + email1 = Smail.testData().parsedMail.simpleTextPlain; + i18n = require('views/i18n'); + } ); + + it('marks the desired message as read', function () { + var readRequest = spyOn($, 'ajax').andReturn({}); + + this.component.trigger(Smail.events.mail.read, {ident: 1}); + + expect(readRequest.mostRecentCall.args[0]).toEqual('/mail/1/read'); + }); + + describe('when marks many emails as read', function () { + var readRequest, checkedMails, uncheckedEmailsEvent, setCheckAllEvent, doneMarkAsRead; + + beforeEach(function () { + readRequest = spyOn($, 'ajax').andReturn({done: function(f) { doneMarkAsRead = f; return {fail: function() {}};}}); + uncheckedEmailsEvent = spyOnEvent(document, Smail.events.ui.mail.unchecked); + setCheckAllEvent = spyOnEvent(document, Smail.events.ui.mails.hasMailsChecked); + spyOn(this.component, 'refreshResults'); + + checkedMails = { + 1: {ident: 1}, + 2: {ident: 2} + }; + + this.component.trigger(Smail.events.mail.read, {checkedMails: checkedMails}); + }); + + it('makes the correct request to the backend', function () { + expect(readRequest.mostRecentCall.args[0]).toEqual('/mails/read'); + expect(readRequest.mostRecentCall.args[1].data).toEqual({idents: '[1,2]'}); + }); + + it('will trigger that a message has been deleted when it is done deleting', function() { + doneMarkAsRead({mails: checkedMails}); + expect(this.component.refreshResults).toHaveBeenCalled(); + }); + + it('unchecks read emails', function () { + doneMarkAsRead({mails: checkedMails}); + expect(uncheckedEmailsEvent).toHaveBeenTriggeredOnAndWith(document, {mails: checkedMails}); + }); + + it('clears the check all checkbox', function () { + doneMarkAsRead({mails: checkedMails}); + expect(setCheckAllEvent).toHaveBeenTriggeredOnAndWith(document, false); + }); + }); + + it('fetches a single email', function () { + var me = {}; + var spyAjax = spyOn($, 'ajax').andReturn({done: function(f) { f(email1); return {fail: function() {}};}}); + var mailHereEvent = spyOnEvent(me, Smail.events.mail.here); + + this.component.trigger(Smail.events.mail.want, { caller: me, mail: email1.ident }); + + expect(mailHereEvent).toHaveBeenTriggeredOn(me); + expect(spyAjax.mostRecentCall.args[0]).toEqual('/mail/' + email1.ident); + }); + + it('answers mail:notFound if mail returned from server is null', function () { + var me = {}; + var spyAjax = spyOn($, 'ajax').andReturn({done: function(f) { f(null); return {fail: function() {}};}}); + var mailNotFound = spyOnEvent(me, Smail.events.mail.notFound); + + this.component.trigger(Smail.events.mail.want, { caller: me, mail: email1.ident }); + + expect(mailNotFound).toHaveBeenTriggeredOn(me); + }); + + it('updates the tags of the desired message', function () { + spyOn(this.component, 'refreshResults'); + var spyAjax = spyOn($, 'ajax').andReturn({done: function(f) { f(); return {fail: function() {}};}}); + + var spyEvent = spyOnEvent(document, Smail.events.mail.tags.updated); + var component = jasmine.createSpyObj('component',['successUpdateTags']); + spyOn(this.component, 'fetchMail'); + + this.component.trigger(Smail.events.mail.tags.update, { ident: email1.ident, tags: email1.tags }); + + expect(spyEvent).toHaveBeenTriggeredOn(document); + expect(spyAjax.calls[0].args[0]).toEqual('/mail/1/tags'); + expect(spyAjax.calls[0].args[1].data).toEqual(JSON.stringify({ newtags: email1.tags } )); + expect(this.component.refreshResults).toHaveBeenCalled(); + }); + + it('triggers an error message when it can\'t update the tags', function () { + var spyAjax = spyOn($, 'ajax').andReturn({done: function() { return {fail: function(f) {f();}};}}); + + var spyEvent = spyOnEvent(document, Smail.events.ui.userAlerts.displayMessage); + var component = jasmine.createSpyObj('component',['failureUpdateTags']); + + this.component.trigger(Smail.events.mail.tags.update, { ident: email1.ident, tags: email1.tags }); + + expect(spyEvent).toHaveBeenTriggeredOn(document); + expect(spyAjax.mostRecentCall.args[0]).toEqual('/mail/1/tags'); + expect(spyAjax.mostRecentCall.args[1].data).toEqual(JSON.stringify({ newtags: email1.tags } )); + }); + + it('will try to delete a message when requested to', function() { + var spyAjax = spyOn($, 'ajax').andReturn({done: function() { return {fail: function(f) {}};}}); + this.component.trigger(Smail.events.mail.delete, {mail: {ident: '43'}}); + expect(spyAjax).toHaveBeenCalled(); + expect(spyAjax.mostRecentCall.args[0]).toEqual('/mail/43'); + expect(spyAjax.mostRecentCall.args[1].type).toEqual('DELETE'); + }); + + describe('when successfuly deletes an email', function () { + var displayMessageEvent, uncheckedEmailsEvent, setCheckAllEvent, mailsDeletedEvent; + + beforeEach(function () { + displayMessageEvent = spyOnEvent(document, Smail.events.ui.userAlerts.displayMessage); + uncheckedEmailsEvent = spyOnEvent(document, Smail.events.ui.mail.unchecked); + setCheckAllEvent = spyOnEvent(document, Smail.events.ui.mails.hasMailsChecked); + mailsDeletedEvent = spyOnEvent(document, Smail.events.mail.deleted); + spyOn(this.component, 'refreshResults'); + + this.component.triggerDeleted({ + successMessage: 'A success message', + mails: {1: 'email 1', 2: 'email 2'} + })(); + }); + + it('will trigger that a message has been deleted when it is done deleting', function() { + expect(this.component.refreshResults).toHaveBeenCalled(); + }); + + it('displays a success message', function () { + expect(displayMessageEvent).toHaveBeenTriggeredOnAndWith(document, {message: 'A success message'}); + }); + + it('unchecks deleted emails', function () { + expect(uncheckedEmailsEvent).toHaveBeenTriggeredOnAndWith(document, { mails: {1: 'email 1', 2: 'email 2'} }); + }); + + it('tells about deleted emails', function () { + expect(mailsDeletedEvent).toHaveBeenTriggeredOnAndWith(document, { mails: {1: 'email 1', 2: 'email 2'} }); + }); + + it('clears the check all checkbox', function () { + expect(setCheckAllEvent).toHaveBeenTriggeredOnAndWith(document, false); + }); + }); + + it('will trigger an error message when a message cannot be deleted', function() { + spyOn($, 'ajax').andReturn({done: function() { return {fail: function(f) { f(); }};}}); + var spyEvent = spyOnEvent(document, Smail.events.ui.userAlerts.displayMessage); + + this.component.trigger(Smail.events.mail.delete, {mail: {ident: '43'}}); + + 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, Smail.events.mails.available); + + spyOn($, 'ajax').andReturn({done: function(f) { g = f; return {fail: function(){}};}}); + this.component.trigger(Smail.events.ui.mails.fetchByTag, {tag: 'inbox'}); + + g({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').andReturn({done: function(f) { return {fail: function(){}};}}); + + this.component.trigger(Smail.events.ui.mails.fetchByTag, {tag: 'new tag'}); + + expect($.ajax.mostRecentCall.args[0]).toContain(encodeURI('tag:"new tag"')); + }); + + 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, Smail.events.mails.availableForRefresh); + this.component.attr.currentTag = 'sent'; + + spyOn($, 'ajax').andReturn({done: function(f) { g = f; return {fail: function(){}};}}); + this.component.trigger(Smail.events.ui.mails.refresh); + + g({stats: {}, mails: [email1]}); + expect(eventSpy.mostRecentCall.data.tag).toEqual('sent'); + expect(eventSpy.mostRecentCall.data.stats).toEqual({}); + }); + + describe('pagination', function() { + var pageChangedEvent; + var g; + + beforeEach(function () { + pageChangedEvent = spyOnEvent(document, Smail.events.ui.page.changed); + spyOn($, 'ajax').andReturn({done: function(f) { + g = f; + return {fail: function(){}}; + }}); + spyOn(this.component, 'fetchMail').andCallThrough(); + }); + + it('changes to the previous page and refetch email when ui:page:previous is fired', function() { + this.component.attr.currentPage = 1; + + this.component.trigger(Smail.events.ui.page.previous); + + expect(this.component.fetchMail).toHaveBeenCalled(); + expect(this.component.attr.currentPage).toEqual(0); + }); + + it('won\'t change the page if it was already at the first page and trying to go to previous', function() { + this.component.attr.currentPage = 0; + + this.component.trigger(Smail.events.ui.page.previous); + + expect(this.component.fetchMail).not.toHaveBeenCalled(); + expect(this.component.attr.currentPage).toEqual(0); + }); + + it('changes to the next page and refetch email when ui:page:next is fired', function() { + this.component.attr.numPages = 10; + this.component.attr.currentPage = 1; + + this.component.trigger(Smail.events.ui.page.next); + + expect(this.component.fetchMail).toHaveBeenCalled(); + expect(this.component.attr.currentPage).toEqual(2); + }); + + it('won\'t change the page if it was already at the first page and trying to go to previous', function() { + this.component.attr.numPages = 10; + this.component.attr.currentPage = 9; + + this.component.trigger(Smail.events.ui.page.next); + + expect(this.component.fetchMail).not.toHaveBeenCalled(); + expect(this.component.attr.currentPage).toEqual(9); + }); + + + it('triggers pageChanged event when going to next page', function() { + this.component.attr.numPages = 10; + this.component.trigger(Smail.events.ui.page.next); + + expect(pageChangedEvent).toHaveBeenTriggeredOnAndWith(document, {currentPage: 1, numPages: 10}); + }); + + it('triggers pageChanged event when going to previous page', function() { + this.component.attr.numPages = 10; + this.component.attr.currentPage = 1; + this.component.trigger(Smail.events.ui.page.previous); + + expect(pageChangedEvent).toHaveBeenTriggeredOnAndWith(document, {currentPage: 0, numPages: 10}); + }); + + it('resets currentPage when fetching mails by tag', function() { + this.component.attr.numPages = 10; + this.component.attr.currentPage = 999; + this.component.trigger(Smail.events.ui.mails.fetchByTag, {tag: 'inbox'}); + + expect(this.component.attr.currentPage).toEqual(0); + expect(pageChangedEvent).toHaveBeenTriggeredOnAndWith(document, {currentPage: 0, numPages: 10}); + }); + + describe('total page numbers', function() { + var mailSetData = { + tag: 'inbox', + stats: { }, + mails: [], + timing: {} + }; + + it('should have 5 pages with a 100 results and w 20', function() { + mailSetData.stats.total = 100; + this.component.attr.w = 20; + this.component.attr.numPages = 0; + + this.component.trigger(Smail.events.ui.mails.fetchByTag, {tag: 'another tag'}); + + g(mailSetData); + expect(this.component.attr.numPages).toBe(5); + }); + + it('should have 6 pages with a 101 results and w 20', function() { + mailSetData.stats.total = 101; + this.component.attr.w = 20; + this.component.attr.numPages = 0; + + this.component.trigger(Smail.events.ui.mails.fetchByTag, {tag: 'another tag'}); + + g(mailSetData); + expect(this.component.attr.numPages).toBe(6); + }); + }); + + }); +}); diff --git a/web-ui/test/spec/services/model/mail.spec.js b/web-ui/test/spec/services/model/mail.spec.js new file mode 100644 index 00000000..5bdad88b --- /dev/null +++ b/web-ui/test/spec/services/model/mail.spec.js @@ -0,0 +1,116 @@ +/*global Smail */ + +require(['services/model/mail'], function (Mail) { + var testData; + + describe('services/model/mail', function () { + describe('reply addresses', function () { + it('returns the "to" and "cc" addresses if the mail was sent', function () { + var mail = Mail.create({ + header: { to: ['a@b.c', 'e@f.g'], cc: ['x@x.x'] }, + tags: ['sent'] + }); + + var addresses = mail.replyToAddress(); + + expect(addresses).toEqual({ to: ['a@b.c', 'e@f.g'], cc: ['x@x.x']}); + }); + }); + + describe('parsing', function () { + describe('a single email', function () { + var sentMail, draftMail, recievedMail, recievedMailWithCC; + beforeEach(function () { + sentMail = Mail.create(Smail.testData().rawMail.sent); + draftMail = Mail.create(Smail.testData().rawMail.draft); + recievedMail = Mail.create(Smail.testData().rawMail.recieved); + recievedMailWithCC = Mail.create(Smail.testData().rawMail.recievedWithCC); + }); + + it('correctly identifies a sent mail', function () { + expect(sentMail.isSentMail()).toBe(true); + }); + + it('correctly identifies a draft mail', function () { + expect(draftMail.isDraftMail()).toBe(true); + }); + + it('correctly identifies a recieved mail', function () { + expect(recievedMail.isSentMail()).toBe(false); + expect(recievedMail.isDraftMail()).toBe(false); + }); + + it('reply to of a sent mail should be original recipient', function () { + expect(sentMail.replyToAddress()).toEqual({to: ['mariane_dach@davis.info'], cc: ['duda@la.lu']}); + }); + + it('reply to of a mail should be the reply_to field if existent', function () { + expect(recievedMail.replyToAddress()).toEqual({to: ['afton_braun@botsford.biz'], cc: [] }); + }); + + it('reply to of a mail should be the from field if no reply_to present', function () { + expect(recievedMailWithCC.replyToAddress()).toEqual({to: ['cleve_jaskolski@schimmelhirthe.net'], cc: []}); + }); + + it('reply to all should include all email addresses in the header', function () { + expect(recievedMailWithCC.replyToAllAddress()).toEqual({ + to: ['cleve_jaskolski@schimmelhirthe.net', 'stanford@sipes.com'], + cc: ['mariane_dach@davis.info'] + }); + }); + }); + + describe('multipart email', function () { + var parsedMultipartMail; + + beforeEach(function () { + parsedMultipartMail = Mail.create(Smail.testData().rawMail.multipart); + }); + + it('parses the mail as multipart/alternative', function () { + expect(parsedMultipartMail.isMailMultipartAlternative()).toBe(true); + }); + + it('lists the correct available content-type of the parts', function () { + expect(parsedMultipartMail.availableBodyPartsContentType()).toEqual(['text/plain;', 'text/html;']); + }); + + it('gets the list of parts', function () { + var expectedParts = [ + { + headers: { 'Content-Type': 'text/plain;' }, + body: 'Hello everyone!\n' + }, + { + headers: { + 'Content-Type': 'text/html;', + 'Content-Transfer-Encoding': 'quoted-printable' + }, + body: '<p><b>Hello everyone!</b></p>\n' + } + ]; + + expect(parsedMultipartMail.getMailMultiParts()).toEqual(expectedParts); + }); + + it('gets the text/plain body by the content-type', function () { + expect(parsedMultipartMail.getMailPartByContentType('text/plain;')).toEqual( + { + headers: { 'Content-Type': 'text/plain;' }, + body: 'Hello everyone!\n' + }); + }); + + it('parses the content type of a text/html body', function () { + expect(parsedMultipartMail.getMailPartByContentType('text/html;')).toEqual({ + headers: { + 'Content-Type': 'text/html;', + 'Content-Transfer-Encoding': 'quoted-printable' + }, + body: '<p><b>Hello everyone!</b></p>\n' + }); + }); + }); + }); + }); +}); |