-
Notifications
You must be signed in to change notification settings - Fork 155
/
sorting.html
471 lines (381 loc) · 15.7 KB
/
sorting.html
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../web-component-tester/browser.js"></script>
<script src="../../iron-test-helpers/mock-interactions.js"></script>
<link rel="import" href="helpers.html">
<link rel="import" href="../vaadin-grid.html">
<link rel="import" href="../vaadin-grid-sorter.html">
</head>
<body>
<test-fixture id="sorter">
<template>
<vaadin-grid-sorter path="path">
<span class="title">title</span><button>Button</button>
</vaadin-grid-sorter>
</template>
</test-fixture>
<test-fixture id="sorter-in-cell-content">
<template>
<div class="cell-content" data-cell-content-id="">
<vaadin-grid-sorter path="path"></vaadin-grid-sorter>
</div>
</template>
</test-fixture>
<test-fixture id="grid">
<template>
<vaadin-grid style="width: 200px; height: 200px;" multi-sort>
<vaadin-grid-column>
<template class="header">
<vaadin-grid-sorter path="first" direction="asc">
<span class="title">first</span>
</vaadin-grid-sorter>
</template>
<template>[[item.first]]</template>
</vaadin-grid-column>
<vaadin-grid-column>
<template class="header">
<span class="title">last</span>
<vaadin-grid-sorter path="last" direction="desc">
</vaadin-grid-sorter>
</template>
<template>[[item.last]]</template>
</vaadin-grid-column>
</vaadin-grid>
</template>
</test-fixture>
<script>
describe('sorting', function() {
describe('sorter', function() {
var sorter, title, button, indicators, directionIndicator, orderIndicator, announcer;
beforeEach(function() {
sorter = fixture('sorter');
title = Polymer.dom(sorter).querySelector('.title');
button = Polymer.dom(sorter).querySelector('button');
indicators = Polymer.dom(sorter.root).querySelector('#indicators');
directionIndicator = Polymer.dom(sorter.root).querySelector('#direction');
orderIndicator = Polymer.dom(sorter.root).querySelector('#order');
announcer = Polymer.dom(sorter.root).querySelector('#announcer');
});
it('should have default direction', function() {
expect(sorter.direction).to.equal(null);
});
it('should toggle direction', function() {
click(title);
expect(sorter.direction).to.equal('asc');
click(title);
expect(sorter.direction).to.equal('desc');
click(title);
expect(sorter.direction).to.equal(null);
});
it('should not toggle on focusable click', function() {
click(button);
expect(sorter.direction).to.equal(null);
});
it('should fire a sorter-changed event', function(done) {
sorter.addEventListener('sorter-changed', function() {
done();
});
sorter.direction = 'asc';
});
it('should show order indicator', function() {
expect(orderIndicator.innerText).to.equal('');
sorter._order = 0;
expect(orderIndicator.innerText).to.equal('1');
sorter._order = 4;
expect(orderIndicator.innerText).to.equal('5');
});
it('should show direction indicator', function() {
expect(sorter.getAttribute('direction')).to.equal(null);
sorter.direction = 'asc';
expect(sorter.getAttribute('direction')).to.equal('asc');
sorter.direction = 'desc';
expect(sorter.getAttribute('direction')).to.equal('desc');
});
it('should have aria-hidden for indicator', function() {
expect(indicators.getAttribute('aria-hidden')).to.equal('true');
});
describe('announcer', function() {
it('should reflect direction', function() {
expect(announcer.textContent).to.have.string('Unsorted');
expect(announcer.textContent).to.not.have.string('Sorted');
sorter.direction = 'asc';
expect(announcer.textContent).to.have.string('Sorted');
expect(announcer.textContent).to.not.have.string('Unsorted');
expect(announcer.textContent).to.have.string('ascending');
expect(announcer.textContent).to.not.have.string('descending');
sorter.direction = 'desc';
expect(announcer.textContent).to.have.string('Sorted');
expect(announcer.textContent).to.not.have.string('Unsorted');
expect(announcer.textContent).to.have.string('descending');
expect(announcer.textContent).to.not.have.string('ascending');
});
it('should reflect order', function() {
sorter.direction = 'asc';
expect(announcer.textContent).to.not.have.string('Order');
sorter._order = 0;
expect(announcer.textContent).to.have.string('Order 1');
sorter._order = 4;
expect(announcer.textContent).to.have.string('Order 5');
});
it('should announce direction change', function() {
var spy = sinon.spy();
sorter.addEventListener('iron-announce', spy);
sorter.direction = 'asc';
sorter.flushDebouncer('direction-announce');
expect(spy.callCount).to.eql(1);
});
it('should announce order change', function() {
var spy = sinon.spy();
sorter.addEventListener('iron-announce', spy);
sorter._order = 0;
sorter.flushDebouncer('direction-announce');
expect(spy.callCount).to.eql(1);
});
it('should debounce direction and order changes', function() {
var spy = sinon.spy();
sorter.addEventListener('iron-announce', spy);
sorter.direction = 'asc';
sorter._order = 0;
sorter.flushDebouncer('direction-announce');
expect(spy.callCount).to.eql(1);
expect(spy.args[0][0].detail.text).to.eql('Sorted ascending, Order 1.');
});
});
it('should prevent default on click', function() {
var clickEvent = new CustomEvent('click', {cancelable: true});
sorter.dispatchEvent(clickEvent);
expect(clickEvent.defaultPrevented).to.be.true;
});
});
describe('sorter in cell content', function() {
var cellContent, sorter, announcer;
beforeEach(function() {
cellContent = fixture('sorter-in-cell-content');
sorter = Polymer.dom(cellContent).querySelector('vaadin-grid-sorter');
announcer = Polymer.dom(sorter.root).querySelector('#announcer');
});
it('should have aria-hidden state on announcer by default', function() {
expect(announcer.getAttribute('aria-hidden')).to.equal('true');
});
it('should not have aria-hidden state on announcer after cell content receives focus', function() {
cellContent.dispatchEvent(new CustomEvent('cell-focusin'));
expect(announcer.getAttribute('aria-hidden')).to.equal('false');
});
it('should have aria-hidden state on announcer after cell content looses focus', function(done) {
cellContent.dispatchEvent(new CustomEvent('cell-focusin'));
cellContent.dispatchEvent(new CustomEvent('cell-focusout'));
sorter.async(function() {
expect(announcer.getAttribute('aria-hidden')).to.equal('true');
done();
}, 1);
});
});
describe('grid', function() {
var grid, sorterFirst, sorterLast;
beforeEach(function(done) {
grid = fixture('grid');
Polymer.dom.flush();
sorterFirst = getHeaderCellContent(grid, 0, 0).querySelector('vaadin-grid-sorter');
sorterLast = getHeaderCellContent(grid, 0, 1).querySelector('vaadin-grid-sorter');
grid.items = [{
first: 'foo',
last: 'bar'
}, {
first: 'foo',
last: 'baz'
}, {
first: 'bar',
last: 'bar'
}];
setTimeout(done, 1);
});
it('should be clickable', function() {
var title = Polymer.dom(sorterFirst).querySelector('.title');
expect(window.getComputedStyle(title).cursor).to.equal('pointer');
});
it('should ignore sorter', function() {
sorterFirst.direction = '';
var sortOrders = grid._mapSorters();
expect(sortOrders).to.have.length(1);
expect(sortOrders[0].path).to.equal('last');
expect(sortOrders[0].direction).to.equal('desc');
});
it('should show order indicators', function() {
expect(sorterFirst._order).to.equal(1);
expect(sorterLast._order).to.equal(0);
});
it('should not show order indicators for one sorter', function() {
sorterLast.direction = '';
expect(sorterFirst._order).to.equal(null);
expect(sorterLast._order).to.equal(null);
});
it('should not show order indicators', function() {
sorterFirst.direction = null;
sorterLast.direction = null;
expect(sorterFirst._order).to.equal(null);
expect(sorterLast._order).to.equal(null);
});
describe('array data provider', function() {
it('should sort automatically', function() {
expect(getBodyCellContent(grid, 0, 0).innerText).to.equal('foo');
expect(getBodyCellContent(grid, 1, 0).innerText).to.equal('bar');
expect(getBodyCellContent(grid, 2, 0).innerText).to.equal('foo');
expect(getBodyCellContent(grid, 0, 1).innerText).to.equal('baz');
expect(getBodyCellContent(grid, 1, 1).innerText).to.equal('bar');
expect(getBodyCellContent(grid, 2, 1).innerText).to.equal('bar');
});
it('should sort automatically on sort', function() {
sorterFirst.direction = null;
expect(getBodyCellContent(grid, 0, 0).innerText).to.equal('foo');
expect(getBodyCellContent(grid, 1, 0).innerText).to.equal('foo');
expect(getBodyCellContent(grid, 2, 0).innerText).to.equal('bar');
});
it('should not sort the items if sorters is empty', function() {
sorterFirst.direction = null;
sorterLast.direction = null;
grid.items = buildDataSet(1000);
var bodyRows = getRows(grid.$.scroller.$.items);
var cells = getRowCells(bodyRows[0]);
expect(cells[0].item).to.equal(grid.items[0]);
});
it('should sort empty values', function() {
grid.items = [{
first: 'foo',
last: 'bar'
}, {
first: '',
last: ''
}, {
first: 'bar',
last: 'bar'
}];
expect(getBodyCellContent(grid, 0, 0).innerText).to.equal('bar');
expect(getBodyCellContent(grid, 1, 0).innerText).to.equal('foo');
expect(getBodyCellContent(grid, 2, 0).innerText).to.equal('');
});
it('should sort null values', function() {
grid.items = [{
first: 'foo',
last: 'bar'
}, {
first: null,
last: null
}, {
first: 'bar',
last: 'bar'
}];
expect(getBodyCellContent(grid, 0, 0).innerText).to.equal('bar');
expect(getBodyCellContent(grid, 1, 0).innerText).to.equal('foo');
expect(getBodyCellContent(grid, 2, 0).innerText).to.equal('');
});
it('should sort undefined values', function() {
grid.items = [{
first: 'foo',
last: 'bar'
}, {
first: undefined,
last: undefined
}, {
first: 'bar',
last: 'bar'
}];
expect(getBodyCellContent(grid, 0, 0).innerText).to.equal('bar');
expect(getBodyCellContent(grid, 1, 0).innerText).to.equal('foo');
expect(getBodyCellContent(grid, 2, 0).innerText).to.equal('');
});
it('should sort NaN values', function() {
grid.items = [{
first: 'foo',
last: 'bar'
}, {
first: NaN,
last: NaN
}, {
first: 'bar',
last: 'bar'
}];
expect(getBodyCellContent(grid, 0, 0).innerText).to.equal('bar');
expect(getBodyCellContent(grid, 1, 0).innerText).to.equal('foo');
expect(getBodyCellContent(grid, 2, 0).innerText).to.equal('NaN');
});
it('should sort numbers correctly', function() {
grid.items = [{
first: 1
}, {
first: 2
}, {
first: 11
}];
expect(getBodyCellContent(grid, 0, 0).innerText).to.equal('1');
expect(getBodyCellContent(grid, 1, 0).innerText).to.equal('2');
expect(getBodyCellContent(grid, 2, 0).innerText).to.equal('11');
});
});
describe('data provider', function() {
beforeEach(function() {
grid.dataProvider = sinon.spy(grid.dataProvider);
grid.clearCache();
});
it('should request with default sorting', function() {
var lastCall = grid.dataProvider.lastCall;
var params = lastCall.args[0];
expect(params.sortOrders).to.eql([{
path: 'last',
direction: 'desc'
}, {
path: 'first',
direction: 'asc'
}]);
});
it('should request new data on sort', function() {
sorterFirst.direction = 'desc';
var lastCall = grid.dataProvider.lastCall;
var params = lastCall.args[0];
expect(params.sortOrders).to.eql([{
path: 'first',
direction: 'desc'
}, {
path: 'last',
direction: 'desc'
}]);
});
it('should request new data on change in existing sorters', function() {
grid.dataProvider.reset();
sorterLast.direction = 'asc';
expect(grid.dataProvider.called).to.be.true;
});
});
describe('single column sorting', function() {
beforeEach(function() {
grid.multiSort = false;
grid.dataProvider = sinon.spy(grid.dataProvider);
});
it('should only using single sorter', function() {
grid.dataProvider.reset();
sorterFirst.direction = 'desc';
expect(grid.dataProvider.args[1][0].sortOrders.length).to.eql(1);
});
it('should remove order from sorters', function() {
// initial order before multiSort was set
expect(sorterLast._order).to.eql(0);
expect(sorterFirst._order).to.eql(1);
sorterFirst.direction = 'desc';
expect(sorterFirst._order).to.be.null;
expect(sorterLast._order).to.be.null;
});
it('should remove direction from previous sorter', function() {
sorterFirst.direction = 'desc';
sorterLast.direction = 'desc';
expect(sorterFirst.direction).to.be.null;
expect(sorterLast.direction).to.eql('desc');
});
});
});
});
</script>
</body>
</html>