import 'reflect-metadata'; jest.mock('@kite-tech/checkout-sdk', () => { return { KiteCheckout: jest.fn(), }; }); import { KiteCheckout, } from '@kite-tech/checkout-sdk'; import { requiredMocks, } from './../../../../test-mocks'; requiredMocks(jest); import { BrandSettingsInterface, } from './../../../models/index'; import { CurrentEnvironmentService, } from './../../services/index'; import { KiteCheckoutService, } from './kite-checkout.service'; const initKiteCheckoutService = ( brandSettings = { publishableKey: 'testkey', } as BrandSettingsInterface, currentEnvironmentService = {} as CurrentEnvironmentService, ) => { return new KiteCheckoutService( brandSettings, currentEnvironmentService, ); }; describe('initCheckout', () => { // tslint:disable-next-line test('It uses the publishableKey from the brand settings to initalise the checkout and sets livePayments to true if _currentEnvironmentService.isProduction returns true', () => { const brandSettings = { publishableKey: 'testkey', } as BrandSettingsInterface; const currentEnvironmentService = {} as CurrentEnvironmentService; currentEnvironmentService.isProduction = jest.fn() .mockReturnValue(true); const kiteCheckoutService = initKiteCheckoutService( brandSettings, currentEnvironmentService, ); kiteCheckoutService.initCheckout(); expect(KiteCheckout).toHaveBeenCalledWith('testkey', true); }); // tslint:disable-next-line test('It uses the testPublishableKey from the brand settings to initalise the checkout and sets livePayments to false if _currentEnvironmentService.isProduction returns false', () => { const brandSettings = { testPublishableKey: 'testpubkey', } as BrandSettingsInterface; const currentEnvironmentService = {} as CurrentEnvironmentService; currentEnvironmentService.isProduction = jest.fn() .mockReturnValue(false); const kiteCheckoutService = initKiteCheckoutService( brandSettings, currentEnvironmentService, ); kiteCheckoutService.initCheckout(); expect(KiteCheckout).toHaveBeenCalledWith('testpubkey', false); }); // tslint:disable-next-line test('It sets the checkout object to the initalised checkout', () => { const expectedCheckout = {}; const currentEnvironmentService = {} as CurrentEnvironmentService; currentEnvironmentService.isProduction = jest.fn() .mockReturnValue(true); const kiteCheckoutService = initKiteCheckoutService( undefined, currentEnvironmentService, ); (KiteCheckout as any) = jest.fn().mockReturnValue(expectedCheckout); 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(); }); });