I have this component defined inside its own routing-module file, like this:
{
path: '',
redirectTo: 'list', pathMatch: 'full'
},
{
path: 'list',
component: BooksListContainerComponent,
canActivate: [AuthenticationGuard],
children: [
{ path: ':customerId' }
],
data: {
accessKey,
cardContainer: false,
noPadding: false,
}
}
As you can see, there are 2 ways to access this component, one of them is by adding a path param & the other one by just leaving that empty. But wasn’t able to recreate both scenarios by doing test with Jasmine.
Heres what I’ve done so far inside the spec file:
describe('(1) BooksListContainerComponent unit test', () => {
let fixture: ComponentFixture<BooksListContainerComponent>;
let component: BooksListContainerComponent;
let router: Router;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
BooksListContainerModule,
ReactiveFormsModule,
RouterTestingModule.withRoutes([{
path: 'list',
children: [{
path: ':customerId',
data: null,
component: BooksListContainerComponent
}]
}]),
],
providers: [
{
provide: ActivatedRoute, useValue: {
params: of({ customerId: 'test' })
}
}
],
declarations: [
BooksListContainerComponent,
AppComponent
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
fixture = TestBed.createComponent(BooksListContainerComponent);
router = TestBed.inject(Router);
component = fixture.componentInstance;
fixture.autoDetectChanges();
fixture.detectChanges();
});
it('Should create BooksListContainerComponent', () => {
let fixture = TestBed.createComponent(BooksListContainerComponent);
let component = fixture.debugElement.children[0].componentInstance;
component.customerECustCode = null;
fixture.detectChanges();
expect(component).toBeTruthy();
})
});
Here’s the full log output with the error:
Chrome 126.0.0 (Windows 10.0.0) (1) BooksListContainerComponent unit test Should create BooksListContainerComponent FAILED
Error: Invalid configuration of route 'list/:customerId'. One of the following must be provided: component, redirectTo, children or loadChildren
at validateNode (http://localhost:9876/_karma_webpack_/node_modules/@angular/router/__ivy_ngcc__/fesm2015/router.js:1292:1)
at validateConfig (http://localhost:9876/_karma_webpack_/node_modules/@angular/router/__ivy_ngcc__/fesm2015/router.js:1247:1)
at validateNode (http://localhost:9876/_karma_webpack_/node_modules/@angular/router/__ivy_ngcc__/fesm2015/router.js:1309:1)
at validateConfig (http://localhost:9876/_karma_webpack_/node_modules/@angular/router/__ivy_ngcc__/fesm2015/router.js:1247:1)
at Router.resetConfig (http://localhost:9876/_karma_webpack_/node_modules/@angular/router/__ivy_ngcc__/fesm2015/router.js:7462:1)
at new Router (http://localhost:9876/_karma_webpack_/node_modules/@angular/router/__ivy_ngcc__/fesm2015/router.js:6921:1)
at Object.setupTestingRouter [as useFactory] (http://localhost:9876/_karma_webpack_/node_modules/@angular/router/__ivy_ngcc__/fesm2015/testing.js:139:1)
at Object.factory (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:17223:1)
at R3Injector.hydrate (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:17053:42)
at R3Injector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:16803:1)
Here’s all dependencies from package.json:
"devDependencies": {
"@angular-devkit/build-angular": "~0.901.0",
"@angular/cli": "~9.1.0",
"@angular/compiler-cli": "~9.1.0",
"@angular/language-service": "~9.1.0",
"@chiragrupani/karma-chromium-edge-launcher": "^2.2.1",
"@types/crypto-js": "^3.1.47",
"@types/jasmine": "~3.5.0",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^12.11.1",
"codelyzer": "^5.1.2",
"jasmine-core": "~3.5.0",
"jasmine-spec-reporter": "~4.2.1",
"javascript-obfuscator": "^4.0.2",
"karma": "~4.4.1",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~2.1.0",
"karma-jasmine": "~3.0.1",
"karma-jasmine-html-reporter": "^1.4.2",
"protractor": "~5.4.3",
"ts-node": "~8.3.0",
"tslint": "~6.1.0",
"typescript": "~3.8.3"
},
Thanks
I’m just trying to make expect(component).toBeTruthy();
work