import 'reflect-metadata'; import { Http, Response } from '@angular/http'; import { LocationService, } from './location.service'; import * as mock from '../../../mocks/index'; import { MEASUREMENTS } from '../../../models'; const initMockHttp = mock.http(jest); const initLocationService = ( http?: Http, ) => { return new LocationService(http); }; const setNavigatorLanguage = (language: string) => { navigator.__defineGetter__('language', () => { return language; }); }; describe('constructor', () => { // tslint:disable-next-line test('It sets _location to an Observable of navigator.language', () => { setNavigatorLanguage('test'); const locationService = initLocationService(); locationService._location.subscribe((resp) => { expect(resp).toBe('test'); }); }); }); describe('getDefaultCurrency', () => { // tslint:disable-next-line test('It returns GBP if no matching currency is found', () => { const httpResp = {} as Response; httpResp.json = jest.fn() .mockReturnValue({ currency: {}, }); const http = initMockHttp(httpResp); const locationService = initLocationService(http); locationService.getDefaultCurrencyCode().subscribe((resp) => { expect(resp).toBe('GBP'); }); }); // tslint:disable-next-line test('It returns the matching currency from the http call', () => { const httpResp = {} as Response; const respObj = { currency: { code: 'TESTCODE', }, }; httpResp.json = jest.fn() .mockReturnValue(respObj); const http = initMockHttp(httpResp); const locationService = initLocationService(http); locationService.getDefaultCurrencyCode().subscribe((resp) => { expect(resp).toBe('TESTCODE'); }); }); }); describe('getDefaultLanguage', () => { // tslint:disable-next-line test('It returns en if no matching language is found', () => { setNavigatorLanguage('not a language'); const locationService = initLocationService(); locationService.getDefaultLanguage().subscribe((resp) => { expect(resp).toBe('en'); }); }); const itReturnsMatchingLanguage = ( language: string, expectedOutcome: string, ) => { // tslint:disable-next-line test('It returns a matching language for navigator.language ' + language, () => { setNavigatorLanguage(language); const locationService = initLocationService(); locationService.getDefaultLanguage().subscribe((resp) => { expect(resp).toBe(expectedOutcome); }); }); }; [{ expectedOutcome: 'en', language: 'en-GB', }, { expectedOutcome: 'fr', language: 'fr-FR', }].forEach((languagePair) => { itReturnsMatchingLanguage( languagePair.language, languagePair.expectedOutcome, ); }); }); describe('getMeasurementSystem', () => { // tslint:disable-next-line test('Returns MEASUREMENTS.IMPERIAL if the language is set to en-US', () => { setNavigatorLanguage('en-US'); const locationService = initLocationService(); locationService.getMeasurementSystem().subscribe((resp) => { expect(resp).toBe(MEASUREMENTS.IMPERIAL); }); }); // tslint:disable-next-line test('Returns MEASUREMENTS.METRIC if the language is not set to en-Us', () => { setNavigatorLanguage('en-GB'); const locationService = initLocationService(); locationService.getMeasurementSystem().subscribe((resp) => { expect(resp).toBe(MEASUREMENTS.METRIC); }); }); });