|
| 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 { By } from '@angular/platform-browser'; |
| 12 | +import { FormsModule } from '@angular/forms'; |
| 13 | +import { IndividualColumnFilteringComponent } from './individual-column-filtering.component'; |
| 14 | + |
| 15 | + |
| 16 | +let fixture: ComponentFixture<IndividualColumnFilteringComponent>, component: IndividualColumnFilteringComponent = null; |
| 17 | + |
| 18 | +function applyValueToInput(inputElement: HTMLInputElement, value: string, table: DataTables.Api) { |
| 19 | + inputElement.value = value; |
| 20 | + inputElement.dispatchEvent(new Event('input')); |
| 21 | + inputElement.dispatchEvent(new Event('change')); |
| 22 | + table.draw(); |
| 23 | +} |
| 24 | + |
| 25 | +describe('IndividualColumnFilteringComponent', () => { |
| 26 | + beforeEach(() => { |
| 27 | + fixture = TestBed.configureTestingModule({ |
| 28 | + declarations: [ |
| 29 | + BaseDemoComponent, |
| 30 | + IndividualColumnFilteringComponent, |
| 31 | + DataTableDirective |
| 32 | + ], |
| 33 | + imports: [ |
| 34 | + AppRoutingModule, |
| 35 | + RouterTestingModule, |
| 36 | + DataTablesModule.forRoot(), |
| 37 | + HttpClientModule, |
| 38 | + MarkdownModule.forRoot( |
| 39 | + { |
| 40 | + sanitize: SecurityContext.NONE |
| 41 | + } |
| 42 | + ), |
| 43 | + FormsModule |
| 44 | + ], |
| 45 | + schemas: [NO_ERRORS_SCHEMA] |
| 46 | + }).createComponent(IndividualColumnFilteringComponent); |
| 47 | + |
| 48 | + component = fixture.componentInstance; |
| 49 | + |
| 50 | + fixture.detectChanges(); // initial binding |
| 51 | + }); |
| 52 | + |
| 53 | + it('should create the app', waitForAsync(() => { |
| 54 | + const app = fixture.debugElement.componentInstance; |
| 55 | + expect(app).toBeTruthy(); |
| 56 | + })); |
| 57 | + |
| 58 | + it('should have title "Individual column searching"', waitForAsync(() => { |
| 59 | + const app = fixture.debugElement.componentInstance as IndividualColumnFilteringComponent; |
| 60 | + expect(app.pageTitle).toBe('Individual column searching'); |
| 61 | + })); |
| 62 | + |
| 63 | + it('should filter contents acc. to column', async () => { |
| 64 | + const app = fixture.componentInstance as IndividualColumnFilteringComponent; |
| 65 | + app.dtOptions.paging = false; |
| 66 | + |
| 67 | + await fixture.whenStable(); |
| 68 | + |
| 69 | + const query = fixture.debugElement.query(By.directive(DataTableDirective)); |
| 70 | + const dir = query.injector.get(DataTableDirective); |
| 71 | + expect(dir).toBeTruthy(); |
| 72 | + |
| 73 | + const instance = await dir.dtInstance; |
| 74 | + |
| 75 | + const inputFields = Array.from(fixture.nativeElement.querySelectorAll('input')) as HTMLInputElement[]; |
| 76 | + const inputFieldID = inputFields.find(e => e.name == "search-id"); |
| 77 | + const inputFieldFirstName = inputFields.find(e => e.name == "search-first-name"); |
| 78 | + const inputFieldLastName = inputFields.find(e => e.name == "search-last-name"); |
| 79 | + |
| 80 | + // # Test 1 |
| 81 | + applyValueToInput(inputFieldID, '113', instance); |
| 82 | + expect(instance.rows({ page: 'current' }).count()).toBe(1); |
| 83 | + |
| 84 | + // # Test 2 |
| 85 | + |
| 86 | + // reset prev. field |
| 87 | + applyValueToInput(inputFieldID, '', instance); |
| 88 | + applyValueToInput(inputFieldFirstName, 'Batman', instance); |
| 89 | + expect(instance.rows({ page: 'current' }).count()).toBe(30); |
| 90 | + |
| 91 | + // # Test 3 |
| 92 | + // reset prev. field |
| 93 | + applyValueToInput(inputFieldFirstName, '', instance); |
| 94 | + applyValueToInput(inputFieldLastName, 'Titi', instance); |
| 95 | + expect(instance.rows({ page: 'current' }).count()).toBe(28); |
| 96 | + |
| 97 | + }); |
| 98 | + |
| 99 | +}); |
0 commit comments