import 'reflect-metadata'; import { HttpClient, } from '@angular/common/http'; import { KiteBasketService, } from './kite-basket.service'; import { of, } from 'rxjs'; import { KiteBasketInterface, } from '../../../models/kite-basket.interface'; const productionUrl = 'https://kite-basket.herokuapp.com/basket'; const initKiteBasketService = ( http?: HttpClient, ) => { return new KiteBasketService(http); }; describe('basketUrl', () => { test('Returns the value of process.env.BASKET_URL if set', () => { process.env.BASKET_URL = 'basketUrl/'; const kiteBasketService = initKiteBasketService(); const url = kiteBasketService.basketUrl; expect(url).toBe('basketUrl/basket'); delete process.env.BASKET_URL; }); // tslint:disable-next-line test('Returns the production basket URL if process.env.BASKET_URL is not set', () => { const kiteBasketService = initKiteBasketService(); const url = kiteBasketService.basketUrl; expect(url).toBe(productionUrl); }); }); describe('getBasket$', () => { const initGetBasket$Data = () => { const mockResponseJson = { testJson: 'abc', }; const mockResponse = {} as Response; mockResponse.json = jest.fn().mockReturnValue(mockResponseJson); const http = {} as HttpClient; http.get = jest.fn().mockReturnValue(of(mockResponse)); const kiteBasketService = initKiteBasketService( http, ); return { http, kiteBasketService, mockResponse, mockResponseJson, }; }; test('Calls get http.get with the correct arguments', () => { const { http, kiteBasketService, } = initGetBasket$Data(); kiteBasketService.getBasket$().subscribe(() => { expect(http.get).toHaveBeenCalledWith( productionUrl, { withCredentials: true, }, ); }); }); test('Gets the json from the response', () => { const { mockResponse, kiteBasketService, } = initGetBasket$Data(); kiteBasketService.getBasket$().subscribe(() => { expect(mockResponse.json).toHaveBeenCalled(); }); }); test('Returns the json from the response', () => { const { mockResponseJson, kiteBasketService, } = initGetBasket$Data(); kiteBasketService.getBasket$().subscribe((resp) => { expect(resp).toBe(mockResponseJson); }); }); }); describe('setBasket$', () => { const initSetBasket$Data = () => { const kiteBasket = { collectorImages: [{}, {}], lineItems: [{}], } as KiteBasketInterface; const mockResponse = {}; const http = {} as HttpClient; http.post = jest.fn().mockReturnValue(of(mockResponse)); const kiteBasketService = initKiteBasketService( http, ); return { http, kiteBasket, kiteBasketService, mockResponse, }; }; test('Calls get http.post with the correct arguments', () => { const { http, kiteBasket, kiteBasketService, } = initSetBasket$Data(); kiteBasketService.setBasket$(kiteBasket).subscribe(() => { expect(http.post).toHaveBeenCalledWith( productionUrl, kiteBasket, { withCredentials: true, }, ); }); }); test('Returns the response of the http call', () => { const { mockResponse, kiteBasket, kiteBasketService, } = initSetBasket$Data(); kiteBasketService.setBasket$(kiteBasket).subscribe((resp) => { expect(resp).toBe(mockResponse); }); }); }); describe('clearBasket$', () => { const initClearBasket$Data = () => { const mockResponse = {}; const http = {} as HttpClient; http.delete = jest.fn().mockReturnValue(of(mockResponse)); const kiteBasketService = initKiteBasketService( http, ); return { http, kiteBasketService, mockResponse, }; }; test('Calls get http.delete with the correct arguments', () => { const { http, kiteBasketService, } = initClearBasket$Data(); kiteBasketService.clearBasket$().subscribe(() => { expect(http.delete).toHaveBeenCalledWith( productionUrl, { withCredentials: true, }, ); }); }); test('Returns the response of the http call', () => { const { mockResponse, kiteBasketService, } = initClearBasket$Data(); kiteBasketService.clearBasket$().subscribe((resp) => { expect(resp).toBe(mockResponse); }); }); });