1
- import { async , TestBed } from '@angular/core/testing' ;
2
- import { Component , DebugElement } from '@angular/core' ;
1
+ import { TestBed , ComponentFixture } from '@angular/core/testing' ;
2
+ import { Component , DebugElement , Type } from '@angular/core' ;
3
3
import { By } from '@angular/platform-browser' ;
4
4
import { MatGridList , MatGridListModule } from './index' ;
5
5
import { MatGridTile , MatGridTileText } from './grid-tile' ;
6
6
import { Directionality } from '@angular/cdk/bidi' ;
7
7
8
8
9
9
describe ( 'MatGridList' , ( ) => {
10
- beforeEach ( async ( ( ) = > {
10
+ function createComponent < T > ( componentType : Type < T > ) : ComponentFixture < T > {
11
11
TestBed . configureTestingModule ( {
12
12
imports : [ MatGridListModule ] ,
13
- declarations : [
14
- GridListWithoutCols ,
15
- GridListWithInvalidRowHeightRatio ,
16
- GridListWithTooWideColspan ,
17
- GridListWithUnspecifiedRowHeight ,
18
- GirdListWithRowHeightRatio ,
19
- GridListWithFitRowHeightMode ,
20
- GridListWithFixedRowHeightMode ,
21
- GridListWithUnitlessFixedRowHeight ,
22
- GridListWithUnspecifiedGutterSize ,
23
- GridListWithGutterSize ,
24
- GridListWithUnitlessGutterSize ,
25
- GridListWithRatioHeightAndMulipleRows ,
26
- GridListWithFixRowHeightAndMultipleRows ,
27
- GridListWithColspanBinding ,
28
- GridListWithRowspanBinding ,
29
- GridListWithComplexLayout ,
30
- GridListWithFootersWithoutLines ,
31
- GridListWithFooterContainingTwoLines ,
32
- GridListWithoutMatchingGap ,
33
- GridListWithEmptyDirectionality ,
34
- GridListWithRtl ,
35
- GridListWithIndirectTileDescendants ,
36
- ] ,
37
- } ) ;
38
-
39
- TestBed . compileComponents ( ) ;
40
- } ) ) ;
13
+ declarations : [ componentType ] ,
14
+ } ) . compileComponents ( ) ;
15
+
16
+ return TestBed . createComponent < T > ( componentType ) ;
17
+ }
41
18
42
19
it ( 'should throw error if cols is not defined' , ( ) => {
43
- let fixture = TestBed . createComponent ( GridListWithoutCols ) ;
20
+ const fixture = createComponent ( GridListWithoutCols ) ;
44
21
45
22
expect ( ( ) => fixture . detectChanges ( ) ) . toThrowError ( / m u s t p a s s i n n u m b e r o f c o l u m n s / ) ;
46
23
} ) ;
47
24
48
25
it ( 'should throw error if rowHeight ratio is invalid' , ( ) => {
49
- let fixture = TestBed . createComponent ( GridListWithInvalidRowHeightRatio ) ;
26
+ const fixture = createComponent ( GridListWithInvalidRowHeightRatio ) ;
50
27
51
28
expect ( ( ) => fixture . detectChanges ( ) ) . toThrowError ( / i n v a l i d r a t i o g i v e n f o r r o w - h e i g h t / ) ;
52
29
} ) ;
53
30
54
31
it ( 'should throw error if tile colspan is wider than total cols' , ( ) => {
55
- let fixture = TestBed . createComponent ( GridListWithTooWideColspan ) ;
32
+ const fixture = createComponent ( GridListWithTooWideColspan ) ;
56
33
57
34
expect ( ( ) => fixture . detectChanges ( ) ) . toThrowError ( / t i l e w i t h c o l s p a n 5 i s w i d e r t h a n g r i d / ) ;
58
35
} ) ;
59
36
60
37
it ( 'should default to 1:1 row height if undefined ' , ( ) => {
61
- let fixture = TestBed . createComponent ( GridListWithUnspecifiedRowHeight ) ;
38
+ const fixture = createComponent ( GridListWithUnspecifiedRowHeight ) ;
62
39
fixture . detectChanges ( ) ;
63
- let tile = fixture . debugElement . query ( By . directive ( MatGridTile ) ) ;
40
+ const tile = fixture . debugElement . query ( By . directive ( MatGridTile ) ) ;
64
41
65
42
// In ratio mode, heights are set using the padding-top property.
66
43
expect ( getStyle ( tile , 'padding-top' ) ) . toBe ( '200px' ) ;
67
44
} ) ;
68
45
69
46
it ( 'should use a ratio row height if passed in' , ( ) => {
70
- let fixture = TestBed . createComponent ( GirdListWithRowHeightRatio ) ;
47
+ const fixture = createComponent ( GirdListWithRowHeightRatio ) ;
71
48
72
49
fixture . componentInstance . rowHeight = '4:1' ;
73
50
fixture . detectChanges ( ) ;
74
51
75
- let tile = fixture . debugElement . query ( By . directive ( MatGridTile ) ) ;
52
+ const tile = fixture . debugElement . query ( By . directive ( MatGridTile ) ) ;
76
53
expect ( getStyle ( tile , 'padding-top' ) ) . toBe ( '100px' ) ;
77
54
78
55
fixture . componentInstance . rowHeight = '2:1' ;
@@ -82,11 +59,11 @@ describe('MatGridList', () => {
82
59
} ) ;
83
60
84
61
it ( 'should divide row height evenly in "fit" mode' , ( ) => {
85
- let fixture = TestBed . createComponent ( GridListWithFitRowHeightMode ) ;
62
+ const fixture = createComponent ( GridListWithFitRowHeightMode ) ;
86
63
87
64
fixture . componentInstance . totalHeight = '300px' ;
88
65
fixture . detectChanges ( ) ;
89
- let tile = fixture . debugElement . query ( By . directive ( MatGridTile ) ) ;
66
+ const tile = fixture . debugElement . query ( By . directive ( MatGridTile ) ) ;
90
67
91
68
// 149.5 * 2 = 299px + 1px gutter = 300px
92
69
expect ( getStyle ( tile , 'height' ) ) . toBe ( '149.5px' ) ;
@@ -99,12 +76,12 @@ describe('MatGridList', () => {
99
76
} ) ;
100
77
101
78
it ( 'should use the fixed row height if passed in' , ( ) => {
102
- let fixture = TestBed . createComponent ( GridListWithFixedRowHeightMode ) ;
79
+ const fixture = createComponent ( GridListWithFixedRowHeightMode ) ;
103
80
104
81
fixture . componentInstance . rowHeight = '100px' ;
105
82
fixture . detectChanges ( ) ;
106
83
107
- let tile = fixture . debugElement . query ( By . directive ( MatGridTile ) ) ;
84
+ const tile = fixture . debugElement . query ( By . directive ( MatGridTile ) ) ;
108
85
expect ( getStyle ( tile , 'height' ) ) . toBe ( '100px' ) ;
109
86
110
87
fixture . componentInstance . rowHeight = '200px' ;
@@ -114,18 +91,18 @@ describe('MatGridList', () => {
114
91
} ) ;
115
92
116
93
it ( 'should default to pixels if row height units are missing' , ( ) => {
117
- let fixture = TestBed . createComponent ( GridListWithUnitlessFixedRowHeight ) ;
94
+ const fixture = createComponent ( GridListWithUnitlessFixedRowHeight ) ;
118
95
fixture . detectChanges ( ) ;
119
96
120
- let tile = fixture . debugElement . query ( By . directive ( MatGridTile ) ) ;
97
+ const tile = fixture . debugElement . query ( By . directive ( MatGridTile ) ) ;
121
98
expect ( getStyle ( tile , 'height' ) ) . toBe ( '100px' ) ;
122
99
} ) ;
123
100
124
101
it ( 'should default gutter size to 1px' , ( ) => {
125
- let fixture = TestBed . createComponent ( GridListWithUnspecifiedGutterSize ) ;
102
+ const fixture = createComponent ( GridListWithUnspecifiedGutterSize ) ;
126
103
fixture . detectChanges ( ) ;
127
104
128
- let tiles = fixture . debugElement . queryAll ( By . css ( 'mat-grid-tile' ) ) ;
105
+ const tiles = fixture . debugElement . queryAll ( By . css ( 'mat-grid-tile' ) ) ;
129
106
130
107
// check horizontal gutter
131
108
expect ( getStyle ( tiles [ 0 ] , 'width' ) ) . toBe ( '99.5px' ) ;
@@ -137,10 +114,10 @@ describe('MatGridList', () => {
137
114
} ) ;
138
115
139
116
it ( 'should set the gutter size if passed' , ( ) => {
140
- let fixture = TestBed . createComponent ( GridListWithGutterSize ) ;
117
+ const fixture = createComponent ( GridListWithGutterSize ) ;
141
118
fixture . detectChanges ( ) ;
142
119
143
- let tiles = fixture . debugElement . queryAll ( By . css ( 'mat-grid-tile' ) ) ;
120
+ const tiles = fixture . debugElement . queryAll ( By . css ( 'mat-grid-tile' ) ) ;
144
121
145
122
// check horizontal gutter
146
123
expect ( getStyle ( tiles [ 0 ] , 'width' ) ) . toBe ( '99px' ) ;
@@ -152,10 +129,10 @@ describe('MatGridList', () => {
152
129
} ) ;
153
130
154
131
it ( 'should use pixels if gutter units are missing' , ( ) => {
155
- let fixture = TestBed . createComponent ( GridListWithUnitlessGutterSize ) ;
132
+ const fixture = createComponent ( GridListWithUnitlessGutterSize ) ;
156
133
fixture . detectChanges ( ) ;
157
134
158
- let tiles = fixture . debugElement . queryAll ( By . css ( 'mat-grid-tile' ) ) ;
135
+ const tiles = fixture . debugElement . queryAll ( By . css ( 'mat-grid-tile' ) ) ;
159
136
160
137
// check horizontal gutter
161
138
expect ( getStyle ( tiles [ 0 ] , 'width' ) ) . toBe ( '99px' ) ;
@@ -167,27 +144,27 @@ describe('MatGridList', () => {
167
144
} ) ;
168
145
169
146
it ( 'should set the correct list height in ratio mode' , ( ) => {
170
- let fixture = TestBed . createComponent ( GridListWithRatioHeightAndMulipleRows ) ;
147
+ const fixture = createComponent ( GridListWithRatioHeightAndMulipleRows ) ;
171
148
fixture . detectChanges ( ) ;
172
149
173
- let list = fixture . debugElement . query ( By . directive ( MatGridList ) ) ;
150
+ const list = fixture . debugElement . query ( By . directive ( MatGridList ) ) ;
174
151
expect ( getStyle ( list , 'padding-bottom' ) ) . toBe ( '201px' ) ;
175
152
} ) ;
176
153
177
154
it ( 'should set the correct list height in fixed mode' , ( ) => {
178
- let fixture = TestBed . createComponent ( GridListWithFixRowHeightAndMultipleRows ) ;
155
+ const fixture = createComponent ( GridListWithFixRowHeightAndMultipleRows ) ;
179
156
fixture . detectChanges ( ) ;
180
157
181
- let list = fixture . debugElement . query ( By . directive ( MatGridList ) ) ;
158
+ const list = fixture . debugElement . query ( By . directive ( MatGridList ) ) ;
182
159
expect ( getStyle ( list , 'height' ) ) . toBe ( '201px' ) ;
183
160
} ) ;
184
161
185
162
it ( 'should allow adjustment of tile colspan' , ( ) => {
186
- let fixture = TestBed . createComponent ( GridListWithColspanBinding ) ;
163
+ const fixture = createComponent ( GridListWithColspanBinding ) ;
187
164
fixture . componentInstance . colspan = 2 ;
188
165
fixture . detectChanges ( ) ;
189
166
190
- let tile = fixture . debugElement . query ( By . directive ( MatGridTile ) ) ;
167
+ const tile = fixture . debugElement . query ( By . directive ( MatGridTile ) ) ;
191
168
expect ( getStyle ( tile , 'width' ) ) . toBe ( '199.5px' ) ;
192
169
193
170
fixture . componentInstance . colspan = 3 ;
@@ -196,12 +173,12 @@ describe('MatGridList', () => {
196
173
} ) ;
197
174
198
175
it ( 'should allow adjustment of tile rowspan' , ( ) => {
199
- let fixture = TestBed . createComponent ( GridListWithRowspanBinding ) ;
176
+ const fixture = createComponent ( GridListWithRowspanBinding ) ;
200
177
201
178
fixture . componentInstance . rowspan = 2 ;
202
179
fixture . detectChanges ( ) ;
203
180
204
- let tile = fixture . debugElement . query ( By . directive ( MatGridTile ) ) ;
181
+ const tile = fixture . debugElement . query ( By . directive ( MatGridTile ) ) ;
205
182
expect ( getStyle ( tile , 'height' ) ) . toBe ( '201px' ) ;
206
183
207
184
fixture . componentInstance . rowspan = 3 ;
@@ -210,57 +187,57 @@ describe('MatGridList', () => {
210
187
} ) ;
211
188
212
189
it ( 'should lay out tiles correctly for a complex layout' , ( ) => {
213
- let fixture = TestBed . createComponent ( GridListWithComplexLayout ) ;
190
+ const fixture = createComponent ( GridListWithComplexLayout ) ;
214
191
215
- fixture . componentInstance . tiles = [
216
- { cols : 3 , rows : 1 } ,
217
- { cols : 1 , rows : 2 } ,
218
- { cols : 1 , rows : 1 } ,
219
- { cols : 2 , rows : 1 }
220
- ] ;
192
+ fixture . componentInstance . tiles = [
193
+ { cols : 3 , rows : 1 } ,
194
+ { cols : 1 , rows : 2 } ,
195
+ { cols : 1 , rows : 1 } ,
196
+ { cols : 2 , rows : 1 }
197
+ ] ;
221
198
222
- fixture . detectChanges ( ) ;
223
- let tiles = fixture . debugElement . queryAll ( By . css ( 'mat-grid-tile' ) ) ;
224
-
225
- expect ( getStyle ( tiles [ 0 ] , 'width' ) ) . toBe ( '299.75px' ) ;
226
- expect ( getStyle ( tiles [ 0 ] , 'height' ) ) . toBe ( '100px' ) ;
227
- expect ( getComputedLeft ( tiles [ 0 ] ) ) . toBe ( 0 ) ;
228
- expect ( getStyle ( tiles [ 0 ] , 'top' ) ) . toBe ( '0px' ) ;
229
-
230
- expect ( getStyle ( tiles [ 1 ] , 'width' ) ) . toBe ( '99.25px' ) ;
231
- expect ( getStyle ( tiles [ 1 ] , 'height' ) ) . toBe ( '201px' ) ;
232
- expect ( getComputedLeft ( tiles [ 1 ] ) ) . toBe ( 300.75 ) ;
233
- expect ( getStyle ( tiles [ 1 ] , 'top' ) ) . toBe ( '0px' ) ;
234
-
235
- expect ( getStyle ( tiles [ 2 ] , 'width' ) ) . toBe ( '99.25px' ) ;
236
- expect ( getStyle ( tiles [ 2 ] , 'height' ) ) . toBe ( '100px' ) ;
237
- expect ( getComputedLeft ( tiles [ 2 ] ) ) . toBe ( 0 ) ;
238
- expect ( getStyle ( tiles [ 2 ] , 'top' ) ) . toBe ( '101px' ) ;
239
-
240
- expect ( getStyle ( tiles [ 3 ] , 'width' ) ) . toBe ( '199.5px' ) ;
241
- expect ( getStyle ( tiles [ 3 ] , 'height' ) ) . toBe ( '100px' ) ;
242
- expect ( getComputedLeft ( tiles [ 3 ] ) ) . toBe ( 100.25 ) ;
243
- expect ( getStyle ( tiles [ 3 ] , 'top' ) ) . toBe ( '101px' ) ;
199
+ fixture . detectChanges ( ) ;
200
+ const tiles = fixture . debugElement . queryAll ( By . css ( 'mat-grid-tile' ) ) ;
201
+
202
+ expect ( getStyle ( tiles [ 0 ] , 'width' ) ) . toBe ( '299.75px' ) ;
203
+ expect ( getStyle ( tiles [ 0 ] , 'height' ) ) . toBe ( '100px' ) ;
204
+ expect ( getComputedLeft ( tiles [ 0 ] ) ) . toBe ( 0 ) ;
205
+ expect ( getStyle ( tiles [ 0 ] , 'top' ) ) . toBe ( '0px' ) ;
206
+
207
+ expect ( getStyle ( tiles [ 1 ] , 'width' ) ) . toBe ( '99.25px' ) ;
208
+ expect ( getStyle ( tiles [ 1 ] , 'height' ) ) . toBe ( '201px' ) ;
209
+ expect ( getComputedLeft ( tiles [ 1 ] ) ) . toBe ( 300.75 ) ;
210
+ expect ( getStyle ( tiles [ 1 ] , 'top' ) ) . toBe ( '0px' ) ;
211
+
212
+ expect ( getStyle ( tiles [ 2 ] , 'width' ) ) . toBe ( '99.25px' ) ;
213
+ expect ( getStyle ( tiles [ 2 ] , 'height' ) ) . toBe ( '100px' ) ;
214
+ expect ( getComputedLeft ( tiles [ 2 ] ) ) . toBe ( 0 ) ;
215
+ expect ( getStyle ( tiles [ 2 ] , 'top' ) ) . toBe ( '101px' ) ;
216
+
217
+ expect ( getStyle ( tiles [ 3 ] , 'width' ) ) . toBe ( '199.5px' ) ;
218
+ expect ( getStyle ( tiles [ 3 ] , 'height' ) ) . toBe ( '100px' ) ;
219
+ expect ( getComputedLeft ( tiles [ 3 ] ) ) . toBe ( 100.25 ) ;
220
+ expect ( getStyle ( tiles [ 3 ] , 'top' ) ) . toBe ( '101px' ) ;
244
221
} ) ;
245
222
246
223
it ( 'should add not add any classes to footers without lines' , ( ) => {
247
- let fixture = TestBed . createComponent ( GridListWithFootersWithoutLines ) ;
224
+ const fixture = createComponent ( GridListWithFootersWithoutLines ) ;
248
225
fixture . detectChanges ( ) ;
249
226
250
- let footer = fixture . debugElement . query ( By . directive ( MatGridTileText ) ) ;
227
+ const footer = fixture . debugElement . query ( By . directive ( MatGridTileText ) ) ;
251
228
expect ( footer . nativeElement . classList . contains ( 'mat-2-line' ) ) . toBe ( false ) ;
252
229
} ) ;
253
230
254
231
it ( 'should add class to footers with two lines' , ( ) => {
255
- let fixture = TestBed . createComponent ( GridListWithFooterContainingTwoLines ) ;
232
+ const fixture = createComponent ( GridListWithFooterContainingTwoLines ) ;
256
233
fixture . detectChanges ( ) ;
257
234
258
- let footer = fixture . debugElement . query ( By . directive ( MatGridTileText ) ) ;
235
+ const footer = fixture . debugElement . query ( By . directive ( MatGridTileText ) ) ;
259
236
expect ( footer . nativeElement . classList . contains ( 'mat-2-line' ) ) . toBe ( true ) ;
260
237
} ) ;
261
238
262
239
it ( 'should not use calc() that evaluates to 0' , ( ) => {
263
- const fixture = TestBed . createComponent ( GirdListWithRowHeightRatio ) ;
240
+ const fixture = createComponent ( GirdListWithRowHeightRatio ) ;
264
241
265
242
fixture . componentInstance . rowHeight = '4:1' ;
266
243
fixture . detectChanges ( ) ;
@@ -272,7 +249,7 @@ describe('MatGridList', () => {
272
249
} ) ;
273
250
274
251
it ( 'should reset the old styles when switching to a new tile styler' , ( ) => {
275
- const fixture = TestBed . createComponent ( GirdListWithRowHeightRatio ) ;
252
+ const fixture = createComponent ( GirdListWithRowHeightRatio ) ;
276
253
277
254
fixture . componentInstance . rowHeight = '4:1' ;
278
255
fixture . detectChanges ( ) ;
@@ -293,7 +270,7 @@ describe('MatGridList', () => {
293
270
} ) ;
294
271
295
272
it ( 'should ensure that all tiles are inside the grid when there are no matching gaps' , ( ) => {
296
- const fixture = TestBed . createComponent ( GridListWithoutMatchingGap ) ;
273
+ const fixture = createComponent ( GridListWithoutMatchingGap ) ;
297
274
const tiles = fixture . debugElement . queryAll ( By . css ( 'mat-grid-tile' ) ) ;
298
275
299
276
fixture . detectChanges ( ) ;
@@ -302,7 +279,7 @@ describe('MatGridList', () => {
302
279
} ) ;
303
280
304
281
it ( 'should default to LTR if empty directionality is given' , ( ) => {
305
- const fixture = TestBed . createComponent ( GridListWithEmptyDirectionality ) ;
282
+ const fixture = createComponent ( GridListWithEmptyDirectionality ) ;
306
283
const tile : HTMLElement = fixture . debugElement . query ( By . css ( 'mat-grid-tile' ) ) . nativeElement ;
307
284
fixture . detectChanges ( ) ;
308
285
@@ -311,7 +288,7 @@ describe('MatGridList', () => {
311
288
} ) ;
312
289
313
290
it ( 'should set `right` styles for RTL' , ( ) => {
314
- const fixture = TestBed . createComponent ( GridListWithRtl ) ;
291
+ const fixture = createComponent ( GridListWithRtl ) ;
315
292
const tile : HTMLElement = fixture . debugElement . query ( By . css ( 'mat-grid-tile' ) ) . nativeElement ;
316
293
fixture . detectChanges ( ) ;
317
294
@@ -320,7 +297,7 @@ describe('MatGridList', () => {
320
297
} ) ;
321
298
322
299
it ( 'should lay out the tiles if they are not direct descendants of the list' , ( ) => {
323
- const fixture = TestBed . createComponent ( GridListWithIndirectTileDescendants ) ;
300
+ const fixture = createComponent ( GridListWithIndirectTileDescendants ) ;
324
301
fixture . detectChanges ( ) ;
325
302
326
303
const tile = fixture . debugElement . query ( By . directive ( MatGridTile ) ) ;
@@ -340,8 +317,8 @@ function getComputedLeft(element: DebugElement): number {
340
317
// for left because iOS Safari doesn't support using `getComputedStyle` to get the calculated
341
318
// `left` value when using CSS `calc`. We subtract the `left` of the document body because
342
319
// browsers, by default, add a margin to the body (typically 8px).
343
- let elementRect = element . nativeElement . getBoundingClientRect ( ) ;
344
- let bodyRect = document . body . getBoundingClientRect ( ) ;
320
+ const elementRect = element . nativeElement . getBoundingClientRect ( ) ;
321
+ const bodyRect = document . body . getBoundingClientRect ( ) ;
345
322
346
323
return elementRect . left - bodyRect . left ;
347
324
}
0 commit comments