Skip to content

Instantly share code, notes, and snippets.

@zimejin
Forked from manivelarjunan/user.component.spec.ts
Created March 24, 2019 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zimejin/940172d6498b4a89223f4db23c7a195d to your computer and use it in GitHub Desktop.
Save zimejin/940172d6498b4a89223f4db23c7a195d to your computer and use it in GitHub Desktop.
import { async, TestBed } from '@angular/core/testing';
import { UserComponent } from './user.component';
import { UserService } from './user.service';
describe('UserComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [UserComponent]
});
// compileComponents(); - compileComponent is not required when using weback. why? When application is
// created with CLI and uses Web pack. it uses different workflow and build process.
}));
describe(':', () => {
function setup() {
const fixture = TestBed.createComponent(UserComponent);
const component = fixture.componentInstance;
const userService = fixture.debugElement.injector.get(UserService);
return { fixture, component, userService };
}
it('should create app component', () => {
const { component } = setup();
expect(component).toBeTruthy();
});
it('should display logged-in user name', () => {
const { fixture, component, userService } = setup();
const mockUser = { name: 'Mannie' };
spyOn(userService, 'getUser').and.returnValue(mockUser);
fixture.detectChanges();
const compile = fixture.debugElement.nativeElement;
const loggedInUser = compile.querySelector('p');
expect(loggedInUser.textContent).toBe(' Welcome Mannie ');
});
it('should display user is NOT logged in message', () => {
const { fixture, component, userService } = setup();
spyOn(userService, 'getUser').and.returnValue(undefined);
fixture.detectChanges();
const compile = fixture.debugElement.nativeElement;
const loggedInUser = compile.querySelector('p');
expect(loggedInUser.textContent).toBe(' user is NOT logged In. ');
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment