Skip to content

Instantly share code, notes, and snippets.

@faran312
Last active January 15, 2020 02:51
Show Gist options
  • Save faran312/0f42c0ef92bfae2af09229865b82050c to your computer and use it in GitHub Desktop.
Save faran312/0f42c0ef92bfae2af09229865b82050c to your computer and use it in GitHub Desktop.
Template Unit Test for Angular Component + Service
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { ComponentName } from 'fileOfComponent';
describe('NameOfTheComponent', () => {
let component: ComponentName;
let fixture: ComponentFixture<ComponentName>;
beforeEach(() => {
return TestBed.configureTestingModule({
declarations: [ComponentName],
providers: []
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(ComponentName);
component = fixture.componentInstance;
});
});
afterEach(() => {
if (fixture) { fixture.destroy(); }
component = null;
});
it('Should render without error', () => {
expect(component instanceof ComponentName).toBeTruthy();
});
it('Should hit ngOnInit', () => {
comp.ngOnInit();
expect(component instanceof ComponentName).toBeTruthy();
});
});
import { Component } from '@angular/core';
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { DirectiveName } from 'directoryOfDirective';
@Component({
selector: 'app-test',
template: `
<div [directive-selector]></div>
`
})
class MockComponent { }
describe('NameOfTheComponent', () => {
let component: MockComponent;
let fixture: ComponentFixture<MockComponent>;
beforeEach(() => {
return TestBed.configureTestingModule({
declarations: [
MockComponent,
DirectiveName
],
providers: []
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(MockComponent);
component = fixture.componentInstance;
});
});
afterEach(() => {
if (fixture) { fixture.destroy(); }
component = null;
});
it('Should render without error', () => {
expect(component instanceof MockComponent).toBeTruthy();
});
});
import { TestBed } from '@angular/core/testing';
import { ServiceName } from 'fileOfService';
describe('NameOfTheService', () => {
let service: ServiceName;
beforeEach(() => {
Testbed.configureTestingModule({
providers: [ServiceName]
});
service = TestBed.get(ServiceName);
})
afterEach(() => {
service = null;
});
it('Will Declared Notification Service without any errors', () => {
expect(service instanceof ServiceName).toBeTruthy();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment