import 'reflect-metadata'; import { requiredMocks, } from './../../../../test-mocks'; requiredMocks(jest); import { BrandSettingsInterface, } from './../../../models/index'; import { CurrentEnvironmentService, } from './../index'; import { ApiKeyHeaderGenerationService, } from './api-key-header-generation.service'; const initApiKeyHeaderGenerationService = ( brandSettings: BrandSettingsInterface, currentEnvironmentService: CurrentEnvironmentService, ) => { return new ApiKeyHeaderGenerationService( brandSettings, currentEnvironmentService, ); }; describe('getProductRequestHeaders', () => { const initGetProductRequestHeadersData = ( brandSettings = { publishableKey: 'pk_live_605f5643f65599624ee886c0ee39474812537df9', testPublishableKey: 'pk_test_5c0f2c5bd8e176e44bd4fa65b8c2cc7abea07842', } as BrandSettingsInterface, currentEnvironmentService = ({ isProduction: jest.fn().mockReturnValue(true), } as any) as CurrentEnvironmentService, ) => { const wallArtDataService = initApiKeyHeaderGenerationService( brandSettings, currentEnvironmentService, ); return { brandSettings, currentEnvironmentService, wallArtDataService, }; }; test('Calls currentEnvironmentService.isProduction', () => { const { currentEnvironmentService, wallArtDataService, } = initGetProductRequestHeadersData(); wallArtDataService.getProductRequestHeaders(); expect(currentEnvironmentService.isProduction).toHaveBeenCalled(); }); // tslint:disable-next-line test('Gets the publishable key as an authorization header', () => { const { wallArtDataService, } = initGetProductRequestHeadersData(); const headers = wallArtDataService.getProductRequestHeaders(); expect(headers.get('Authorization')) .toBe('ApiKey pk_live_605f5643f65599624ee886c0ee39474812537df9'); }); // tslint:disable-next-line test('Gets the test publishable key without the pk_test_ prefix as an authorization header if not on a production environment', () => { const currentEnvironmentService = {} as CurrentEnvironmentService; currentEnvironmentService.isProduction = jest.fn().mockReturnValue(false); const { wallArtDataService, } = initGetProductRequestHeadersData( undefined, currentEnvironmentService, ); const headers = wallArtDataService.getProductRequestHeaders(); expect(headers.get('Authorization')) .toBe('ApiKey pk_test_5c0f2c5bd8e176e44bd4fa65b8c2cc7abea07842'); }); });