/
grid-list.spec.ts
460 lines (363 loc) · 15.1 KB
/
grid-list.spec.ts
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
import {async, TestBed} from '@angular/core/testing';
import {Component, DebugElement} from '@angular/core';
import {By} from '@angular/platform-browser';
import {MdGridList, MdGridListModule} from './index';
import {MdGridTile, MdGridTileText} from './grid-tile';
describe('MdGridList', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MdGridListModule],
declarations: [
GridListWithoutCols,
GridListWithInvalidRowHeightRatio,
GridListWithTooWideColspan,
GridListWithUnspecifiedRowHeight,
GirdListWithRowHeightRatio,
GridListWithFitRowHeightMode,
GridListWithFixedRowHeightMode,
GridListWithUnitlessFixedRowHeight,
GridListWithUnspecifiedGutterSize,
GridListWithGutterSize,
GridListWithUnitlessGutterSize,
GridListWithRatioHeightAndMulipleRows,
GridListWithFixRowHeightAndMultipleRows,
GridListWithColspanBinding,
GridListWithRowspanBinding,
GridListWithComplexLayout,
GridListWithFootersWithoutLines,
GridListWithFooterContainingTwoLines,
],
});
TestBed.compileComponents();
}));
it('should throw error if cols is not defined', () => {
let fixture = TestBed.createComponent(GridListWithoutCols);
expect(() => fixture.detectChanges()).toThrowError(/must pass in number of columns/);
});
it('should throw error if rowHeight ratio is invalid', () => {
let fixture = TestBed.createComponent(GridListWithInvalidRowHeightRatio);
expect(() => fixture.detectChanges()).toThrowError(/invalid ratio given for row-height/);
});
it('should throw error if tile colspan is wider than total cols', () => {
let fixture = TestBed.createComponent(GridListWithTooWideColspan);
expect(() => fixture.detectChanges()).toThrowError(/tile with colspan 5 is wider than grid/);
});
it('should default to 1:1 row height if undefined ', () => {
let fixture = TestBed.createComponent(GridListWithUnspecifiedRowHeight);
fixture.detectChanges();
let tile = fixture.debugElement.query(By.directive(MdGridTile));
// In ratio mode, heights are set using the padding-top property.
expect(getStyle(tile, 'padding-top')).toBe('200px');
});
it('should use a ratio row height if passed in', () => {
let fixture = TestBed.createComponent(GirdListWithRowHeightRatio);
fixture.componentInstance.rowHeight = '4:1';
fixture.detectChanges();
let tile = fixture.debugElement.query(By.directive(MdGridTile));
expect(getStyle(tile, 'padding-top')).toBe('100px');
fixture.componentInstance.rowHeight = '2:1';
fixture.detectChanges();
expect(getStyle(tile, 'padding-top')).toBe('200px');
});
it('should divide row height evenly in "fit" mode', () => {
let fixture = TestBed.createComponent(GridListWithFitRowHeightMode);
fixture.componentInstance.totalHeight = '300px';
fixture.detectChanges();
let tile = fixture.debugElement.query(By.directive(MdGridTile));
// 149.5 * 2 = 299px + 1px gutter = 300px
expect(getStyle(tile, 'height')).toBe('149.5px');
fixture.componentInstance.totalHeight = '200px';
fixture.detectChanges();
// 99.5 * 2 = 199px + 1px gutter = 200px
expect(getStyle(tile, 'height')).toBe('99.5px');
});
it('should use the fixed row height if passed in', () => {
let fixture = TestBed.createComponent(GridListWithFixedRowHeightMode);
fixture.componentInstance.rowHeight = '100px';
fixture.detectChanges();
let tile = fixture.debugElement.query(By.directive(MdGridTile));
expect(getStyle(tile, 'height')).toBe('100px');
fixture.componentInstance.rowHeight = '200px';
fixture.detectChanges();
expect(getStyle(tile, 'height')).toBe('200px');
});
it('should default to pixels if row height units are missing', () => {
let fixture = TestBed.createComponent(GridListWithUnitlessFixedRowHeight);
fixture.detectChanges();
let tile = fixture.debugElement.query(By.directive(MdGridTile));
expect(getStyle(tile, 'height')).toBe('100px');
});
it('should default gutter size to 1px', () => {
let fixture = TestBed.createComponent(GridListWithUnspecifiedGutterSize);
fixture.detectChanges();
let tiles = fixture.debugElement.queryAll(By.css('md-grid-tile'));
// check horizontal gutter
expect(getStyle(tiles[0], 'width')).toBe('99.5px');
expect(getComputedLeft(tiles[1])).toBe(100.5);
// check vertical gutter
expect(getStyle(tiles[0], 'height')).toBe('100px');
expect(getStyle(tiles[2], 'top')).toBe('101px');
});
it('should set the gutter size if passed', () => {
let fixture = TestBed.createComponent(GridListWithGutterSize);
fixture.detectChanges();
let tiles = fixture.debugElement.queryAll(By.css('md-grid-tile'));
// check horizontal gutter
expect(getStyle(tiles[0], 'width')).toBe('99px');
expect(getComputedLeft(tiles[1])).toBe(101);
// check vertical gutter
expect(getStyle(tiles[0], 'height')).toBe('100px');
expect(getStyle(tiles[2], 'top')).toBe('102px');
});
it('should use pixels if gutter units are missing', () => {
let fixture = TestBed.createComponent(GridListWithUnitlessGutterSize);
fixture.detectChanges();
let tiles = fixture.debugElement.queryAll(By.css('md-grid-tile'));
// check horizontal gutter
expect(getStyle(tiles[0], 'width')).toBe('99px');
expect(getComputedLeft(tiles[1])).toBe(101);
// check vertical gutter
expect(getStyle(tiles[0], 'height')).toBe('100px');
expect(getStyle(tiles[2], 'top')).toBe('102px');
});
it('should set the correct list height in ratio mode', () => {
let fixture = TestBed.createComponent(GridListWithRatioHeightAndMulipleRows);
fixture.detectChanges();
let list = fixture.debugElement.query(By.directive(MdGridList));
expect(getStyle(list, 'padding-bottom')).toBe('201px');
});
it('should set the correct list height in fixed mode', () => {
let fixture = TestBed.createComponent(GridListWithFixRowHeightAndMultipleRows);
fixture.detectChanges();
let list = fixture.debugElement.query(By.directive(MdGridList));
expect(getStyle(list, 'height')).toBe('201px');
});
it('should allow adjustment of tile colspan', () => {
let fixture = TestBed.createComponent(GridListWithColspanBinding);
fixture.componentInstance.colspan = 2;
fixture.detectChanges();
let tile = fixture.debugElement.query(By.directive(MdGridTile));
expect(getStyle(tile, 'width')).toBe('199.5px');
fixture.componentInstance.colspan = 3;
fixture.detectChanges();
expect(getStyle(tile, 'width')).toBe('299.75px');
});
it('should allow adjustment of tile rowspan', () => {
let fixture = TestBed.createComponent(GridListWithRowspanBinding);
fixture.componentInstance.rowspan = 2;
fixture.detectChanges();
let tile = fixture.debugElement.query(By.directive(MdGridTile));
expect(getStyle(tile, 'height')).toBe('201px');
fixture.componentInstance.rowspan = 3;
fixture.detectChanges();
expect(getStyle(tile, 'height')).toBe('302px');
});
it('should lay out tiles correctly for a complex layout', () => {
let fixture = TestBed.createComponent(GridListWithComplexLayout);
fixture.componentInstance.tiles = [
{cols: 3, rows: 1},
{cols: 1, rows: 2},
{cols: 1, rows: 1},
{cols: 2, rows: 1}
];
fixture.detectChanges();
let tiles = fixture.debugElement.queryAll(By.css('md-grid-tile'));
expect(getStyle(tiles[0], 'width')).toBe('299.75px');
expect(getStyle(tiles[0], 'height')).toBe('100px');
expect(getComputedLeft(tiles[0])).toBe(0);
expect(getStyle(tiles[0], 'top')).toBe('0px');
expect(getStyle(tiles[1], 'width')).toBe('99.25px');
expect(getStyle(tiles[1], 'height')).toBe('201px');
expect(getComputedLeft(tiles[1])).toBe(300.75);
expect(getStyle(tiles[1], 'top')).toBe('0px');
expect(getStyle(tiles[2], 'width')).toBe('99.25px');
expect(getStyle(tiles[2], 'height')).toBe('100px');
expect(getComputedLeft(tiles[2])).toBe(0);
expect(getStyle(tiles[2], 'top')).toBe('101px');
expect(getStyle(tiles[3], 'width')).toBe('199.5px');
expect(getStyle(tiles[3], 'height')).toBe('100px');
expect(getComputedLeft(tiles[3])).toBe(100.25);
expect(getStyle(tiles[3], 'top')).toBe('101px');
});
it('should add not add any classes to footers without lines', () => {
let fixture = TestBed.createComponent(GridListWithFootersWithoutLines);
fixture.detectChanges();
let footer = fixture.debugElement.query(By.directive(MdGridTileText));
expect(footer.nativeElement.classList.contains('mat-2-line')).toBe(false);
});
it('should add class to footers with two lines', () => {
let fixture = TestBed.createComponent(GridListWithFooterContainingTwoLines);
fixture.detectChanges();
let footer = fixture.debugElement.query(By.directive(MdGridTileText));
expect(footer.nativeElement.classList.contains('mat-2-line')).toBe(true);
});
it('should not use calc() that evaluates to 0', () => {
const fixture = TestBed.createComponent(GirdListWithRowHeightRatio);
fixture.componentInstance.rowHeight = '4:1';
fixture.detectChanges();
const firstTile = fixture.debugElement.query(By.directive(MdGridTile)).nativeElement;
expect(firstTile.style.marginTop).toBe('0px');
expect(firstTile.style.left).toBe('0px');
});
it('should reset the old styles when switching to a new tile styler', () => {
const fixture = TestBed.createComponent(GirdListWithRowHeightRatio);
fixture.componentInstance.rowHeight = '4:1';
fixture.detectChanges();
const list = fixture.debugElement.query(By.directive(MdGridList));
const tile = fixture.debugElement.query(By.directive(MdGridTile));
expect(getStyle(tile, 'padding-top')).toBe('100px');
expect(getStyle(list, 'padding-bottom')).toBe('100px');
fixture.componentInstance.rowHeight = '400px';
fixture.detectChanges();
expect(getStyle(tile, 'padding-top')).toBe('0px', 'Expected tile padding to be reset.');
expect(getStyle(list, 'padding-bottom')).toBe('0px', 'Expected list padding to be reset.');
expect(getStyle(tile, 'top')).toBe('0px');
expect(getStyle(tile, 'height')).toBe('400px');
});
});
function getStyle(el: DebugElement, prop: string): string {
return getComputedStyle(el.nativeElement).getPropertyValue(prop);
}
/** Gets the `left` position of an element. */
function getComputedLeft(element: DebugElement): number {
// While the other properties in this test use `getComputedStyle`, we use `getBoundingClientRect`
// for left because iOS Safari doesn't support using `getComputedStyle` to get the calculated
// `left` balue when using CSS `calc`. We subtract the `left` of the document body because
// browsers, by default, add a margin to the body (typically 8px).
let elementRect = element.nativeElement.getBoundingClientRect();
let bodyRect = document.body.getBoundingClientRect();
return elementRect.left - bodyRect.left;
}
@Component({template: '<md-grid-list></md-grid-list>'})
class GridListWithoutCols { }
@Component({template: '<md-grid-list cols="4" rowHeight="4:3:2"></md-grid-list>'})
class GridListWithInvalidRowHeightRatio { }
@Component({template:
'<md-grid-list cols="4"><md-grid-tile colspan="5"></md-grid-tile></md-grid-list>'})
class GridListWithTooWideColspan { }
@Component({template: `
<div style="width:200px">
<md-grid-list cols="1">
<md-grid-tile></md-grid-tile>
</md-grid-list>
</div>`})
class GridListWithUnspecifiedRowHeight { }
@Component({template: `
<div style="width:400px">
<md-grid-list cols="1" [rowHeight]="rowHeight">
<md-grid-tile></md-grid-tile>
</md-grid-list>
</div>`})
class GirdListWithRowHeightRatio {
rowHeight: string;
}
@Component({template: `
<md-grid-list cols="1" rowHeight="fit" [style.height]="totalHeight">
<md-grid-tile></md-grid-tile>
<md-grid-tile></md-grid-tile>
</md-grid-list>`})
class GridListWithFitRowHeightMode {
totalHeight: string;
}
@Component({template: `
<md-grid-list cols="4" [rowHeight]="rowHeight">
<md-grid-tile></md-grid-tile>
</md-grid-list>`})
class GridListWithFixedRowHeightMode {
rowHeight: string;
}
@Component({template: `
<md-grid-list cols="4" rowHeight="100">
<md-grid-tile></md-grid-tile>
</md-grid-list>`})
class GridListWithUnitlessFixedRowHeight {
rowHeight: string;
}
@Component({template: `
<div style="width:200px">
<md-grid-list cols="2" rowHeight="100px">
<md-grid-tile></md-grid-tile>
<md-grid-tile></md-grid-tile>
<md-grid-tile></md-grid-tile>
</md-grid-list>
</div>`})
class GridListWithUnspecifiedGutterSize { }
@Component({template: `
<div style="width:200px">
<md-grid-list cols="2" gutterSize="2px" rowHeight="100px">
<md-grid-tile></md-grid-tile>
<md-grid-tile></md-grid-tile>
<md-grid-tile></md-grid-tile>
</md-grid-list>
</div>`})
class GridListWithGutterSize { }
@Component({template: `
<div style="width:200px">
<md-grid-list cols="2" gutterSize="2" rowHeight="100px">
<md-grid-tile></md-grid-tile>
<md-grid-tile></md-grid-tile>
<md-grid-tile></md-grid-tile>
</md-grid-list>
</div>`})
class GridListWithUnitlessGutterSize { }
@Component({template: `
<div style="width:400px">
<md-grid-list cols="1" rowHeight="4:1">
<md-grid-tile></md-grid-tile>
<md-grid-tile></md-grid-tile>
</md-grid-list>
</div>`})
class GridListWithRatioHeightAndMulipleRows { }
@Component({template: `
<md-grid-list cols="1" rowHeight="100px">
<md-grid-tile></md-grid-tile>
<md-grid-tile></md-grid-tile>
</md-grid-list>`})
class GridListWithFixRowHeightAndMultipleRows { }
@Component({template: `
<div style="width:400px">
<md-grid-list cols="4">
<md-grid-tile [colspan]="colspan"></md-grid-tile>
</md-grid-list>
</div>`})
class GridListWithColspanBinding {
colspan: number;
}
@Component({template: `
<md-grid-list cols="1" rowHeight="100px">
<md-grid-tile [rowspan]="rowspan"></md-grid-tile>
</md-grid-list>`})
class GridListWithRowspanBinding {
rowspan: number;
}
@Component({template: `
<div style="width:400px">
<md-grid-list cols="4" rowHeight="100px">
<md-grid-tile *ngFor="let tile of tiles" [colspan]="tile.cols" [rowspan]="tile.rows"
[style.background]="tile.color">
{{tile.text}}
</md-grid-tile>
</md-grid-list>
</div>`})
class GridListWithComplexLayout {
tiles: any[];
}
@Component({template: `
<md-grid-list cols="1">
<md-grid-tile>
<md-grid-tile-footer>
I'm a footer!
</md-grid-tile-footer>
</md-grid-tile>
</md-grid-list>`})
class GridListWithFootersWithoutLines { }
@Component({template: `
<md-grid-list cols="1">
<md-grid-tile>
<md-grid-tile-footer>
<h3 md-line>First line</h3>
<span md-line>Second line</span>
</md-grid-tile-footer>
</md-grid-tile>
</md-grid-list>`})
class GridListWithFooterContainingTwoLines { }