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
|
define(['mail_view/ui/recipients/recipients_iterator'], function (RecipientsIterator) {
'use strict';
function createRecipient() {
return jasmine.createSpyObj('recipient', ['doSelect', 'doUnselect', 'destroy']);
}
var recipientsIterator,
exitInput;
function createIterator(elements) {
return new RecipientsIterator({ elements: elements, exitInput: exitInput });
}
function resetMock(m) {
m.destroy.calls.reset();m.doSelect.calls.reset();m.doUnselect.calls.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].doSelect).toHaveBeenCalled();
expect(elements[1].doUnselect).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].doSelect).not.toHaveBeenCalled();
expect(elements[0].doUnselect).not.toHaveBeenCalled();
expect(elements[1].doSelect).not.toHaveBeenCalled();
expect(elements[1].doUnselect).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].doUnselect).toHaveBeenCalled();
expect(elements[1].doSelect).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].doUnselect).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].doSelect).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();
});
});
});
|