Skip to content

Instantly share code, notes, and snippets.

@manivelarjunan
Created December 2, 2018 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save manivelarjunan/901db1349f923c97f2485323fe9c581e to your computer and use it in GitHub Desktop.
Save manivelarjunan/901db1349f923c97f2485323fe9c581e to your computer and use it in GitHub Desktop.
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