|
| 1 | +import { |
| 2 | + ComponentFixture, |
| 3 | + fakeAsync, |
| 4 | + TestBed, |
| 5 | + tick, |
| 6 | + waitForAsync, |
| 7 | +} from '@angular/core/testing'; |
| 8 | +import { FormsModule } from '@angular/forms'; |
| 9 | +import { By } from '@angular/platform-browser'; |
| 10 | +import { |
| 11 | + TranslateLoader, |
| 12 | + TranslateModule, |
| 13 | +} from '@ngx-translate/core'; |
| 14 | + |
| 15 | +import { TranslateLoaderMock } from '../../../../../shared/mocks/translate-loader.mock'; |
| 16 | +import { IntegerValueInputComponent } from './integer-value-input.component'; |
| 17 | + |
| 18 | +describe('IntegerValueInputComponent', () => { |
| 19 | + let component: IntegerValueInputComponent; |
| 20 | + let fixture: ComponentFixture<IntegerValueInputComponent>; |
| 21 | + |
| 22 | + beforeEach(waitForAsync(() => { |
| 23 | + TestBed.configureTestingModule({ |
| 24 | + imports: [ |
| 25 | + FormsModule, |
| 26 | + TranslateModule.forRoot({ |
| 27 | + loader: { |
| 28 | + provide: TranslateLoader, |
| 29 | + useClass: TranslateLoaderMock, |
| 30 | + }, |
| 31 | + }), |
| 32 | + IntegerValueInputComponent, |
| 33 | + ], |
| 34 | + providers: [], |
| 35 | + }) |
| 36 | + .compileComponents(); |
| 37 | + })); |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + fixture = TestBed.createComponent(IntegerValueInputComponent); |
| 41 | + component = fixture.componentInstance; |
| 42 | + fixture.detectChanges(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should create', () => { |
| 46 | + expect(component).toBeTruthy(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should not show a validation error if the input field was left untouched but left empty', () => { |
| 50 | + const validationError = fixture.debugElement.query(By.css('.validation-error')); |
| 51 | + expect(validationError).toBeFalsy(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should show a validation error if the input field was touched but left empty', fakeAsync(() => { |
| 55 | + component.value = undefined; |
| 56 | + fixture.detectChanges(); |
| 57 | + tick(); |
| 58 | + |
| 59 | + const input = fixture.debugElement.query(By.css('input')); |
| 60 | + input.triggerEventHandler('blur', null); |
| 61 | + |
| 62 | + fixture.detectChanges(); |
| 63 | + |
| 64 | + const validationError = fixture.debugElement.query(By.css('.validation-error')); |
| 65 | + expect(validationError).toBeTruthy(); |
| 66 | + })); |
| 67 | + |
| 68 | + it('should not show a validation error if the input field was touched but not left empty', fakeAsync(() => { |
| 69 | + component.value = 1; |
| 70 | + fixture.detectChanges(); |
| 71 | + tick(); |
| 72 | + |
| 73 | + const input = fixture.debugElement.query(By.css('input')); |
| 74 | + input.triggerEventHandler('blur', null); |
| 75 | + |
| 76 | + fixture.detectChanges(); |
| 77 | + |
| 78 | + const validationError = fixture.debugElement.query(By.css('.validation-error')); |
| 79 | + expect(validationError).toBeFalsy(); |
| 80 | + })); |
| 81 | +}); |
0 commit comments