I am unit testing my component using Karma and jasmine. fixture.detechChanges() should be use for every test in order to detect the changes. But this method is making the test case to fail and giving error
Error: InvalidPipeArgument: 'Unable to convert "Invalid Date" into a date' for pipe 'DatePipe'
Since I am new to this can anyone let me know why this is behaving like this and what is the correct place to use this.
Out the three test cases 2nd test case is failing. I have done lot of research but i am not sure why my test case is failing.
below is my code
component.ts
export class ResourceOverviewComponent implements OnInit {
id = '--';
lastupdated : any;
creationDate = '--'
description = '--'
tags = '--'
dateTimeFormat = 'MMM d, y h:mm a';
dateTimeZone: any;
constructor(
private readonly resourceOverviewService: ResourceOverviewService,
private readonly utilsService: UtilsService,
private readonly router: Router,
readonly route: ActivatedRoute,
) {}
ngOnInit() {
this.id = this.route.snapshot.queryParamMap.get('id');
this.getDetails();
}
viewInventory(){
this.router.navigate(['..'], {
relativeTo: this.route
});
}
getDetails() {
if (this.id) {
this.getResourceOverview();
}
}
getResourceOverview(): void {
const resourceID = `search_keys=${"resource_id"}&search=${this.id}`
this.resourceId = this.id
this.resourceOverviewService
.getResourceOverview(resourceID)
.subscribe(
(response) => {
const result = response as any;
if (result && result.raw_items[0]) {
this.creationDate = result.raw_resource_created || '--' ;
}
},
(error) => {
console.log('Resource overview details failed with error', error);
}
);
}
}
component.spec.ts
class mockHttpWrapperService {
readonly isApiReady = false;
}
describe('SomeComponent', () => {
let component: SomeComponent;
let fixture: ComponentFixture<SomeComponent>;
let someService: any;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ SomeComponent ],
imports: [
some module,some-module,somemodule.forRoot(),
RouterModule.forRoot([]),
],
providers: [
{ provide: NGXLogger, useClass: MockNGXLogger },
{ provide: HttpWrapperService, useClass: mockHttpWrapperService },
{ provide: Router, useClass: class { navigate = jasmine.createSpy("navigate"); }},
ApiService,
SomeService,
SomeService,
SomeService,
SomeService,
{
provide: ActivatedRoute,
useValue: {
params: of({ id: 'aeb24ca0549c', code: 'IBM' }),
snapshot: {
queryParamMap: convertToParamMap({
id: 'aeb24ca0549c',
code: 'IBM',
})
}
}
},
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
.compileComponents()
}));
beforeEach(() => {
fixture = TestBed.createComponent(SomeComponent);
component = fixture.debugElement.componentInstance;
// fixture.detectChanges();
});
it('should create component ', () => {
expect(component).toBeTruthy();
});
it('should init object for action tabs', async () => {
await fixture.whenStable();
component.ngOnInit();
fixture.detectChanges();
expect(component).toBeTruthy();
});
it('should get resource data ', () => {
someService = TestBed.inject(SomeService);
const data = {some-xyz-object};
spyOn(someService, "getResourceOverview").and.callFake(() => {
return of(data)
})
component.getDetails();
fixture.detectChanges();
expect(component).toBeTruthy();
});
});
component.html
<div class="container-wrapper">
<div class="breadcrumWrapper" >
<ibm-breadcrumb>
<ibm-breadcrumb-item >
{{ id }}
</ibm-breadcrumb-item>
</ibm-breadcrumb>
<div class="breadcrumWrapper" >
</div>
<div>
</div>
</div>
<div class="container-overview" >
<div class="overview" >
</div>
<div class="marginTop ">
<div class="bx--row">
<div class="bx--col-xs-6">
<div class="bx--row rowMargins">
<span
class="ellipsis overViewcontent bx--col-xs-5"
title="{{ id }}"
>{{ id }}</span
>
</div>
</div>
<div class="bx--col-xs-6">
<div class="bx--row rowMargins">
<span
class="ellipsis overViewcontent bx--col-xs-5"
title="{{ resourceType }}"
>{{ resourceType }}</span
>
</div>
</div>
</div>
<div class="bx--row">
<div class="bx--col-xs-6">
<div class="bx--row rowMargins">
<span
class="ellipsis overViewcontent bx--col-xs-5"
title="{{ region }}"
>{{ region }}</span
>
</div>
</div>
<div class="bx--col-xs-6">
<div class="bx--row rowMargins">
<div *ngFor="let k of keyValue; let i = index;">
<span *ngIf="i!=(keyValue.length-1)"
class="ellipsis overViewcontent bx--col-xs-5"
title="{{ id }}"
>{{ k.Key }} : {{ k.Value }} ,
</span>
<span *ngIf="i==(keyValue.length-1)"
class="ellipsis overViewcontent bx--col-xs-5"
title="{{ id }}"
>{{ k.Key }}:{{ k.Value }}
</span>
</div>
</div>
</div>
</div>
<div class="bx--row">
<div class="bx--col-xs-6">
<div class="bx--row rowMargins">
<span
class="ellipsis overViewcontent bx--col-xs-5"
title="{{ correlationID }}"
>{{ correlationID }}</span
>
</div>
</div>
<div class="bx--col-xs-6">
<div class="bx--row rowMargins">
<span
class="ellipsis overViewcontent bx--col-xs-5"
title="{{ resourceCategory }}"
>{{ resourceCategory }}</span
>
</div>
</div>
</div>
<div *ngIf="showMore">
<div class="bx--row">
<div class="bx--col-xs-6">
<div class="bx--row rowMargins">
<span
class="ellipsis overViewcontent bx--col-xs-5"
title="{{ status }}"
>{{ status }}</span
>
</div>
</div>
<div class="bx--col-xs-6">
<div class="bx--row rowMargins">
<span
class="ellipsis overViewcontent bx--col-xs-5"
title="{{ provider }}"
>{{ provider }}</span
>
</div>
</div>
</div>
<div class="bx--row">
<div class="bx--col-xs-6">
<div class="bx--row rowMargins">
<span
class="ellipsis overViewcontent bx--col-xs-5"
title="{{ providerAccount }}"
>{{ providerAccount }}</span
>
</div>
</div>
<div class="bx--col-xs-6">
<div class="bx--row rowMargins">
<span
class="ellipsis overViewcontent bx--col-xs-5"
title="{{ resourceName }}"
>{{ resourceName }}</span
>
</div>
</div>
</div>
</div>
</div>
<div class="container-overview complex-data-viewer-wrap" >
<div class="bx--row">
<div class="bx--col-xs-6">
<div class="bx--row rowMargins">
<span
class="ellipsis overViewcontent bx--col-xs-5"
title="{{ creationDate }}"
>{{ creationDate }}</span
>
</div>
</div>
</div>
</div>