1. Fix broken tests for the EventComponent
Here we are using the TestBed API from Angular which helps configure and initialise our environment for unit testing and provides methods for creating components and services in unit tests. It is very similar to an @ngModule
but has some extra logic to be able to run a test with just this logic and not all the app.
src/app/event/containers/event/event.component.spec.ts
Copy import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
import { EventComponent } from './event.component' ;
import { NO_ERRORS_SCHEMA } from '@angular/compiler/src/core' ;
describe ( 'EventComponent' , () => {
let component : EventComponent ;
let fixture : ComponentFixture < EventComponent >;
beforeEach ( async (() => {
TestBed .configureTestingModule ({
declarations : [EventComponent] ,
schemas : [ NO_ERRORS_SCHEMA ]
}) .compileComponents ();
}));
beforeEach (() => {
fixture = TestBed .createComponent (EventComponent);
component = fixture .componentInstance;
fixture .detectChanges ();
});
it ( 'should create' , () => {
expect (component) .toBeTruthy ();
});
});
We need to make sure all dependencies our tests need are added available including the ReactiveForms module.
src/app/event/components/add-attendee/add-attendee.component.spec.ts
Copy import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
import { ReactiveFormsModule } from '@angular/forms' ;
import { AddAttendeeComponent } from './add-attendee.component' ;
describe ( 'AddAttendeeComponent' , () => {
let component : AddAttendeeComponent ;
let fixture : ComponentFixture < AddAttendeeComponent >;
beforeEach ( async (() => {
TestBed .configureTestingModule ({
imports : [ ReactiveFormsModule ] ,
declarations : [ AddAttendeeComponent ]
})
.compileComponents ();
}));
beforeEach (() => {
fixture = TestBed .createComponent (AddAttendeeComponent);
component = fixture .componentInstance;
fixture .detectChanges ();
});
it ( 'should create' , () => {
expect (component) .toBeTruthy ();
});
});
src/app/event/components/add-attendee/add-attendee.component.spec.ts
Copy import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
import { ReactiveFormsModule } from '@angular/forms' ;
import { AddAttendeeComponent } from './add-attendee.component' ;
import { Attendee } from '../../../models' ;
describe ( 'AddAttendeeComponent' , () => {
let component : AddAttendeeComponent ;
let fixture : ComponentFixture < AddAttendeeComponent >;
beforeEach ( async (() => {
TestBed .configureTestingModule ({
imports : [ReactiveFormsModule] ,
declarations : [AddAttendeeComponent]
}) .compileComponents ();
}));
beforeEach (() => {
fixture = TestBed .createComponent (AddAttendeeComponent);
component = fixture .componentInstance;
fixture .detectChanges ();
});
it ( 'should create' , () => {
expect (component) .toBeTruthy ();
});
it ( 'should have an invalid form on load' , () => {
expect ( component . addAttendeeForm .invalid) .toEqual ( false );
});
});
src/app/event/components/add-attendee/add-attendee.component.spec.ts
Copy import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
import { ReactiveFormsModule } from '@angular/forms' ;
import { AddAttendeeComponent } from './add-attendee.component' ;
import { Attendee } from '../../../models' ;
describe ( 'AddAttendeeComponent' , () => {
let component : AddAttendeeComponent ;
let fixture : ComponentFixture < AddAttendeeComponent >;
beforeEach ( async (() => {
TestBed .configureTestingModule ({
imports : [ReactiveFormsModule] ,
declarations : [AddAttendeeComponent]
}) .compileComponents ();
}));
beforeEach (() => {
fixture = TestBed .createComponent (AddAttendeeComponent);
component = fixture .componentInstance;
fixture .detectChanges ();
});
it ( 'should create' , () => {
expect (component) .toBeTruthy ();
});
it ( 'should have an invalid form on load' , () => {
expect ( component . addAttendeeForm .valid) .toEqual ( false );
});
it ( 'should have a valid form' , () => {
component . addAttendeeForm . controls . name .setValue ( 'Duncan' );
expect ( component . addAttendeeForm .valid) .toEqual ( true );
});
});