summaryrefslogtreecommitdiff
path: root/web-ui/test/spec/mail_view/ui/send_button.spec.js
blob: 351b4a085aeff0b63bcc1fb1bcae0b76991b1af1 (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
/* global Pixelated */

describeComponent('mail_view/ui/send_button', function () {

  'use strict';

  describe('send button', function () {
    beforeEach(function () {
      this.setupComponent('<button></button>');
    });

    describe('when it is disabled', function () {
      beforeEach(function () {
        this.$node.prop('disabled', true);
      });

      it('gets enabled in a inputFieldHasCharacters event', function () {
        $(document).trigger(Pixelated.events.ui.recipients.inputFieldHasCharacters, { name: 'to' });

        expect(this.$node).not.toBeDisabled();
      });

      it('gets enabled in a recipients:updated where there are new recipients', function () {
        $(document).trigger(Pixelated.events.ui.recipients.updated, { newRecipients: ['a@b.c']});

        expect(this.$node).not.toBeDisabled();
      });

      it('gets enabled if recipients:updated also with invalid email', function () {
        $(document).trigger(Pixelated.events.ui.recipients.inputFieldHasCharacters, { name: 'to' });
        $(document).trigger(Pixelated.events.ui.recipients.updated, { newRecipients: ['InvalidEmail']});

        expect(this.$node).not.toBeDisabled();
        expect(this.$node.text()).toBe('Send');
      });
    });

    describe('multiple events', function () {
      it('gets enabled and remains enabled when a inputFieldHasCharacters is followed by a recipients:updated with NO new recipients', function () {
        this.$node.prop('disabled', true);

        $(document).trigger(Pixelated.events.ui.recipients.inputFieldHasCharacters, { name: 'to' });
        $(document).trigger(Pixelated.events.ui.recipients.updated, { newRecipients: [] });

        expect(this.$node).not.toBeDisabled();
      });

      it('gets enabled and remains enabled when a recipients:updated with recipients is followed by a inputFieldIsEmpty', function () {
        this.$node.prop('disabled', true);

        $(document).trigger(Pixelated.events.ui.recipients.updated, { newRecipients: ['a@b.c']});
        $(document).trigger(Pixelated.events.ui.recipients.inputFieldIsEmpty, { name: 'to' });

        expect(this.$node).not.toBeDisabled();
      });
    });

    describe('when it is enabled', function () {
      beforeEach(function () {
        this.$node.prop('disabled', false);
      });

      it('gets disabled in a inputFieldIsEmpty', function () {
        $(document).trigger(Pixelated.events.ui.recipients.inputFieldIsEmpty, { name: 'to' });

        expect(this.$node).toBeDisabled();
      });

      it('gets disabled in a recipients:updated without new recipients', function () {
        $(document).trigger(Pixelated.events.ui.recipients.updated, { newRecipients: []});

        expect(this.$node).toBeDisabled();
      });
    });

    describe('on click', function () {

      it ('asks for the recipients input to complete its current input', function () {
        var doCompleteInputEvent = spyOnEvent(document, Pixelated.events.ui.recipients.doCompleteInput);

        this.$node.click();

        expect(doCompleteInputEvent).toHaveBeenTriggeredOn(document);
      });

      it('disables the button after clicking', function () {
        expect(this.$node.text()).toBe('Send');

        this.$node.click();

        expect(this.$node.text()).toBe('Sending...');
        expect(this.$node.prop('disabled')).toBeTruthy();
      });

      it('enables again if sending errors out', function() {
        expect(this.$node.text()).toBe('Send');

        this.$node.click();

        $(document).trigger(Pixelated.events.mail.send_failed);

        expect(this.$node.text()).toBe('Send');
        expect(this.$node.prop('disabled')).not.toBeTruthy();

      });
    });

    describe('after clicking', function () {
      beforeEach(function () {
        this.$node.click();
      });

      it('waits for ui:mail:recipientsUpdated to happen 3 times in the mail then sends the mail then stops listening to ui:mail:recipientsUpdated', function () {
        var sendMailEvent = spyOnEvent(document, Pixelated.events.ui.mail.send);
        spyOn(this.component, 'off');

        _.times(3, function () { $(document).trigger(Pixelated.events.ui.mail.recipientsUpdated); });

        expect(sendMailEvent).toHaveBeenTriggeredOn(document);
        expect(this.component.off).toHaveBeenCalledWith(document, Pixelated.events.ui.mail.recipientsUpdated);
      });
    });
  });
});