Forked from manivelarjunan/user-async.component.spec.ts
Created
March 24, 2019 17:52
-
-
Save zimejin/4a787fe7695653cada7e9b1c6d1049f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { TestBed, tick, fakeAsync } from '@angular/core/testing'; | |
import { UserAsyncComponent } from './user-async.component'; | |
import { UserAsyncService } from './user-async.service'; | |
import { Observable, Observer } from 'rxjs'; | |
describe('User Async Component:', () => { | |
beforeEach(async () => { | |
TestBed.configureTestingModule({ | |
declarations: [UserAsyncComponent] | |
}); | |
}); | |
describe(':', () => { | |
function setup() { | |
const fixture = TestBed.createComponent(UserAsyncComponent); | |
const app = fixture.debugElement.componentInstance; | |
const userAsyncService = fixture.debugElement.injector.get( | |
UserAsyncService | |
); | |
return { fixture, app, userAsyncService }; | |
} | |
it('should create the app component', () => { | |
const { app } = setup(); | |
expect(app).toBeTruthy(); | |
}); | |
it('should display user name', fakeAsync(() => { | |
const { fixture, app, userAsyncService } = setup(); | |
const mockUser = { name: 'Mannie' }; | |
spyOn(userAsyncService, 'getUserDetails').and.returnValue( | |
Observable.create((observer: Observer<{ name: string }>) => { | |
observer.next(mockUser); | |
return observer; | |
}) | |
); | |
tick(); | |
fixture.detectChanges(); | |
const userAsyncElement = fixture.debugElement.nativeElement; | |
const loggedInUserName = userAsyncElement.querySelector('p'); | |
expect(loggedInUserName.textContent).toBe(' Mannie '); | |
})); | |
it('should display a system error', fakeAsync(() => { | |
const { app, fixture, userAsyncService } = setup(); | |
spyOn(userAsyncService, 'getUserDetails').and.returnValue( | |
Observable.create((observer: Observer<{ name: string }>) => { | |
return observer.error('something went wrong'); | |
}) | |
); | |
tick(); | |
fixture.detectChanges(); | |
const userAsyncElement = fixture.debugElement.nativeElement; | |
const systemError = userAsyncElement.querySelector('p'); | |
expect(systemError.textContent).toBe('something went wrong'); | |
})); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment