aboutsummaryrefslogtreecommitdiffstats
path: root/vaadin-grid/test/grid-selecting-rows.html
blob: 0ac2eeef25955ca7c5c35333e896a8052282548c (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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
<!DOCTYPE html>
<html>

<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <script src="../../webcomponentsjs/webcomponents-lite.min.js"></script>
  <script src="../../web-component-tester/browser.js"></script>

  <script src="common.js"></script>

  <link rel="import" href="../vaadin-grid.html">
</head>

<body>

  <div id="gridwrapper"></div>

  <script>
    describe.feature('selecting rows', function() {
      var selectListener;

      before(function() {
        selectListener = sinon.spy();
        grid.addEventListener('select', selectListener);
      });

      function getSelectionModeAttr() {
        return grid.getAttribute('selection-mode');
      };

      function setSelectionModeAttr(mode) {
        grid.setAttribute('selection-mode', mode);

        return grid;
      };

      afterEach(function() {
        selectListener.reset();

        return grid.then(function() {
          // force selection clear
          grid.selection.mode = 'disabled';
          grid.selection.mode = 'single';

          return grid;
        });
      });

      describe('using attributes', function() {
        describe('selection-mode', function() {
          it('should set selectionMode to disabled', function() {
            return setSelectionModeAttr('disabled').then(function() {
              expect(grid.selection.mode).to.equal('disabled');
            });
          });

          it('should set selectionMode to single', function() {
            return setSelectionModeAttr('single').then(function() {
              expect(grid.selection.mode).to.equal('single');
            });
          });

          it('should set selectionMode to multi', function() {
            return setSelectionModeAttr('multi').then(function() {
              expect(grid.selection.mode).to.equal('multi');
            });
          });

          it('should should remove selectionMode property', function() {
            setSelectionModeAttr('disabled');
            grid.removeAttribute('selection-mode');

            return grid.then(function() {
              // 'single' is the default
              expect(grid.selection.mode).to.equal('single');
            });
          });

          // TODO: throws an error, but for some reason expect doesn't catch it.
          it.skip('should fail setting the value to foobar', function() {
            expect(function() {
              setSelectionModeAttr('foobar');
            }).to.throw(Object);
          });
        });
      });

      describe('using properties', function() {
        describe('selection.mode', function() {
          var checkBoxQuery = "input[type='checkbox']";

          it('should be single by default', function() {
            expect(grid.selection.mode).to.equal('single');
          });

          it('should be use default value when removing the value', function() {
            grid.selection.mode = 'disabled';

            grid.selection.mode = undefined;

            expect(grid.selection.mode).to.equal('single');
          });

          it('should set selection-mode to disabled', function() {
            grid.selection.mode = 'disabled';

            return grid.then(function() {
              expect(getSelectionModeAttr()).to.equal('disabled');
              expect(qLocal(checkBoxQuery)).to.be.undefined;
            });
          });

          it('should set selection-mode to single', function() {
            grid.selection.mode = 'single';

            return grid.then(function() {
              expect(getSelectionModeAttr()).to.equal('single');
              expect(qLocal(checkBoxQuery)).to.be.undefined;
            });
          });

          it('should set selection-mode to multi', function() {
            grid.selection.mode = 'multi';

            return grid.then(function() {
              expect(getSelectionModeAttr()).to.equal('multi');
              expect(qLocal(checkBoxQuery)).to.not.be.undefined;
            });
          });

          it('should fail setting the value to foobar', function() {
            expect(function() {
              grid.selection.mode = 'foobar';
            }).to.throw(Object);
          });
        });

        describe('selected', function() {
          describe('with mode: single', function() {
            beforeEach(function() {
              grid.selection.mode = 'single';
            });

            it('should clear the selection when changing mode to disabled', function() {
              grid.selection.select(0);

              grid.selection.mode = 'disabled';

              expect(grid.selection.selected()).to.be.empty;
            });

            it('should clear selection when changing mode to multi', function() {
              grid.selection.select(0);

              grid.selection.mode = 'multi';

              expect(grid.selection.selected()).to.be.empty;
            });
          });

          describe('with selectionMode: multi', function() {
            beforeEach(function() {
              grid.selection.mode = 'multi';
            });

            it('should fire a \'select\' event', function() {
              grid.selection.select(0);

              expect(selectListener.calledOnce).to.be.true;
            });

            it('should clear the selection when changing mode to disabled', function() {
              grid.selection.select(0);

              grid.selection.mode = 'disabled';

              expect(grid.selection.selected()).to.be.empty;
            });

            it('should clear selection when changing mode to single', function() {
              grid.selection.select(0);

              grid.selection.mode = 'single';

              expect(grid.selection.selected()).to.be.empty;
            });

            it('should remove multiple selections when changing mode to single', function() {
              grid.selection.select(0);
              grid.selection.select(1);

              grid.selection.mode = 'single';

              expect(grid.selection.selected()).to.be.empty;
            });
          });

          describe('with selectionMode: disabled', function() {
            beforeEach(function() {
              grid.selection.mode = 'disabled';
            });

            it('should not select any rows', function() {
              grid.selection.select(0);

              expect(grid.selection.selected()).to.be.empty;
            });
          });
        });
      });

      describe('by clicking', function() {
        function getFirstCell() {
          return qLocal('.v-grid-body .v-grid-cell');
        }

        function clickFirstCell() {
          getFirstCell().click();

          return grid;
        }

        before(function() {
          // extra 'wait' added for IE11.
          return grid;
        });

        it('should fire a \'select\' event', function()  {
          return clickFirstCell().then(function() {
            expect(selectListener.calledOnce).to.be.true;
            expect(selectListener.args[0][0]).to.have.property('type', 'select');
          });
        });

        describe('with selection-mode: single', function() {
          beforeEach(function() {
            return setSelectionModeAttr('single');
          });

          it('should select a row', function() {
            return clickFirstCell().then(function() {
              expect(grid.selection.selected()).to.eql([0]);
            });
          });
        });

        describe('with selection-mode: multi', function() {
          beforeEach(function() {
            return setSelectionModeAttr('multi');
          });

          it('should only focus a clicked cell', function() {
            return clickFirstCell().then(function() {
              expect(grid.selection.selected()).to.be.empty;
              expect(getFirstCell().getAttribute('class')).to.contain('focus');
            });
          });

          // TODO: apparently clicking checkboxes isn't going to happen using JS...
          it.skip('should select multiple rows', function() {
            var inputs = qaLocal('input');
            inputs[0].checked = true;
            inputs[1].click();

            expect(getSelectedRowsAttr()).to.equal('0,1');
          });
        });

        describe('with selection-mode: disabled', function() {
          beforeEach(function() {
            return setSelectionModeAttr('disabled');
          });

          it('should not select a row', function() {
            return clickFirstCell().then(function() {
              expect(grid.selection.selected()).to.be.empty;
            });
          });
        });
      });

      describe('by using API', function() {
        it('should fire a \'select\' event', function() {
          grid.selection.select(0);

          expect(selectListener.calledOnce).to.be.true;
        });

        it('should not select invalid row', function() {
          grid.selection.select('foo');

          expect(grid.selection.selected()).to.be.empty;
        });

        it('should add \'row-selected\' class to the selected row', function() {
          return grid.selection.select(1).then(function() {
            expect(qaLocal('.v-grid-body .v-grid-row-selected')).to.have.length(1);
          });
        });

        describe('with selectionMode: single', function() {
          beforeEach(function() {
            grid.selection.mode = 'single';
          });

          it('should select a row', function() {
            grid.selection.select(1);

            expect(grid.selection.selected()).to.eql([1]);
          });

          it('should select another row', function() {
            grid.selection.select(1);
            grid.selection.select(0);

            expect(grid.selection.selected()).to.eql([0]);
          });
        });

        describe('with selectionMode: multi', function() {
          beforeEach(function() {
            grid.selection.mode = 'multi';
          })

          it('should select multiple rows', function() {
            grid.selection.select(0);
            grid.selection.select(1);

            expect(grid.selection.selected()).to.eql([0,1]);
          });
        });

        describe('with selectionMode: disabled', function() {
          beforeEach(function() {
            grid.selection.mode = 'disabled';
          });

          it('should not select a row', function() {
            grid.selection.select(0);

            expect(grid.selection.selected()).to.be.empty;
          });
        });
      });

      describe('deselecting rows using API', function() {
        it('should fire \'select\' event', function() {
          grid.selection.select(0);

          grid.selection.deselect(0);

          expect(selectListener.calledTwice).to.be.true;
        });

        describe('with selectionMode: single', function() {
          beforeEach(function() {
            grid.selection.mode = 'single';
          });

          it('should deselect a row', function() {
            grid.selection.select(0);

            grid.selection.deselect(0);

            expect(grid.selectedRows).to.be.empty;
          });

          it('should not deselect other rows', function() {
            grid.selection.select(0);

            grid.selection.deselect(1);

            expect(grid.selection.selected()).to.eql([0]);
          });
        });

        describe('with selectionMode: multi', function() {
          beforeEach(function() {
            grid.selection.mode = 'multi';
          });

          it('should deselect a single row', function() {
            grid.selection.select(0);
            grid.selection.select(1);

            grid.selection.deselect(0);

            expect(grid.selection.selected()).to.eql([1]);
          });
        });
      });

      describe('selected indexes', function() {

        describe('in mode: single', function() {
          beforeEach(function() {
            grid.selection.mode = 'single';
            grid.selection.select(0);
          });

          it('should have a right size', function() {
            expect(grid.selection.size).to.eql(1);
          });

          it('should have right deselections', function() {
            expect(grid.selection.deselected()).to.be.empty;
          });

          it('should have right selections', function() {
            expect(grid.selection.selected()).to.eql([0]);
          });

          it('should iterate right selections', function() {
            var spy = sinon.spy();
            grid.selection.selected(spy);
            expect(spy.calledOnce).to.be.true;
          });

          it('should return right mapped value', function() {
            var mapper = function(index){
              return "foo " + index;
            };
            expect(grid.selection.selected(mapper)).to.eql(["foo 0"]);
          });

          it('should return nothing with iterator', function() {
            var iterator = function(i){
              // Itearate
            };
            expect(grid.selection.selected(iterator)).to.be.empty;
          });

          it('clear should work right', function() {
            grid.selection.clear();
            expect(grid.selection.selected()).to.be.empty;
          });

          it('selectAll should work right', function() {
            grid.selection.selectAll();
            expect(grid.selection.selected()).to.be.empty;
          });
        });

        describe('in mode: multi', function() {
          beforeEach(function() {
            grid.data.source =  function(req) {
              var data = [];
              for (var i = req.index; i < req.index + req.count; i++) {
                data.push(["foo " + i, "bar " + i]);
              }
              req.success(data, 100);
            };
            grid.selection.mode = 'multi';
            grid.selection.select(0);
            grid.selection.select(1);
            grid.selection.select(80);
            grid.selection.select(81);
          });

          it('should have a right size', function() {
            expect(grid.selection.size).to.eql(4);
          });

          it('should have right deselections', function() {
            expect(grid.selection.deselected()).to.be.empty;
          });

          it('should have right selections', function() {
            expect(grid.selection.selected()).to.eql([0,1,80,81]);
          });

          it('should iterate right selections', function() {
            var spy = sinon.spy();
            grid.selection.selected(spy);
            expect(spy.callCount).to.eql(4);
          });

          it('should return right mapped value', function() {
            var mapper = function(index){
              return "foo " + index;
            };
            expect(grid.selection.selected(mapper)).to.eql(["foo 0", "foo 1", "foo 80", "foo 81"]);
          });

          it('should return nothing with iterator', function() {
            var iterator = function(i){
              // Itearate
            };
            expect(grid.selection.selected(iterator)).to.be.empty;
          });

          it('should return tail', function() {
            expect(grid.selection.selected(undefined, 3)).to.eql([81]);
          });

          it('should return head', function() {
            expect(grid.selection.selected(undefined, undefined, 1)).to.eql([0, 1]);
          });

          it('should return range', function() {
            expect(grid.selection.selected(undefined, 1, 2)).to.eql([1, 80]);
          });

          it('clear should work right', function() {
            grid.selection.clear();
            expect(grid.selection.selected()).to.be.empty;
          });

          it('selectAll should work right', function() {
            grid.selection.selectAll();
            expect(grid.selection.mode).to.eql("all");
          });

        });

        describe('in mode: all', function() {
          beforeEach(function() {
            grid.data.source =  function(req) {
              var data = [];
              for (var i = req.index; i < req.index + req.count; i++) {
                data.push(["foo " + i, "bar " + i]);
              }
              req.success(data, 100);
            };
            grid.selection.mode = 'all';
            grid.selection.deselect(0);
            grid.selection.deselect(1);
            grid.selection.deselect(80);
            grid.selection.deselect(81);
          });

          it('should have a right size', function() {
            expect(grid.selection.size).to.eql(96);
          });

          it('should have right deselections', function() {
            expect(grid.selection.deselected()).to.eql([0,1,80,81]);
          });

          it('should have right selections', function() {
            expect(grid.selection.selected()).to.have.length(96);
          });

          it('should iterate right selections', function() {
            var spy = sinon.spy();
            grid.selection.selected(spy);
            expect(spy.callCount).to.eql(96);
          });

          it('should return right mapped value', function() {
            var mapper = function(index){
              return "foo " + index;
            };
            expect(grid.selection.selected(mapper)).to.contain("foo 2");
            expect(grid.selection.deselected(mapper)).to.eql(["foo 0", "foo 1", "foo 80", "foo 81"]);
          });

          it('should return nothing with iterator', function() {
            var iterator = function(i){
              // Itearate
            };
            expect(grid.selection.selected(iterator)).to.be.empty;
            expect(grid.selection.deselected(iterator)).to.be.empty;
          });

          it('should return tail', function() {
            expect(grid.selection.selected(undefined, 50)).to.have.length(46);
            expect(grid.selection.deselected(undefined, 3)).to.eql([81]);
          });

          it('should return head', function() {
            expect(grid.selection.selected(undefined, undefined, 30)).to.have.length(31);
            expect(grid.selection.deselected(undefined, undefined, 1)).to.eql([0, 1]);
          });

          it('should not contain deselected values', function() {
            expect(grid.selection.selected()).not.to.contain(0);
            expect(grid.selection.selected()).not.to.contain(80);
          });

          it('should return range', function() {
            expect(grid.selection.selected(undefined, 1, 2)).to.eql([3, 4]);
            expect(grid.selection.deselected(undefined, 1, 2)).to.eql([1, 80]);
          });

          it('clear should work right', function() {
            grid.selection.clear();
            expect(grid.selection.mode).to.eql("multi");
          });

          it('selectAll should work right', function() {
            grid.selection.selectAll();
            expect(grid.selection.deselected()).to.be.empty;
          });

        });

        describe('in mode: disabled', function() {
          beforeEach(function() {
            grid.selection.mode = 'disabled';
            grid.selection.select(0);
          });

          it('should have a right size', function() {
            expect(grid.selection.size).to.eql(0);
          });

          it('should have right deselections', function() {
            expect(grid.selection.deselected()).to.eql([]);
          });

          it('should have right selections', function() {
            expect(grid.selection.selected()).to.eql([]);
          });

          it('should iterate right selections', function() {
            var spy = sinon.spy();
            grid.selection.selected(spy);
            expect(spy.callCount).to.eql(0);
          });

          it('should return right mapped value', function() {
            var mapper = function(index){
              return "foo " + index;
            };
            expect(grid.selection.selected(mapper)).to.eql([]);
            expect(grid.selection.deselected(mapper)).to.eql([]);
          });

          it('selectAll should work right', function() {
            grid.selection.selectAll();
            expect(grid.selection.selected()).to.eql([]);
          });

        });

      });


    });
  </script>
</body>

</html>