Skip to content

Commit f8c9a7d

Browse files
crisbetojelbourn
authored andcommitted
test(grid-list): avoid recompiling all fixtures for each test (#12896)
Reworks the grid list to avoid having to recompile all the other fixture for each test. This shaves of a few seconds of the test runs. Also does some minor cleanup.
1 parent dc8cf46 commit f8c9a7d

File tree

1 file changed

+76
-99
lines changed

1 file changed

+76
-99
lines changed

src/lib/grid-list/grid-list.spec.ts

Lines changed: 76 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,55 @@
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';
33
import {By} from '@angular/platform-browser';
44
import {MatGridList, MatGridListModule} from './index';
55
import {MatGridTile, MatGridTileText} from './grid-tile';
66
import {Directionality} from '@angular/cdk/bidi';
77

88

99
describe('MatGridList', () => {
10-
beforeEach(async(() => {
10+
function createComponent<T>(componentType: Type<T>): ComponentFixture<T> {
1111
TestBed.configureTestingModule({
1212
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+
}
4118

4219
it('should throw error if cols is not defined', () => {
43-
let fixture = TestBed.createComponent(GridListWithoutCols);
20+
const fixture = createComponent(GridListWithoutCols);
4421

4522
expect(() => fixture.detectChanges()).toThrowError(/must pass in number of columns/);
4623
});
4724

4825
it('should throw error if rowHeight ratio is invalid', () => {
49-
let fixture = TestBed.createComponent(GridListWithInvalidRowHeightRatio);
26+
const fixture = createComponent(GridListWithInvalidRowHeightRatio);
5027

5128
expect(() => fixture.detectChanges()).toThrowError(/invalid ratio given for row-height/);
5229
});
5330

5431
it('should throw error if tile colspan is wider than total cols', () => {
55-
let fixture = TestBed.createComponent(GridListWithTooWideColspan);
32+
const fixture = createComponent(GridListWithTooWideColspan);
5633

5734
expect(() => fixture.detectChanges()).toThrowError(/tile with colspan 5 is wider than grid/);
5835
});
5936

6037
it('should default to 1:1 row height if undefined ', () => {
61-
let fixture = TestBed.createComponent(GridListWithUnspecifiedRowHeight);
38+
const fixture = createComponent(GridListWithUnspecifiedRowHeight);
6239
fixture.detectChanges();
63-
let tile = fixture.debugElement.query(By.directive(MatGridTile));
40+
const tile = fixture.debugElement.query(By.directive(MatGridTile));
6441

6542
// In ratio mode, heights are set using the padding-top property.
6643
expect(getStyle(tile, 'padding-top')).toBe('200px');
6744
});
6845

6946
it('should use a ratio row height if passed in', () => {
70-
let fixture = TestBed.createComponent(GirdListWithRowHeightRatio);
47+
const fixture = createComponent(GirdListWithRowHeightRatio);
7148

7249
fixture.componentInstance.rowHeight = '4:1';
7350
fixture.detectChanges();
7451

75-
let tile = fixture.debugElement.query(By.directive(MatGridTile));
52+
const tile = fixture.debugElement.query(By.directive(MatGridTile));
7653
expect(getStyle(tile, 'padding-top')).toBe('100px');
7754

7855
fixture.componentInstance.rowHeight = '2:1';
@@ -82,11 +59,11 @@ describe('MatGridList', () => {
8259
});
8360

8461
it('should divide row height evenly in "fit" mode', () => {
85-
let fixture = TestBed.createComponent(GridListWithFitRowHeightMode);
62+
const fixture = createComponent(GridListWithFitRowHeightMode);
8663

8764
fixture.componentInstance.totalHeight = '300px';
8865
fixture.detectChanges();
89-
let tile = fixture.debugElement.query(By.directive(MatGridTile));
66+
const tile = fixture.debugElement.query(By.directive(MatGridTile));
9067

9168
// 149.5 * 2 = 299px + 1px gutter = 300px
9269
expect(getStyle(tile, 'height')).toBe('149.5px');
@@ -99,12 +76,12 @@ describe('MatGridList', () => {
9976
});
10077

10178
it('should use the fixed row height if passed in', () => {
102-
let fixture = TestBed.createComponent(GridListWithFixedRowHeightMode);
79+
const fixture = createComponent(GridListWithFixedRowHeightMode);
10380

10481
fixture.componentInstance.rowHeight = '100px';
10582
fixture.detectChanges();
10683

107-
let tile = fixture.debugElement.query(By.directive(MatGridTile));
84+
const tile = fixture.debugElement.query(By.directive(MatGridTile));
10885
expect(getStyle(tile, 'height')).toBe('100px');
10986

11087
fixture.componentInstance.rowHeight = '200px';
@@ -114,18 +91,18 @@ describe('MatGridList', () => {
11491
});
11592

11693
it('should default to pixels if row height units are missing', () => {
117-
let fixture = TestBed.createComponent(GridListWithUnitlessFixedRowHeight);
94+
const fixture = createComponent(GridListWithUnitlessFixedRowHeight);
11895
fixture.detectChanges();
11996

120-
let tile = fixture.debugElement.query(By.directive(MatGridTile));
97+
const tile = fixture.debugElement.query(By.directive(MatGridTile));
12198
expect(getStyle(tile, 'height')).toBe('100px');
12299
});
123100

124101
it('should default gutter size to 1px', () => {
125-
let fixture = TestBed.createComponent(GridListWithUnspecifiedGutterSize);
102+
const fixture = createComponent(GridListWithUnspecifiedGutterSize);
126103
fixture.detectChanges();
127104

128-
let tiles = fixture.debugElement.queryAll(By.css('mat-grid-tile'));
105+
const tiles = fixture.debugElement.queryAll(By.css('mat-grid-tile'));
129106

130107
// check horizontal gutter
131108
expect(getStyle(tiles[0], 'width')).toBe('99.5px');
@@ -137,10 +114,10 @@ describe('MatGridList', () => {
137114
});
138115

139116
it('should set the gutter size if passed', () => {
140-
let fixture = TestBed.createComponent(GridListWithGutterSize);
117+
const fixture = createComponent(GridListWithGutterSize);
141118
fixture.detectChanges();
142119

143-
let tiles = fixture.debugElement.queryAll(By.css('mat-grid-tile'));
120+
const tiles = fixture.debugElement.queryAll(By.css('mat-grid-tile'));
144121

145122
// check horizontal gutter
146123
expect(getStyle(tiles[0], 'width')).toBe('99px');
@@ -152,10 +129,10 @@ describe('MatGridList', () => {
152129
});
153130

154131
it('should use pixels if gutter units are missing', () => {
155-
let fixture = TestBed.createComponent(GridListWithUnitlessGutterSize);
132+
const fixture = createComponent(GridListWithUnitlessGutterSize);
156133
fixture.detectChanges();
157134

158-
let tiles = fixture.debugElement.queryAll(By.css('mat-grid-tile'));
135+
const tiles = fixture.debugElement.queryAll(By.css('mat-grid-tile'));
159136

160137
// check horizontal gutter
161138
expect(getStyle(tiles[0], 'width')).toBe('99px');
@@ -167,27 +144,27 @@ describe('MatGridList', () => {
167144
});
168145

169146
it('should set the correct list height in ratio mode', () => {
170-
let fixture = TestBed.createComponent(GridListWithRatioHeightAndMulipleRows);
147+
const fixture = createComponent(GridListWithRatioHeightAndMulipleRows);
171148
fixture.detectChanges();
172149

173-
let list = fixture.debugElement.query(By.directive(MatGridList));
150+
const list = fixture.debugElement.query(By.directive(MatGridList));
174151
expect(getStyle(list, 'padding-bottom')).toBe('201px');
175152
});
176153

177154
it('should set the correct list height in fixed mode', () => {
178-
let fixture = TestBed.createComponent(GridListWithFixRowHeightAndMultipleRows);
155+
const fixture = createComponent(GridListWithFixRowHeightAndMultipleRows);
179156
fixture.detectChanges();
180157

181-
let list = fixture.debugElement.query(By.directive(MatGridList));
158+
const list = fixture.debugElement.query(By.directive(MatGridList));
182159
expect(getStyle(list, 'height')).toBe('201px');
183160
});
184161

185162
it('should allow adjustment of tile colspan', () => {
186-
let fixture = TestBed.createComponent(GridListWithColspanBinding);
163+
const fixture = createComponent(GridListWithColspanBinding);
187164
fixture.componentInstance.colspan = 2;
188165
fixture.detectChanges();
189166

190-
let tile = fixture.debugElement.query(By.directive(MatGridTile));
167+
const tile = fixture.debugElement.query(By.directive(MatGridTile));
191168
expect(getStyle(tile, 'width')).toBe('199.5px');
192169

193170
fixture.componentInstance.colspan = 3;
@@ -196,12 +173,12 @@ describe('MatGridList', () => {
196173
});
197174

198175
it('should allow adjustment of tile rowspan', () => {
199-
let fixture = TestBed.createComponent(GridListWithRowspanBinding);
176+
const fixture = createComponent(GridListWithRowspanBinding);
200177

201178
fixture.componentInstance.rowspan = 2;
202179
fixture.detectChanges();
203180

204-
let tile = fixture.debugElement.query(By.directive(MatGridTile));
181+
const tile = fixture.debugElement.query(By.directive(MatGridTile));
205182
expect(getStyle(tile, 'height')).toBe('201px');
206183

207184
fixture.componentInstance.rowspan = 3;
@@ -210,57 +187,57 @@ describe('MatGridList', () => {
210187
});
211188

212189
it('should lay out tiles correctly for a complex layout', () => {
213-
let fixture = TestBed.createComponent(GridListWithComplexLayout);
190+
const fixture = createComponent(GridListWithComplexLayout);
214191

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+
];
221198

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');
244221
});
245222

246223
it('should add not add any classes to footers without lines', () => {
247-
let fixture = TestBed.createComponent(GridListWithFootersWithoutLines);
224+
const fixture = createComponent(GridListWithFootersWithoutLines);
248225
fixture.detectChanges();
249226

250-
let footer = fixture.debugElement.query(By.directive(MatGridTileText));
227+
const footer = fixture.debugElement.query(By.directive(MatGridTileText));
251228
expect(footer.nativeElement.classList.contains('mat-2-line')).toBe(false);
252229
});
253230

254231
it('should add class to footers with two lines', () => {
255-
let fixture = TestBed.createComponent(GridListWithFooterContainingTwoLines);
232+
const fixture = createComponent(GridListWithFooterContainingTwoLines);
256233
fixture.detectChanges();
257234

258-
let footer = fixture.debugElement.query(By.directive(MatGridTileText));
235+
const footer = fixture.debugElement.query(By.directive(MatGridTileText));
259236
expect(footer.nativeElement.classList.contains('mat-2-line')).toBe(true);
260237
});
261238

262239
it('should not use calc() that evaluates to 0', () => {
263-
const fixture = TestBed.createComponent(GirdListWithRowHeightRatio);
240+
const fixture = createComponent(GirdListWithRowHeightRatio);
264241

265242
fixture.componentInstance.rowHeight = '4:1';
266243
fixture.detectChanges();
@@ -272,7 +249,7 @@ describe('MatGridList', () => {
272249
});
273250

274251
it('should reset the old styles when switching to a new tile styler', () => {
275-
const fixture = TestBed.createComponent(GirdListWithRowHeightRatio);
252+
const fixture = createComponent(GirdListWithRowHeightRatio);
276253

277254
fixture.componentInstance.rowHeight = '4:1';
278255
fixture.detectChanges();
@@ -293,7 +270,7 @@ describe('MatGridList', () => {
293270
});
294271

295272
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);
297274
const tiles = fixture.debugElement.queryAll(By.css('mat-grid-tile'));
298275

299276
fixture.detectChanges();
@@ -302,7 +279,7 @@ describe('MatGridList', () => {
302279
});
303280

304281
it('should default to LTR if empty directionality is given', () => {
305-
const fixture = TestBed.createComponent(GridListWithEmptyDirectionality);
282+
const fixture = createComponent(GridListWithEmptyDirectionality);
306283
const tile: HTMLElement = fixture.debugElement.query(By.css('mat-grid-tile')).nativeElement;
307284
fixture.detectChanges();
308285

@@ -311,7 +288,7 @@ describe('MatGridList', () => {
311288
});
312289

313290
it('should set `right` styles for RTL', () => {
314-
const fixture = TestBed.createComponent(GridListWithRtl);
291+
const fixture = createComponent(GridListWithRtl);
315292
const tile: HTMLElement = fixture.debugElement.query(By.css('mat-grid-tile')).nativeElement;
316293
fixture.detectChanges();
317294

@@ -320,7 +297,7 @@ describe('MatGridList', () => {
320297
});
321298

322299
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);
324301
fixture.detectChanges();
325302

326303
const tile = fixture.debugElement.query(By.directive(MatGridTile));
@@ -340,8 +317,8 @@ function getComputedLeft(element: DebugElement): number {
340317
// for left because iOS Safari doesn't support using `getComputedStyle` to get the calculated
341318
// `left` value when using CSS `calc`. We subtract the `left` of the document body because
342319
// 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();
345322

346323
return elementRect.left - bodyRect.left;
347324
}

0 commit comments

Comments
 (0)