|
| 1 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 2 | +import { BookStaticAsyncDataService } from './../shared/book-static-async-data.service'; |
| 3 | +import { BookDataService } from '../shared/book-data.service'; |
| 4 | +import { ComponentFixture, TestBed, async, fakeAsync, tick } from '@angular/core/testing'; |
| 5 | + |
| 6 | +import { BookEditComponent } from './book-edit.component'; |
| 7 | +import { DebugElement } from '@angular/core'; |
| 8 | +import { FormsModule } from '@angular/forms'; |
| 9 | + |
| 10 | + |
| 11 | +describe('BookEditComponent', () => { |
| 12 | + let component: BookEditComponent; |
| 13 | + let fixture: ComponentFixture<BookEditComponent>; |
| 14 | + let compiled; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + TestBed.configureTestingModule({ |
| 18 | + declarations: [ |
| 19 | + BookEditComponent |
| 20 | + ], |
| 21 | + imports: [ |
| 22 | + FormsModule, |
| 23 | + RouterTestingModule.withRoutes([]) |
| 24 | + ], |
| 25 | + providers: [{ provide: BookDataService, useClass: BookStaticAsyncDataService }] |
| 26 | + }) |
| 27 | + .compileComponents(); |
| 28 | + }); |
| 29 | + |
| 30 | + beforeEach(async(() => { |
| 31 | + fixture = TestBed.createComponent(BookEditComponent); |
| 32 | + component = fixture.componentInstance; |
| 33 | + component.ngOnInit(); |
| 34 | + fixture.detectChanges(); |
| 35 | + compiled = fixture.debugElement.nativeElement; |
| 36 | + })); |
| 37 | + |
| 38 | + it('should be created', () => { |
| 39 | + expect(component).toBeTruthy(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should display the isbn of the loaded book', () => { |
| 43 | + expect(compiled.querySelector('input[name="isbn"]').value).toContain('978-0-20163-361-0'); |
| 44 | + expect(compiled.querySelector('input[name="title"]').value).toContain('esign Patterns'); |
| 45 | + expect(compiled.querySelector('input[name="author"]').value).toContain('Erich Gamma / Richard Helm / Ralph E. Johnson / John Vlissides'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should validate that title is required', fakeAsync(() => { |
| 49 | + compiled.querySelector('input[name="title"]').value = '' |
| 50 | + compiled.querySelector('input[name="title"]').dispatchEvent(new Event('input')); |
| 51 | + |
| 52 | + tick(); |
| 53 | + fixture.detectChanges(); |
| 54 | + |
| 55 | + expect(compiled.querySelector('.title-error').innerText).toContain('Title is required'); |
| 56 | + })); |
| 57 | + |
| 58 | + it('should submit the whole form', fakeAsync(() => { |
| 59 | + const submitSpy = spyOn(component, 'onSubmit'); |
| 60 | + |
| 61 | + compiled.querySelector('button[type="submit"]').click() |
| 62 | + |
| 63 | + expect(submitSpy.calls.any()).toBeTruthy(); |
| 64 | + })); |
| 65 | +}); |
0 commit comments