2

I've written the test case for services where I'm testing Negative and positive scenarios both but giving me errors for the negative scenario. I've written in order first I'm checking the negative scenario and then the positive one.

Configuration :

describe('CartItemProductService', () => {
let service: CartItemProductService;
const apiServiceSpy = jasmine.createSpyObj<ApiService>('ApiService', ['refreshToken', 'get', 'post', 'patch', 'delete']);
apiServiceSpy.refreshToken.and.returnValue(of(null));
const priceListServiceSpy = jasmine.createSpyObj<PriceListService>('PriceListService', ['isPricelistId']);

beforeEach(() => {
    TestBed.configureTestingModule({
        imports: [HttpClientTestingModule, ApttusModule, CommerceModule.forRoot({}), TranslateModule.forRoot()],
        providers: [
            CartItemProductService,
            { provide: ApiService, useValue: apiServiceSpy },
            { provide: PriceListService, useValue: priceListServiceSpy },
        ],
    });
    service = TestBed.inject(CartItemProductService);
});

Negative Scenario :

    it('should return given Cart Line Item itself with no product info', () => {
    const results = service.addProductInfoToLineItems([CART_ITEMS_WITH_NO_PRODUCT_INFO[0]]);
    results.pipe(take(1)).subscribe((val) => {
        expect(apiServiceSpy.get).toHaveBeenCalledTimes(0);
        expect(val[0]).toEqual(CART_ITEMS_WITH_NO_PRODUCT_INFO[0]);
        expect(val[0].Product).toEqual(null);
    })
});

Positive Scenario :

    it('should return additional info for the given cart line items', () => {
    priceListServiceSpy.isPricelistId.and.returnValue(true);
    apiServiceSpy.get.and.returnValue(of(PRODUCT_WITH_ADDITIONAL_INFO));
    const results = service.addProductInfoToLineItems([CART_ITEMS[2]]);
    results.pipe(take(1)).subscribe((val) => {
        expect(priceListServiceSpy.isPricelistId).toHaveBeenCalledTimes(1);
        expect(apiServiceSpy.get).toHaveBeenCalledTimes(1);
        expect(val[0].Product.Id).toEqual(CART_ITEMS[2].Product.Id);
        expect(val[0].Product.ProductCode).not.toEqual(null);
    });
});

Error :

Image

0 Answers0