I am trying run some tests on Angular Karma on an angular component to determine the route.
The component is structured like this
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
@Component({
selector: 'app-basic-form',
templateUrl: './basic-form.component.html',
styleUrls: ['./basic-form.component.less'],
})
export class BasicFormComponent implements OnInit {
constructor(
public activatedRoute: ActivatedRoute,
) {}
ngOnInit() {
if(this.activatedRoute.snapshot.routeConfig.path === 'basic-form/:date'){ // route with date
let date = this.route.snapshot.paramMap.get('date'); // get the date
} else {
// no worries, just use the latest date, and this component can be used on multiple routes
}
}
In my test file I have
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
import { BasicFormComponent } from './basic-form.component';
describe('BasicFormComponent', () => {
let component: BasicFormComponent;
let fixture: ComponentFixture<BasicFormComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ BasicFormComponent ],
imports: [DemoMaterialModule, ReactiveFormsModule, FormsModule, HttpClientTestingModule, RouterTestingModule.withRoutes([])],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
providers: [
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(BasicFormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component.activatedRoute.snapshot.routeConfig.path).toBeTruthy(); // why is this not working?
});
});
The errors I am getting is
BasicFormComponent should create TypeError: Cannot read property 'path' of null TypeError: Cannot read property 'snapshot' of undefined
Why won't it import the activatedRoute module?
I am trying to get my head wrapped testing, which seems to be a dark art.
Any guidance will be appreciated.