|
| 1 | +/* tslint:disable:no-unused-variable */ |
| 2 | + |
| 3 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 4 | +import { HttpClientModule } from '@angular/common/http'; |
| 5 | +import { NO_ERRORS_SCHEMA, SecurityContext } from '@angular/core'; |
| 6 | +import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; |
| 7 | +import { DataTableDirective, DataTablesModule } from 'angular-datatables'; |
| 8 | +import { MarkdownModule } from 'ngx-markdown'; |
| 9 | +import { BaseDemoComponent } from '../base-demo/base-demo.component'; |
| 10 | +import { AppRoutingModule } from '../app.routing'; |
| 11 | +import { FormsModule } from '@angular/forms'; |
| 12 | +import { RerenderComponent } from './rerender.component'; |
| 13 | + |
| 14 | + |
| 15 | +let fixture: ComponentFixture<RerenderComponent>, component: RerenderComponent = null; |
| 16 | + |
| 17 | +describe('RerenderComponent', () => { |
| 18 | + beforeEach(() => { |
| 19 | + fixture = TestBed.configureTestingModule({ |
| 20 | + declarations: [ |
| 21 | + BaseDemoComponent, |
| 22 | + RerenderComponent, |
| 23 | + DataTableDirective |
| 24 | + ], |
| 25 | + imports: [ |
| 26 | + AppRoutingModule, |
| 27 | + RouterTestingModule, |
| 28 | + DataTablesModule.forRoot(), |
| 29 | + HttpClientModule, |
| 30 | + MarkdownModule.forRoot( |
| 31 | + { |
| 32 | + sanitize: SecurityContext.NONE |
| 33 | + } |
| 34 | + ), |
| 35 | + FormsModule |
| 36 | + ], |
| 37 | + schemas: [NO_ERRORS_SCHEMA] |
| 38 | + }).createComponent(RerenderComponent); |
| 39 | + |
| 40 | + component = fixture.componentInstance; |
| 41 | + |
| 42 | + fixture.detectChanges(); // initial binding |
| 43 | + }); |
| 44 | + |
| 45 | + it('should create the app', waitForAsync(() => { |
| 46 | + const app = fixture.debugElement.componentInstance; |
| 47 | + expect(app).toBeTruthy(); |
| 48 | + })); |
| 49 | + |
| 50 | + it('should have title "Rerender"', waitForAsync(() => { |
| 51 | + const app = fixture.debugElement.componentInstance as RerenderComponent; |
| 52 | + expect(app.pageTitle).toBe('Rerender'); |
| 53 | + })); |
| 54 | + |
| 55 | + it('should recreate table when Rerender is clicked', async () => { |
| 56 | + const app = fixture.componentInstance as RerenderComponent; |
| 57 | + await fixture.whenStable(); |
| 58 | + |
| 59 | + const rerenderSpy = spyOn(app, 'rerender' as any); |
| 60 | + |
| 61 | + const triggerBtns: HTMLButtonElement[] = Array.from(fixture.nativeElement.querySelectorAll('button')); |
| 62 | + const triggerBtn: HTMLButtonElement = triggerBtns.find(e => e.textContent.includes('Rerender')); |
| 63 | + |
| 64 | + triggerBtn.click(); |
| 65 | + triggerBtn.dispatchEvent(new Event('click')); |
| 66 | + |
| 67 | + expect(rerenderSpy).toHaveBeenCalled(); |
| 68 | + }); |
| 69 | + |
| 70 | +}); |
0 commit comments