summaryrefslogtreecommitdiff
path: root/web-ui/test/spec/mail_list/ui/mail_items/generic_mail_item.spec.js
blob: b49bc7f0c70bfc0ebef608f35b6dbc840e1b7080 (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
describeComponent('mail_list/ui/mail_items/generic_mail_item', function () {
  'use strict';

  var mail;

  beforeEach(function () {
    mail = Smail.testData().parsedMail.simpleTextPlain;
    mail.tags = ['inbox'];

    setupComponent('<li></li>', {
      mail: mail,
      selected: false,
      tag: 'inbox'
    });
  });

  it('should trigger ui:openMail on click', function () {
    var spyEvent = spyOnEvent(document, Smail.events.ui.mail.open);

    this.component.$node.find('a').click();

    expect(spyEvent).toHaveBeenTriggeredOn(document);
    expect(spyEvent.mostRecentCall.data).toEqual({ ident: mail.ident });
  });

  it('should add selected class when selecting', function () {
    this.$node.find('a').click();

    expect(this.$node).toHaveClass('selected');
  });

  it('should remove selected class when selecting a different mail', function () {
    $(document).trigger(Smail.events.ui.mail.updateSelected, { ident: 2 });

    expect(this.$node).not.toHaveClass('selected');
  });

  it('should remove selected class when enabling compose box', function () {
    this.$node.find('a').click();

    $(document).trigger(Smail.events.ui.composeBox.newMessage);

    expect(this.$node).not.toHaveClass('selected');
  });

  it('should have the href link with mail ident and tag name', function () {
    expect(this.$node.find('a')[0].href).toMatch('inbox/mail/' + mail.ident);
  });

  describe('clicking on a mail', function () {

    function createClickEvent(options) {
      var clickEvent = $.Event('click');
      _.merge(clickEvent, options);
      spyOn(clickEvent, 'preventDefault');
      return clickEvent;
    }

    it('triggers mail open and pushes the state', function () {
      var clickEvent = createClickEvent();
      var mailOpenEvent = spyOnEvent(document, Smail.events.ui.mail.open);
      var pushStateEvent = spyOnEvent(document, Smail.events.router.pushState);

      $(this.$node.find('a')).trigger(clickEvent);

      expect(mailOpenEvent).toHaveBeenTriggeredOnAndWith(document, { ident: mail.ident });
      expect(pushStateEvent).toHaveBeenTriggeredOnAndWith(document, { mailIdent: mail.ident });
      expect(clickEvent.preventDefault).toHaveBeenCalled();
    });

    describe('when opening on a new tab', function () {

      _.each([
        {metaKey: true},
        {which: 2},
        {ctrlKey: true}
      ], function (specialKey) {
        it('doesnt trigger mail open and nor pushes the state', function () {
          var clickEvent = createClickEvent(specialKey);
          var mailOpenEvent = spyOnEvent(document, Smail.events.ui.mail.open);
          var pushStateEvent = spyOnEvent(document, Smail.events.router.pushState);

          $(this.$node.find('a')).trigger(clickEvent);

          expect(mailOpenEvent).not.toHaveBeenTriggeredOnAndWith(document, { ident: mail.ident });
          expect(pushStateEvent).not.toHaveBeenTriggeredOnAndWith(document, { mailIdent: mail.ident });
          expect(clickEvent.preventDefault).not.toHaveBeenCalled();
        });

        it('marks the email as read', function () {
          debugger;
          var mailReadEvent = spyOnEvent(document, Smail.events.mail.read);
          var clickEvent = createClickEvent(specialKey);

          $(this.$node.find('a')).trigger(clickEvent);

          expect(this.component.attr.mail.status).toContain(this.component.status.READ);
          expect(this.$node.attr('class')).toMatch('status-read');
          expect(mailReadEvent).toHaveBeenTriggeredOnAndWith(document, { ident: mail.ident, tags: ['inbox'] });
        });

      });

    });

  });

  describe('marking emails as read', function () {
    it('should trigger mail:read event when unread is clicked', function () {
      var mailReadEvent = spyOnEvent(document, Smail.events.mail.read);

      this.$node.find('a').click();

      expect(mailReadEvent).toHaveBeenTriggeredOnAndWith(document, jasmine.objectContaining({ident: mail.ident}));
    });

    it('should not trigger mail:read event when clicking mail that is already read', function () {
      var mailReadEvent = spyOnEvent(document, Smail.events.mail.read);
      this.component.attr.mail.status.push(this.component.status.READ);

      this.$node.find('a').click();

      expect(mailReadEvent).not.toHaveBeenTriggeredOnAndWith(document, {ident: mail.ident});
    });

    it('should add status-read class to email when clicking an unread email', function () {
      this.$node.find('a').click();

      expect(this.$node).toHaveClass('status-read');
    });

    it('should not have status-read class when initializing email without read status', function () {
      expect(this.$node).not.toHaveClass('status-read');
    });
  });
});