Last active
January 15, 2020 02:51
-
-
Save faran312/0f42c0ef92bfae2af09229865b82050c to your computer and use it in GitHub Desktop.
Template Unit Test for Angular Component + Service
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, 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(); | |
}); | |
}); |
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 { 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(); | |
}); | |
}); |
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 } 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