summaryrefslogtreecommitdiff
path: root/web-ui/test/spec/mail_view/ui/recipients/recipients_iterator.spec.js
blob: 480ea2564167b3bac3afc0b2672bc88493a1a8c0 (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
/* global Smail */

define(['mail_view/ui/recipients/recipients_iterator'], function (RecipientsIterator) {
  'use strict';

  function createRecipient() {
    return jasmine.createSpyObj('recipient', ['select', 'unselect', 'destroy']);
  }

  var recipientsIterator,
    exitInput;

  function createIterator(elements) {
    return recipientsIterator = new RecipientsIterator({ elements: elements, exitInput: exitInput });
  }

  function resetMock(m) {
    m.destroy.reset();m.select.reset();m.unselect.reset();
  }

  beforeEach(function () {
    exitInput = $('<input>');
  });

  describe('moving left', function () {
    it('unselects the current element and selects the element in the left if there is one', function () {
      var elements = _.times(2, createRecipient);

      recipientsIterator = createIterator(elements);
      recipientsIterator.moveLeft();

      expect(elements[0].select).toHaveBeenCalled();
      expect(elements[1].unselect).toHaveBeenCalled();
    });

    it('doesnt do anything if there are no elements in the left', function () {
      var elements = _.times(2, createRecipient);
      recipientsIterator = createIterator(elements);
      recipientsIterator.moveLeft();
      _.each(elements, resetMock);

      recipientsIterator.moveLeft();

      expect(elements[0].select).not.toHaveBeenCalled();
      expect(elements[0].unselect).not.toHaveBeenCalled();
      expect(elements[1].select).not.toHaveBeenCalled();
      expect(elements[1].unselect).not.toHaveBeenCalled();
    });

  });

  describe('moving right', function () {
    it('unselects the current element and selects the one in the right if there is one', function () {
      var elements = _.times(2, createRecipient);
      recipientsIterator = createIterator(elements);
      recipientsIterator.moveLeft();
      _.each(elements, resetMock);

      recipientsIterator.moveRight();

      expect(elements[0].unselect).toHaveBeenCalled();
      expect(elements[1].select).toHaveBeenCalled();
    });

    it('unselects current element and focus on exit input if there are no elements on the right', function () {
      var elements = _.times(2, createRecipient);
      spyOn(exitInput, 'focus');

      recipientsIterator = createIterator(elements);
      recipientsIterator.moveRight();

      expect(elements[1].unselect).toHaveBeenCalled();
      expect(exitInput.focus).toHaveBeenCalled();
    });
  });

  describe('delete current', function () {
    it('selects what is left in the right after deleting the current element', function () {
      var elements = _.times(2, createRecipient);
      var toBeDeleted = elements[0];
      recipientsIterator = createIterator(elements);
      recipientsIterator.moveLeft();

      recipientsIterator.deleteCurrent();

      expect(toBeDeleted.destroy).toHaveBeenCalled();
      expect(elements[0].select).toHaveBeenCalled();
    });

    it('focus on the input if there are no more elements', function () {
      recipientsIterator = createIterator([createRecipient()]);
      spyOn(exitInput, 'focus');

      recipientsIterator.deleteCurrent();

      expect(exitInput.focus).toHaveBeenCalled();
    });
  });


});