I`m trying to write unit tests to cover every line of my code. I have two lines of my code that are not covered.
I can`t understand where I make a mistake and how I can cover these lines of code?
Here is an image of not covered lines of code:
Product-item.spect.ts
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { async, ComponentFixture,TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { ProductService } from '../../services/product.service';
import { ProdItemComponent } from './product-item.component';
describe('ProdItemComponent', () => {
let component: ProdItemComponent;
let fixture: ComponentFixture<ProdItemComponent>;
let productService: ProductService;
let mockProductItem: any;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ProductItemComponent],
imports: [HttpClientTestingModule, RouterTestingModule],
providers: [ProductService],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
.compileComponents();
}));
beforeEach(() => {
mockProductItem= {
id: "c7336f01-5219-4631-a865-af1fa9837766",
title:"Carpet",
description:"A soft Carpet"
}
fixture = TestBed.createComponent(ProductItemComponent);
component = fixture.componentInstance;
productService = TestBed.inject(ProductService);
fixture.detectChanges();
component.productItem = mockProductItem;
component.selectedItemId = component.mockProductItem.id;
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should call delete method', () => {
component.onDelete();
fixture.detectChanges();
productService.deleteProduct(component.selectedItemId).subscribe(selectedItemId => {
expect(selectedItemId).toBeTruthy();
const spy = spyOn(component.fetchDataEventEmitter, 'emit');
expect(spy).toHaveBeenCalled();
})
});
expect(component.displayConfirmDialog).toBe(false);
});
Product-service.ts
deleteProduct(productId: string) {
return this.httpClient.delete(
this.BASE_URL +
this.DELETE_ITEM_URL(
productId
)
)
}
Product-component.ts
onDelete(): void {
if (this.selectedItemId) {
this.productService.deleteProduct(this.selectedItemId).subscribe(res => {
this.fetchDataEventEmitter.emit();
});
}
this.displayConfirmDialog = false;
}