summaryrefslogtreecommitdiff
path: root/web-ui/test/spec/mail_view/ui/recipients/recipients_input.spec.js
blob: 1656a83a8dadcbade62e78924b4407bd0d57ceb5 (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
describeComponent('mail_view/ui/recipients/recipients_input',function () {
  'use strict';

  beforeEach(function () {
    this.setupComponent({name: 'to'});
    spyOn($, 'getJSON').and.returnValue($.Deferred());
  });

  describe('keys that finish address input', function () {

    _.each([
      [186, 'semicolon'],
      [188, 'comma'],

    ], function (keycode) {

      it(': ' + keycode[1], function () {
        var addressEnteredEvent = spyOnEvent(this.$node, Pixelated.events.ui.recipients.entered);

        var enterAddressKeyPressEvent = $.Event('keydown', { which: keycode[0] });
        this.$node.val('a@b.c');
        this.$node.trigger(enterAddressKeyPressEvent);

        expect(addressEnteredEvent).toHaveBeenTriggeredOnAndWith(this, { name: 'to', address: 'a@b.c' });
      });

      it('wont add address if val is empty: ' + keycode[1], function () {
        var addressEnteredEvent = spyOnEvent(this.$node, Pixelated.events.ui.recipients.entered);

        var enterAddressKeyPressEvent = $.Event('keydown', { which: keycode[0] });
        this.$node.val('');
        this.$node.trigger(enterAddressKeyPressEvent);

        expect(addressEnteredEvent).not.toHaveBeenTriggeredOnAndWith(this, { name: 'to', address: '' });
      });

      it('wont add address if shift key is pressed together: ' + keycode[1], function () {
        var addressEnteredEvent = spyOnEvent(this.$node, Pixelated.events.ui.recipients.entered);

        var enterAddressKeyPressEvent = $.Event('keydown', { which: keycode[0], shiftKey: true });
        this.$node.val('a@b.c');
        this.$node.trigger(enterAddressKeyPressEvent);

        expect(addressEnteredEvent).not.toHaveBeenTriggeredOnAndWith(this, { name: 'to', address: 'a@b.c' });
      });

      it('prevents event default regardless on input val when key is ' + keycode[1], function () {
        var enterAddressKeyPressEvent = $.Event('keydown', { which: keycode[0] });
        spyOn(enterAddressKeyPressEvent, 'preventDefault');

        this.$node.val('');
        this.$node.trigger(enterAddressKeyPressEvent);
        expect(enterAddressKeyPressEvent.preventDefault).toHaveBeenCalled();

        enterAddressKeyPressEvent.preventDefault.calls.reset();
        this.$node.val('anything');
        this.$node.trigger(enterAddressKeyPressEvent);
        expect(enterAddressKeyPressEvent.preventDefault).toHaveBeenCalled();
      });

    });

    describe('when tab is pressed', function () {
      it('enters an address and prevents event default if there is an input val', function () {
        var addressEnteredEvent = spyOnEvent(this.$node, Pixelated.events.ui.recipients.entered);

        var tabKeyPressEvent = $.Event('keydown', { which: 9});
        spyOn(tabKeyPressEvent, 'preventDefault');

        this.$node.val('a@b.c');
        this.$node.trigger(tabKeyPressEvent);

        expect(tabKeyPressEvent.preventDefault).toHaveBeenCalled();
        expect(addressEnteredEvent).toHaveBeenTriggeredOnAndWith(this, { name: 'to', address: 'a@b.c'});
      });

      it('doesnt enter an address and doesnt prevent event default if input val is empty (so tab moves it to the next input)', function () {
        var addressEnteredEvent = spyOnEvent(this.$node, Pixelated.events.ui.recipients.entered);

        var tabKeyPressEvent = $.Event('keydown', { which: 9});
        spyOn(tabKeyPressEvent, 'preventDefault');

        this.$node.val('');
        this.$node.trigger(tabKeyPressEvent);

        expect(tabKeyPressEvent.preventDefault).not.toHaveBeenCalled();
        expect(addressEnteredEvent).not.toHaveBeenTriggeredOnAndWith(this, { name: 'to', address: ''});
      });
    });

    describe('when space is pressed', function () {
      it('should tokenize email address', function () {
        var addressEnteredEvent = spyOnEvent(this.$node, Pixelated.events.ui.recipients.entered);

        var spaceKeyPressEvent = $.Event('keydown', { which: 32});
        spyOn(spaceKeyPressEvent, 'preventDefault');

        this.$node.val('a@b.c');
        this.$node.trigger(spaceKeyPressEvent);

        expect(spaceKeyPressEvent.preventDefault).toHaveBeenCalled();
        expect(addressEnteredEvent).toHaveBeenTriggeredOnAndWith(this, { name: 'to', address: 'a@b.c' });
      });
    });
  });

  describe('on keyup', function () {
    it('triggers inputFieldIsEmpty if input is empty', function () {
      var inputFieldIsEmptyEvent = spyOnEvent(document, Pixelated.events.ui.recipients.inputFieldIsEmpty);
      this.$node.val('');

      this.$node.trigger('keyup');

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

    it('triggers inputFieldHasCharacters if input is not empty', function () {
      var inputFieldHasCharactersEvent = spyOnEvent(document, Pixelated.events.ui.recipients.inputFieldHasCharacters);
      this.$node.val('lalala');

      this.$node.trigger('keyup');

      expect(inputFieldHasCharactersEvent).toHaveBeenTriggeredOn(document, { name: 'to' });
    });
  });

  describe('on blur', function() {
    var addressEnteredEvent, blurEvent;

    beforeEach(function() {
      addressEnteredEvent = spyOnEvent(this.$node, Pixelated.events.ui.recipients.entered);
      blurEvent = $.Event('blur');
      spyOn(blurEvent, 'preventDefault');
    });

    it('tokenizes and sanitize recipient email if there is an input val', function() {
      this.$node.val('a@b.c, Friend <friend@domain.com>; d@e.f  , , , , , , , ,');
      this.$node.trigger(blurEvent);

      expect(blurEvent.preventDefault).toHaveBeenCalled();
      expect(addressEnteredEvent.callCount).toEqual(3);

      expect(addressEnteredEvent.calls[0].data).toEqual({name: 'to', address: 'a@b.c'});
      expect(addressEnteredEvent.calls[1].data).toEqual({name: 'to', address: 'Friend <friend@domain.com>'});
      expect(addressEnteredEvent.calls[2].data).toEqual({name: 'to', address: 'd@e.f'});
    });

    it('tokenizes and sanitize adresses separated by space, comma and semicolon', function() {
      this.$node.val('a@b.c Friend <friend@domain.com>, d@e.f; g@h.i');
      this.$node.trigger(blurEvent);

      expect(blurEvent.preventDefault).toHaveBeenCalled();
      expect(addressEnteredEvent.callCount).toEqual(4);

      expect(addressEnteredEvent.calls[0].data).toEqual({name: 'to', address: 'a@b.c'});
      expect(addressEnteredEvent.calls[1].data).toEqual({name: 'to', address: 'Friend <friend@domain.com>'});
      expect(addressEnteredEvent.calls[2].data).toEqual({name: 'to', address: 'd@e.f'});
      expect(addressEnteredEvent.calls[3].data).toEqual({name: 'to', address: 'g@h.i'});
    });
  });

  describe('when entering an invalid address', function() {
    var invalidAddressEnteredEvent, addressEnteredEvent, blurEvent;

    beforeEach(function () {
      invalidAddressEnteredEvent = spyOnEvent(this.$node, Pixelated.events.ui.recipients.enteredInvalid);
      addressEnteredEvent = spyOnEvent(this.$node, Pixelated.events.ui.recipients.entered);
      blurEvent = $.Event('blur');
      spyOn(blurEvent, 'preventDefault');
    });

    it('displays it as an invalid address token', function() {
      this.$node.val('invalid');
      this.$node.trigger(blurEvent);

      expect(blurEvent.preventDefault).toHaveBeenCalled();
      expect(invalidAddressEnteredEvent).toHaveBeenTriggeredOnAndWith(this, { name: 'to', address: 'invalid' });
    });

    it('displays it as an invalid address token when cannot parse', function() {
      this.$node.val('invalid_format email@example.com');
      this.$node.trigger(blurEvent);

      expect(blurEvent.preventDefault).toHaveBeenCalled();
      expect(invalidAddressEnteredEvent.calls[0].data).toEqual({name: 'to', address: 'invalid_format'});
      expect(addressEnteredEvent.calls[0].data).toEqual({name: 'to', address: 'email@example.com'});
    });

  });
});