summaryrefslogtreecommitdiff
path: root/web-ui/test/spec/mail_list/ui/mail_list.spec.js
blob: f383d5408ca4cb8473ca53e2557e72784b27c5a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*global Smail */

describeComponent('mail_list/ui/mail_list', function () {
  'use strict';

  var mailList;

  beforeEach(function () {
    setupComponent('<div id="mails"></div>', {
      urlParams: {
        hasMailIdent: function () {
          return false;
        }
      }
    });
    mailList =
      [
        createMail('the mail subject', 'from@mail.com', '1', '2012-12-26T01:38:46-08:00'),
        createMail('another mail subject', 'from_another@mail.com', '2', '2012-12-28T01:38:46-08:00')
      ];
  });


  it('should open mail at first mail:available if there is a mailIdent in the url hash', function () {
    this.component.attr.urlParams = {
      hasMailIdent: function () {
        return true;
      },
      getMailIdent: function () {
        return '10';
      }
    };
    var openMailEvent = spyOnEvent(document, Smail.events.ui.mail.open);

    this.$node.trigger(Smail.events.mails.available, { mails: mailList });
    expect(openMailEvent).toHaveBeenTriggeredOnAndWith(document, { ident: '10' });

    this.$node.trigger(Smail.events.mails.available, { mails: mailList });
    expect(openMailEvent.calls.length).toEqual(1);
  });

  it('should push the state if there is a mail ident in the hash url', function () {
    this.component.attr.urlParams = {
      hasMailIdent: function () {
        return true;
      },
      getMailIdent: function () {
        return '10';
      }
    };
    var pushState = spyOnEvent(document, Smail.events.router.pushState);
    this.component.attr.currentTag = 'inbox';

    this.$node.trigger(Smail.events.mails.available, { mails: mailList });

    expect(pushState).toHaveBeenTriggeredOnAndWith(document, { tag: 'inbox', mailIdent: '10' });
  });

  describe('checking/unchecking mails in the list', function () {

    it('keeps a list with the currently checked mails', function () {
      var checkedMails = {};

      this.component.attr.checkedMails = {};

      $(document).trigger(Smail.events.ui.mail.checked, {mail: mailList[0]});

      checkedMails[mailList[0].ident] = mailList[0];

      expect(this.component.attr.checkedMails).toEqual(checkedMails);
    });

    it('returns the list of checked mails to whomever requests them', function () {
      var caller = {};
      this.component.attr.checkedMails = {'1': {}};
      var mailHereCheckedEvent = spyOnEvent(caller, Smail.events.ui.mail.hereChecked);

      $(document).trigger(Smail.events.ui.mail.wantChecked, caller);

      expect(mailHereCheckedEvent).toHaveBeenTriggeredOnAndWith(caller, { checkedMails: {'1': {} }});
    });

    it('returns an empty list to whomever requests the checked mails if there are no checked mails', function () {
      var caller = {};
      var mailHereCheckedEvent = spyOnEvent(caller, Smail.events.ui.mail.hereChecked);

      $(document).trigger(Smail.events.ui.mail.wantChecked, caller);

      expect(mailHereCheckedEvent).toHaveBeenTriggeredOnAndWith(caller, { checkedMails: {} });
    });

    it('removes for the checked mails when mail is unchecked', function () {
      this.component.attr.checkedMails = {
        '1': {},
        '2': {},
        '3': {}
      };

      $(document).trigger(Smail.events.ui.mail.unchecked, {mail: {ident: '1'}});

      expect(this.component.attr.checkedMails).toEqual({'2': {}, '3': {} });
    });

    it('checks the check all checkbox if at least one mail is checked', function () {
      var setCheckAllCheckboxEvent = spyOnEvent(document, Smail.events.ui.mails.hasMailsChecked);

      $(document).trigger(Smail.events.ui.mail.checked, {mail: mailList[0]});

      expect(setCheckAllCheckboxEvent).toHaveBeenTriggeredOnAndWith(document, true);
    });

    it('unchecks the check all checkbox if no mail is left checked', function () {
      this.component.attr.checkedMails = {1: {}};

      var setCheckAllCheckboxEvent = spyOnEvent(document, Smail.events.ui.mails.hasMailsChecked);

      $(document).trigger(Smail.events.ui.mail.unchecked, {mail: {ident: '1'}});

      expect(setCheckAllCheckboxEvent).toHaveBeenTriggeredOnAndWith(document, false);
    });
  });

  describe('when mails are available', function () {
    it('should open email if popstate event happened (when mailIdent isnt undefined)', function () {
      var openMailEvent = spyOnEvent(document, Smail.events.ui.mail.open);

      this.component.$node.trigger(Smail.events.mails.available, { mails: mailList, mailIdent: '30' });

      expect(openMailEvent).toHaveBeenTriggeredOnAndWith(document, { ident: '30'});
    });

    it('should open draft in popstate event if tag is Drafts', function () {
      var openDraftEvent = spyOnEvent(document, Smail.events.dispatchers.rightPane.openDraft);

      this.component.$node.trigger(Smail.events.mails.available, { mails: mailList, mailIdent: '30', tag: 'drafts' });

      expect(openDraftEvent).toHaveBeenTriggeredOnAndWith(document, { ident: '30'});
    });
  });

  it('should not append emails when another mails:available event is triggered', function () {
    this.component.$node.trigger(Smail.events.mails.available, { mails: mailList });

    expect(this.component.$node.find('a').length).toEqual(2);

    this.component.$node.trigger(Smail.events.mails.available, { mails: mailList });

    expect(this.component.$node.find('a').length).toEqual(2);
  });

  it('resets scroll when opening a new tag or choosing a new tag', function () {
    var eventSpy = spyOnEvent(document, Smail.events.dispatchers.middlePane.resetScroll);
    this.component.$node.trigger(Smail.events.mails.available, { mails: mailList });
    expect(eventSpy).toHaveBeenTriggeredOn(document);
  });

  describe('rendering the mails', function () {

    describe('when mails are available for refreshing', function () {
      it('renders the new mails', function () {
        this.component.$node.trigger(Smail.events.mails.availableForRefresh, { mails: mailList });

        matchMail(mailList[0], this.component.$node);
        matchMail(mailList[1], this.component.$node);
      });

    });

    it('should render all mails sent in ui:mails:show event', function () {
      this.component.$node.trigger(Smail.events.mails.available, { mails: mailList });

      matchMail(mailList[0], this.component.$node);
      matchMail(mailList[1], this.component.$node);
    });

    it('should select the current email when mails are available', function () {
      this.component.attr.currentMailIdent = '1';

      this.component.trigger(Smail.events.mails.available, { mails: mailList });

      matchSelectedMail(mailList[0], this.component.$node);
      matchMail(mailList[1], this.component.$node);
    });

    it('should keep the mail checked when it was previously checked (so refresh works)', function () {
      var checkbox, mailIdent;

      mailIdent = mailList[0].ident;
      this.component.attr.checkedMails[mailIdent] = mailList[0];
      this.component.$node.trigger(Smail.events.mails.available, { mails: [mailList[0]] });
      checkbox = this.$node.find('input[type=checkbox]');

      expect(checkbox.prop('checked')).toBe(true);
    });

    it('should render links for the emails', function () {
      this.component.$node.trigger(Smail.events.mails.available, { mails: mailList, tag: 'inbox' });

      expect(this.$node.html()).toMatch('href="/#/inbox/mail/1');
      expect(this.$node.html()).toMatch('href="/#/inbox/mail/2');
    });

    it('should clean the selected email', function () {
      this.component.attr.currentMailIdent = '1';
      this.component.trigger(Smail.events.ui.mails.cleanSelected);

      expect(this.component.attr.currentMailIdent).toEqual('');
    });

    function matchMail(mail, node) {
      expect(node.html()).toMatch('id="mail-' + mail.ident + '"');
      expect(node.html()).toMatch('<div class="subject-and-tags">');
      expect(node.html()).toMatch('<div class="from">' + mail.header.from + '</div>');
      expect(node.html()).toMatch('<span class="received-date">' + mail.header.formattedDate + '</span>');
    }

    function matchSelectedMail(mail, node) {
      expect(node.html()).toMatch(['id="mail-', mail.ident, '" class="selected"'].join(''));
    }
  });

  describe('when saving a draft', function () {
    it('refreshes the list if the current tag is drafts', function () {
      this.component.attr.currentTag = 'drafts';
      var spyRefresh = spyOnEvent(document, Smail.events.ui.mails.refresh);
      var spyScroll = spyOnEvent(document, Smail.events.dispatchers.middlePane.resetScroll);
      this.component.trigger(Smail.events.mail.draftSaved, {ident: 1});
      expect(spyRefresh).toHaveBeenTriggeredOn(document);
      expect(spyScroll).toHaveBeenTriggeredOn(document);
    });

    it('does not refresh the list if the current tag is not drafts', function() {
      this.component.attr.currentTag = 'sent';
      var spyRefresh = spyOnEvent(document, Smail.events.ui.mails.refresh);
      var spyScroll = spyOnEvent(document, Smail.events.dispatchers.middlePane.resetScroll);
      this.component.trigger(Smail.events.mail.draftSaved, {ident: 1});
      expect(spyRefresh).not.toHaveBeenTriggeredOn(document);
      expect(spyScroll).not.toHaveBeenTriggeredOn(document);
    });
  });

  describe('when sending a mail', function () {
    it('refreshes the list if the current tag is drafts', function () {
      this.component.attr.currentTag = 'drafts';
      var spyRefresh = spyOnEvent(document, Smail.events.ui.mails.refresh);
      var spyScroll = spyOnEvent(document, Smail.events.dispatchers.middlePane.resetScroll);
      this.component.trigger(Smail.events.mail.sent);
      expect(spyRefresh).toHaveBeenTriggeredOn(document);
      expect(spyScroll).toHaveBeenTriggeredOn(document);
    });

    it('refreshes the list if the current tag is sent', function() {
      this.component.attr.currentTag = 'sent';
      var spyRefresh = spyOnEvent(document, Smail.events.ui.mails.refresh);
      var spyScroll = spyOnEvent(document, Smail.events.dispatchers.middlePane.resetScroll);
      this.component.trigger(Smail.events.mail.sent);
      expect(spyRefresh).toHaveBeenTriggeredOn(document);
      expect(spyScroll).toHaveBeenTriggeredOn(document);
    });
  });

  function createMail(subject, from, ident, date) {
    var mail = Smail.testData().parsedMail.simpleTextPlain;

    return _.merge(mail, {
      header: {
        subject: subject,
        from: from,
        date: date
      },
      ident: ident,
      tags: ['inbox']
    });
  }
});