|
| 1 | +import {HarnessLoader} from '@angular/cdk/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 3 | +import {Component, ViewChild} from '@angular/core'; |
| 4 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
| 5 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 6 | +import {MatPaginatorModule, PageEvent, MatPaginator} from '@angular/material/paginator'; |
| 7 | +import {MatPaginatorHarness} from './paginator-harness'; |
| 8 | +import {expectAsyncError} from '@angular/cdk/testing/private'; |
| 9 | + |
| 10 | +/** Shared tests to run on both the original and MDC-based paginator. */ |
| 11 | +export function runHarnessTests( |
| 12 | + paginatorModule: typeof MatPaginatorModule, paginatorHarness: typeof MatPaginatorHarness) { |
| 13 | + let fixture: ComponentFixture<PaginatorHarnessTest>; |
| 14 | + let loader: HarnessLoader; |
| 15 | + let instance: PaginatorHarnessTest; |
| 16 | + |
| 17 | + beforeEach(async () => { |
| 18 | + await TestBed.configureTestingModule({ |
| 19 | + imports: [paginatorModule, NoopAnimationsModule], |
| 20 | + declarations: [PaginatorHarnessTest], |
| 21 | + }).compileComponents(); |
| 22 | + |
| 23 | + fixture = TestBed.createComponent(PaginatorHarnessTest); |
| 24 | + fixture.detectChanges(); |
| 25 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 26 | + instance = fixture.componentInstance; |
| 27 | + }); |
| 28 | + |
| 29 | + it('should load all paginator harnesses', async () => { |
| 30 | + const paginators = await loader.getAllHarnesses(paginatorHarness); |
| 31 | + expect(paginators.length).toBe(1); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should be able to go to the next page', async () => { |
| 35 | + const paginator = await loader.getHarness(paginatorHarness); |
| 36 | + |
| 37 | + expect(instance.pageIndex).toBe(0); |
| 38 | + await paginator.goToNextPage(); |
| 39 | + expect(instance.pageIndex).toBe(1); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should be able to go to the previous page', async () => { |
| 43 | + const paginator = await loader.getHarness(paginatorHarness); |
| 44 | + |
| 45 | + instance.pageIndex = 5; |
| 46 | + fixture.detectChanges(); |
| 47 | + |
| 48 | + await paginator.goToPreviousPage(); |
| 49 | + expect(instance.pageIndex).toBe(4); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should be able to go to the first page', async () => { |
| 53 | + const paginator = await loader.getHarness(paginatorHarness); |
| 54 | + |
| 55 | + instance.pageIndex = 5; |
| 56 | + fixture.detectChanges(); |
| 57 | + |
| 58 | + await paginator.goToFirstPage(); |
| 59 | + expect(instance.pageIndex).toBe(0); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should be able to go to the last page', async () => { |
| 63 | + const paginator = await loader.getHarness(paginatorHarness); |
| 64 | + |
| 65 | + expect(instance.pageIndex).toBe(0); |
| 66 | + await paginator.goToLastPage(); |
| 67 | + expect(instance.pageIndex).toBe(49); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should be able to set the page size', async () => { |
| 71 | + const paginator = await loader.getHarness(paginatorHarness); |
| 72 | + |
| 73 | + expect(instance.pageSize).toBe(10); |
| 74 | + await paginator.setPageSize(25); |
| 75 | + expect(instance.pageSize).toBe(25); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should be able to get the page size', async () => { |
| 79 | + const paginator = await loader.getHarness(paginatorHarness); |
| 80 | + expect(await paginator.getPageSize()).toBe(10); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should be able to get the range label', async () => { |
| 84 | + const paginator = await loader.getHarness(paginatorHarness); |
| 85 | + expect(await paginator.getRangeLabel()).toBe('1 – 10 of 500'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should throw an error if the first page button is not available', async () => { |
| 89 | + const paginator = await loader.getHarness(paginatorHarness); |
| 90 | + |
| 91 | + instance.showFirstLastButtons = false; |
| 92 | + fixture.detectChanges(); |
| 93 | + |
| 94 | + await expectAsyncError(() => paginator.goToFirstPage(), |
| 95 | + /Error: Could not find first page button inside paginator/); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should throw an error if the last page button is not available', async () => { |
| 99 | + const paginator = await loader.getHarness(paginatorHarness); |
| 100 | + |
| 101 | + instance.showFirstLastButtons = false; |
| 102 | + fixture.detectChanges(); |
| 103 | + |
| 104 | + await expectAsyncError(() => paginator.goToLastPage(), |
| 105 | + /Error: Could not find last page button inside paginator/); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should throw an error if the page size selector is not available', async () => { |
| 109 | + const paginator = await loader.getHarness(paginatorHarness); |
| 110 | + |
| 111 | + instance.pageSizeOptions = []; |
| 112 | + fixture.detectChanges(); |
| 113 | + |
| 114 | + await expectAsyncError(() => paginator.setPageSize(10), |
| 115 | + /Error: Cannot find page size selector in paginator/); |
| 116 | + }); |
| 117 | +} |
| 118 | + |
| 119 | +@Component({ |
| 120 | + template: ` |
| 121 | + <mat-paginator |
| 122 | + (page)="handlePageEvent($event)" |
| 123 | + [length]="length" |
| 124 | + [pageSize]="pageSize" |
| 125 | + [showFirstLastButtons]="showFirstLastButtons" |
| 126 | + [pageSizeOptions]="pageSizeOptions" |
| 127 | + [pageIndex]="pageIndex"> |
| 128 | + </mat-paginator> |
| 129 | + ` |
| 130 | +}) |
| 131 | +class PaginatorHarnessTest { |
| 132 | + @ViewChild(MatPaginator) paginator: MatPaginator; |
| 133 | + length = 500; |
| 134 | + pageSize = 10; |
| 135 | + pageIndex = 0; |
| 136 | + pageSizeOptions = [5, 10, 25]; |
| 137 | + showFirstLastButtons = true; |
| 138 | + |
| 139 | + handlePageEvent(event: PageEvent) { |
| 140 | + this.length = event.length; |
| 141 | + this.pageSize = event.pageSize; |
| 142 | + this.pageIndex = event.pageIndex; |
| 143 | + } |
| 144 | +} |
| 145 | + |
0 commit comments