forked from nylas/nylas-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel-view-selection-spec.coffee
195 lines (155 loc) · 6.8 KB
/
model-view-selection-spec.coffee
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
191
192
193
194
195
_ = require 'underscore'
Thread = require '../src/flux/models/thread'
ModelView = require '../src/flux/stores/model-view'
ModelViewSelection = require '../src/flux/stores/model-view-selection'
describe "ModelViewSelection", ->
beforeEach ->
@trigger = jasmine.createSpy('trigger')
@items = []
@items.push(new Thread(id: "#{ii}")) for ii in [0..99]
@view = new ModelView()
@view._pages =
"0":
items: @items
loaded: true
@selection = new ModelViewSelection(@view, @trigger)
it "should initialize with an empty set", ->
expect(@selection.items()).toEqual([])
expect(@selection.ids()).toEqual([])
it "should throw an exception if a view is not provided", ->
expect( => new ModelViewSelection(null, @trigger)).toThrow()
describe "set", ->
it "should replace the current selection with the provided models", ->
@selection.set([@items[2], @items[4], @items[7]])
expect(@selection.ids()).toEqual(['2', '4', '7'])
@selection.set([@items[2], @items[5], @items[6]])
expect(@selection.ids()).toEqual(['2', '5', '6'])
it "should throw an exception if the items passed are not models", ->
expect( => @selection.set(['hi'])).toThrow()
it "should trigger", ->
@selection.set([@items[2], @items[4], @items[7]])
expect(@trigger).toHaveBeenCalled()
describe "clear", ->
beforeEach ->
@selection.set([@items[2]])
it "should empty the selection set", ->
@selection.clear()
expect(@selection.ids()).toEqual([])
it "should trigger", ->
@selection.clear()
expect(@trigger).toHaveBeenCalled()
describe "remove", ->
beforeEach ->
@selection.set([@items[2], @items[4]])
it "should do nothing if called without a valid item", ->
@selection.remove(null)
@selection.remove(undefined)
@selection.remove(false)
expect(@selection.ids()).toEqual(['2', '4'])
it "should remove the item from the set", ->
@selection.remove(@items[2])
expect(@selection.ids()).toEqual(['4'])
it "should throw an exception if the item passed is not a model", ->
expect( => @selection.remove('hi')).toThrow()
it "should trigger", ->
@selection.remove()
expect(@trigger).toHaveBeenCalled()
describe "updateModelReferences", ->
it "should replace items in the selection with the matching provided items, if present", ->
@selection.set([@items[2], @items[4], @items[7]])
expect(@selection.items()[0]).toBe(@items[2])
expect(@selection.items()[0].subject).toBe(undefined)
newItem2 = new Thread(id: '2', subject:'Hello world!')
@selection.updateModelReferences([newItem2])
expect(@selection.items()[0].subject).toBe('Hello world!')
describe "toggle", ->
beforeEach ->
@selection.set([@items[2]])
it "should do nothing if called without a valid item", ->
@selection.toggle(null)
@selection.toggle(undefined)
@selection.toggle(false)
expect(@selection.ids()).toEqual(['2'])
it "should throw an exception if the item passed is not a model", ->
expect( => @selection.toggle('hi')).toThrow()
it "should select the item if it is not selected", ->
@selection.toggle(@items[3])
expect(@selection.ids()).toEqual(['2', '3'])
it "should de-select the item if it is selected", ->
@selection.toggle(@items[2])
expect(@selection.ids()).toEqual([])
it "should trigger", ->
@selection.toggle(@items[2])
expect(@trigger).toHaveBeenCalled()
describe "expandTo", ->
it "should select the item, if no other items are selected", ->
@selection.clear()
@selection.expandTo(@items[2])
expect(@selection.ids()).toEqual(['2'])
it "should do nothing if called without a valid item", ->
@selection.expandTo(null)
@selection.expandTo(undefined)
@selection.expandTo(false)
expect(@selection.ids()).toEqual([])
it "should throw an exception if the item passed is not a model", ->
expect( => @selection.expandTo('hi')).toThrow()
it "should select all items from the last selected item to the provided item", ->
@selection.set([@items[2], @items[5]])
@selection.expandTo(@items[8])
expect(@selection.ids()).toEqual(['2','5','6','7','8'])
it "should not do anything if the provided item is not in the view set", ->
@selection.set([@items[2]])
@selection.expandTo(new Thread(id:'not-in-view!'))
expect(@selection.ids()).toEqual(['2'])
it "should re-order items so that the order still reflects the order selection actions were taken", ->
@selection.set([@items[10], @items[4], @items[1]])
@selection.expandTo(@items[8])
expect(@selection.ids()).toEqual(['10','1','2','3','4','5','6','7','8'])
it "should trigger", ->
@selection.set([@items[5], @items[4], @items[1]])
@selection.expandTo(@items[8])
expect(@trigger).toHaveBeenCalled()
describe "walk", ->
beforeEach ->
@selection.set([@items[2]])
it "should trigger", ->
current = @items[4]
next = @items[5]
@selection.walk({current, next})
expect(@trigger).toHaveBeenCalled()
it "should select both items if neither the start row or the end row are selected", ->
current = @items[4]
next = @items[5]
@selection.walk({current, next})
expect(@selection.ids()).toEqual(['2', '4', '5'])
it "should select only one item if either current or next is null or undefined", ->
current = null
next = @items[5]
@selection.walk({current, next})
expect(@selection.ids()).toEqual(['2', '5'])
next = null
current = @items[7]
@selection.walk({current, next})
expect(@selection.ids()).toEqual(['2', '5', '7'])
describe "when the `next` item is a step backwards in the selection history", ->
it "should deselect the current item", ->
@selection.set([@items[2], @items[3], @items[4], @items[5]])
current = @items[5]
next = @items[4]
@selection.walk({current, next})
expect(@selection.ids()).toEqual(['2', '3', '4'])
describe "otherwise", ->
it "should select the next item", ->
@selection.set([@items[2], @items[3], @items[4], @items[5]])
current = @items[5]
next = @items[6]
@selection.walk({current, next})
expect(@selection.ids()).toEqual(['2', '3', '4', '5', '6'])
describe "if the item was already selected", ->
it "should re-order the selection array so the selection still represents selection history", ->
@selection.set([@items[5], @items[8], @items[7], @items[6]])
expect(@selection.ids()).toEqual(['5', '8', '7', '6'])
current = @items[6]
next = @items[5]
@selection.walk({current, next})
expect(@selection.ids()).toEqual(['8', '7', '6', '5'])