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