|
| 1 | +import {Component} from '@angular/core'; |
| 2 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 3 | +import {HarnessLoader, parallel} from '@angular/cdk/testing'; |
| 4 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 5 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
1 | 6 | import {MatGridListModule} from '@angular/material/grid-list';
|
2 | 7 | import {MatGridListHarness} from './grid-list-harness';
|
3 | 8 | import {MatGridTileHarness} from './grid-tile-harness';
|
4 |
| -import {runHarnessTests} from './shared.spec'; |
5 | 9 |
|
6 |
| -describe('Non-MDC-based MatGridListHarness', () => { |
7 |
| - runHarnessTests(MatGridListModule, MatGridListHarness, MatGridTileHarness); |
| 10 | +describe('MatGridListHarness', () => { |
| 11 | + let fixture: ComponentFixture<GridListHarnessTest>; |
| 12 | + let loader: HarnessLoader; |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + await TestBed.configureTestingModule({ |
| 16 | + imports: [MatGridListModule, NoopAnimationsModule], |
| 17 | + declarations: [GridListHarnessTest], |
| 18 | + }).compileComponents(); |
| 19 | + |
| 20 | + fixture = TestBed.createComponent(GridListHarnessTest); |
| 21 | + fixture.detectChanges(); |
| 22 | + loader = TestbedHarnessEnvironment.documentRootLoader(fixture); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should be able to load grid-list harnesses', async () => { |
| 26 | + const harnesses = await loader.getAllHarnesses(MatGridListHarness); |
| 27 | + expect(harnesses.length).toBe(2); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should be able to load grid-tile harnesses', async () => { |
| 31 | + const harnesses = await loader.getAllHarnesses(MatGridTileHarness); |
| 32 | + expect(harnesses.length).toBe(8); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should be able to load grid-tile harness by header text', async () => { |
| 36 | + const harnesses = await loader.getAllHarnesses(MatGridTileHarness.with({headerText: /Tile 3/})); |
| 37 | + expect(harnesses.length).toBe(1); |
| 38 | + expect(await harnesses[0].getFooterText()).toBe('Tile 3 footer'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should be able to load grid-tile harness by footer text', async () => { |
| 42 | + const harnesses = await loader.getAllHarnesses( |
| 43 | + MatGridTileHarness.with({footerText: 'Tile 3 footer'}), |
| 44 | + ); |
| 45 | + expect(harnesses.length).toBe(1); |
| 46 | + expect(await harnesses[0].getHeaderText()).toBe('Tile 3'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should be able to get all tiles of a grid-list', async () => { |
| 50 | + const gridList = await loader.getHarness(MatGridListHarness.with({selector: '#second'})); |
| 51 | + const tiles = await gridList.getTiles(); |
| 52 | + expect(tiles.length).toBe(4); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should be able to get tiles of grid-list with filters', async () => { |
| 56 | + const gridList = await loader.getHarness(MatGridListHarness.with({selector: '#second'})); |
| 57 | + const tiles = await gridList.getTiles({headerText: /Tile [34]/}); |
| 58 | + expect(tiles.length).toBe(2); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should be able to get amount of columns of grid-list', async () => { |
| 62 | + const gridLists = await loader.getAllHarnesses(MatGridListHarness); |
| 63 | + expect(await gridLists[0].getColumns()).toBe(4); |
| 64 | + expect(await gridLists[1].getColumns()).toBe(2); |
| 65 | + fixture.componentInstance.columns = 3; |
| 66 | + expect(await gridLists[0].getColumns()).toBe(3); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should be able to get tile by position', async () => { |
| 70 | + const gridList = await loader.getHarness(MatGridListHarness); |
| 71 | + const tiles = await parallel(() => [ |
| 72 | + gridList.getTileAtPosition({row: 0, column: 0}), |
| 73 | + gridList.getTileAtPosition({row: 0, column: 1}), |
| 74 | + gridList.getTileAtPosition({row: 1, column: 0}), |
| 75 | + ]); |
| 76 | + expect(await tiles[0].getHeaderText()).toBe('One'); |
| 77 | + expect(await tiles[1].getHeaderText()).toBe('Two'); |
| 78 | + expect(await tiles[2].getHeaderText()).toBe('Four (second row)'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should be able to get tile by position with respect to tile span', async () => { |
| 82 | + const gridList = await loader.getHarness(MatGridListHarness); |
| 83 | + const tiles = await parallel(() => [ |
| 84 | + gridList.getTileAtPosition({row: 0, column: 2}), |
| 85 | + gridList.getTileAtPosition({row: 0, column: 3}), |
| 86 | + ]); |
| 87 | + expect(await tiles[0].getHeaderText()).toBe('Three'); |
| 88 | + expect(await tiles[1].getHeaderText()).toBe('Three'); |
| 89 | + await expectAsync(gridList.getTileAtPosition({row: 2, column: 0})).toBeRejectedWithError( |
| 90 | + /Could not find tile/, |
| 91 | + ); |
| 92 | + |
| 93 | + // Update the fourth tile to span over two rows. The previous position |
| 94 | + // should now be valid and the fourth tile should be returned. |
| 95 | + fixture.componentInstance.tile4Rowspan = 2; |
| 96 | + const foundTile = await gridList.getTileAtPosition({row: 2, column: 0}); |
| 97 | + expect(await foundTile.getHeaderText()).toBe('Four (second and third row)'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should be able to check whether tile has header', async () => { |
| 101 | + const tiles = await loader.getAllHarnesses(MatGridTileHarness); |
| 102 | + expect(await tiles[0].hasHeader()).toBe(true); |
| 103 | + expect(await tiles[4].hasHeader()).toBe(false); |
| 104 | + expect(await (await tiles[4].host()).text()).toBe('Tile 1 (no header, no footer)'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should be able to check whether tile has footer', async () => { |
| 108 | + const tiles = await loader.getAllHarnesses(MatGridTileHarness); |
| 109 | + expect(await tiles[0].hasFooter()).toBe(false); |
| 110 | + expect(await tiles[6].hasFooter()).toBe(true); |
| 111 | + expect(await tiles[6].getFooterText()).toBe('Tile 3 footer'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should be able to check whether tile has avatar', async () => { |
| 115 | + const tiles = await loader.getAllHarnesses(MatGridTileHarness); |
| 116 | + expect(await tiles[0].hasAvatar()).toBe(false); |
| 117 | + expect(await tiles[1].hasAvatar()).toBe(true); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should be able to get rowspan of tile', async () => { |
| 121 | + const tiles = await loader.getAllHarnesses(MatGridTileHarness); |
| 122 | + expect(await tiles[0].getRowspan()).toBe(1); |
| 123 | + expect(await tiles[3].getRowspan()).toBe(1); |
| 124 | + fixture.componentInstance.tile4Rowspan = 10; |
| 125 | + expect(await tiles[3].getRowspan()).toBe(10); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should be able to get colspan of tile', async () => { |
| 129 | + const tiles = await loader.getAllHarnesses(MatGridTileHarness); |
| 130 | + expect(await tiles[0].getColspan()).toBe(1); |
| 131 | + expect(await tiles[2].getColspan()).toBe(2); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should be able to get header text of tile', async () => { |
| 135 | + const tiles = await loader.getAllHarnesses(MatGridTileHarness); |
| 136 | + expect(await tiles[0].getHeaderText()).toBe('One'); |
| 137 | + fixture.componentInstance.firstTileText = 'updated'; |
| 138 | + expect(await tiles[0].getHeaderText()).toBe('updated'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should be able to get footer text of tile', async () => { |
| 142 | + const tiles = await loader.getAllHarnesses(MatGridTileHarness); |
| 143 | + expect(await tiles[0].getFooterText()).toBe(null); |
| 144 | + fixture.componentInstance.showFooter = true; |
| 145 | + expect(await tiles[0].getFooterText()).toBe('Footer'); |
| 146 | + }); |
8 | 147 | });
|
| 148 | + |
| 149 | +@Component({ |
| 150 | + template: ` |
| 151 | + <mat-grid-list [cols]="columns"> |
| 152 | + <mat-grid-tile> |
| 153 | + <mat-grid-tile-header>{{firstTileText}}</mat-grid-tile-header> |
| 154 | + <mat-grid-tile-footer *ngIf="showFooter">Footer</mat-grid-tile-footer> |
| 155 | + </mat-grid-tile> |
| 156 | + <mat-grid-tile> |
| 157 | + <mat-grid-tile-header>Two</mat-grid-tile-header> |
| 158 | + <div matGridAvatar class="css-rendered-avatar"></div> |
| 159 | + </mat-grid-tile> |
| 160 | + <mat-grid-tile colspan="2"> |
| 161 | + <mat-grid-tile-header>Three</mat-grid-tile-header> |
| 162 | + </mat-grid-tile> |
| 163 | + <mat-grid-tile [rowspan]="tile4Rowspan"> |
| 164 | + <mat-grid-tile-header> |
| 165 | + Four (second {{tile4Rowspan === 2 ? 'and third ' : ''}}row) |
| 166 | + </mat-grid-tile-header> |
| 167 | + </mat-grid-tile> |
| 168 | + </mat-grid-list> |
| 169 | +
|
| 170 | + <mat-grid-list id="second" cols="2" rowHeight="100px"> |
| 171 | + <mat-grid-tile>Tile 1 (no header, no footer)</mat-grid-tile> |
| 172 | + <mat-grid-tile> |
| 173 | + <mat-grid-tile-header>Tile 2</mat-grid-tile-header> |
| 174 | + </mat-grid-tile> |
| 175 | + <mat-grid-tile colspan="2"> |
| 176 | + <mat-grid-tile-header>Tile 3</mat-grid-tile-header> |
| 177 | + <mat-grid-tile-footer>Tile 3 footer</mat-grid-tile-footer> |
| 178 | + </mat-grid-tile> |
| 179 | + <mat-grid-tile> |
| 180 | + <mat-grid-tile-header>Tile 4</mat-grid-tile-header> |
| 181 | + </mat-grid-tile> |
| 182 | + </mat-grid-list> |
| 183 | + `, |
| 184 | +}) |
| 185 | +class GridListHarnessTest { |
| 186 | + firstTileText = 'One'; |
| 187 | + showFooter = false; |
| 188 | + columns = 4; |
| 189 | + tile4Rowspan = 1; |
| 190 | +} |
0 commit comments