Skip to content

Instantly share code, notes, and snippets.

@manivelarjunan
Last active December 2, 2018 22:24
Show Gist options
  • Save manivelarjunan/33ef42a8a98b271d749bceb1a7f56533 to your computer and use it in GitHub Desktop.
Save manivelarjunan/33ef42a8a98b271d749bceb1a7f56533 to your computer and use it in GitHub Desktop.
App component spec file with beforeEach implementaion
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { UserComponent } from './user/user.component';
import { UserAsyncComponent } from './user-async/user-async.component';
describe('App component', () => {
beforeEach(async(() => {
// The TestBed is the most important of the Angular testing utilities.
// The TestBed creates a dynamically-constructed Angular test module that emulates an Angular @NgModule.
// The TestBed.configureTestingModule() method takes a metadata object that can have most of the properties of an @NgModule.
TestBed.configureTestingModule({
declarations: [AppComponent, UserComponent, UserAsyncComponent]
}).compileComponents();
}));
describe(':', () => {
// Begin by putting re-usable, preparatory code in a setup function instead of beforeEach().
// The setup() function returns an object literal with the variables, such as app, that a test might reference.
// You don't define semi-global variables (e.g., let app,fixture ) in the body of the describe().
// Then each test invokes setup() in its first line, before continuing with steps that
// manipulate the test subject and assert expectations.
function setup() {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
return { fixture, app };
}
it('should create the app', async(() => {
const { app } = setup();
expect(app).toBeTruthy();
}));
it('should have title as \'Angular7-unit-testing!\'', async(() => {
const { app } = setup();
expect(app.title).toBe('Angular7-unit-testing!');
}));
it('should have h1 tag as \'Welcome Angulat7-unit-testing!\'', async(() => {
const { app, fixture } = setup();
fixture.detectChanges();
const compile = fixture.debugElement.nativeElement;
const h1tag = compile.querySelector('h1');
expect(h1tag.textContent).toBe(' Welcome to Angular7-unit-testing!! ');
}));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment