import 'reflect-metadata'; jest.mock('angular-svg-icon', () => ({})); jest.mock('angular-svg-round-progressbar/src/index', () => ({})); jest.mock('mixpanel-browser/src/loader-module', () => ({})); import { BrandSettingsInterface, KiteCheckoutInterface, } from './../../../models/index'; import { CurrentEnvironmentService, GeneralBrandService, } from './../../services/index'; import KiteCheckoutService from './kite-checkout.service'; const initKiteCheckoutService = ( window: Window = { KiteCheckout: jest.fn(), } as any, generalBrandService: GeneralBrandService = { getBrandSettings: () => { return { publishableKey: 'testkey', } as BrandSettingsInterface; }, } as GeneralBrandService, currentEnvironmentService = {} as CurrentEnvironmentService, ) => { return new KiteCheckoutService( window, generalBrandService, currentEnvironmentService, ); }; describe('initCheckout', () => { // tslint:disable-next-line test('It uses the publishableKey from the brand settings to initalise the checkout if _currentEnvironmentService.isProduction returns true', () => { const window = { KiteCheckout: jest.fn(), } as any; const generalBrandService = { getBrandSettings: () => { return { publishableKey: 'testkey', } as BrandSettingsInterface; }, } as GeneralBrandService; const currentEnvironmentService = {} as CurrentEnvironmentService; currentEnvironmentService.isProduction = jest.fn() .mockReturnValue(true); const kiteCheckoutService = initKiteCheckoutService( window, generalBrandService, currentEnvironmentService, ); kiteCheckoutService.initCheckout(); expect(window.KiteCheckout).toHaveBeenCalledWith('testkey'); }); // tslint:disable-next-line test('It uses the testPublishableKey from the brand settings to initalise the checkout if _currentEnvironmentService.isProduction returns false', () => { const window = { KiteCheckout: jest.fn(), } as any; const generalBrandService = { getBrandSettings: () => { return { testPublishableKey: 'testpubkey', } as BrandSettingsInterface; }, } as GeneralBrandService; const currentEnvironmentService = {} as CurrentEnvironmentService; currentEnvironmentService.isProduction = jest.fn() .mockReturnValue(false); const kiteCheckoutService = initKiteCheckoutService( window, generalBrandService, currentEnvironmentService, ); kiteCheckoutService.initCheckout(); expect(window.KiteCheckout).toHaveBeenCalledWith('testpubkey'); }); // tslint:disable-next-line test('It sets the checkout object to the initalised checkout', () => { const expectedCheckout = {}; const window = { KiteCheckout: jest.fn().mockReturnValue( expectedCheckout, ), } as any; const currentEnvironmentService = {} as CurrentEnvironmentService; currentEnvironmentService.isProduction = jest.fn() .mockReturnValue(true); const kiteCheckoutService = initKiteCheckoutService( window, undefined, currentEnvironmentService, ); kiteCheckoutService.initCheckout(); expect(kiteCheckoutService.checkout).toBe( expectedCheckout, ); }); }); describe('get checkout', () => { test('It returns the value in private var _checkout', () => { const kiteCheckoutService = initKiteCheckoutService(); const checkoutValue = {} as KiteCheckoutInterface; kiteCheckoutService._checkout = checkoutValue; expect(kiteCheckoutService.checkout) .toBe(checkoutValue); }); test('If _checkout is not defined it calls initCheckout', () => { const kiteCheckoutService = initKiteCheckoutService(); kiteCheckoutService.initCheckout = jest.fn(); kiteCheckoutService._checkout = undefined; const testVal = kiteCheckoutService.checkout; expect(kiteCheckoutService.initCheckout) .toHaveBeenCalled(); }); });