From 34c7fd4ee06de17213f396b762e36e89c28c1ff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Fri, 21 Sep 2018 19:08:24 +0200 Subject: [PATCH 1/2] Allow to override navigator for testing purposes To be able to test the language detection from the navigator allow to override the used navigator object. --- gsa/src/gmp/locale/detector.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gsa/src/gmp/locale/detector.js b/gsa/src/gmp/locale/detector.js index 275475092a..ce6820a985 100644 --- a/gsa/src/gmp/locale/detector.js +++ b/gsa/src/gmp/locale/detector.js @@ -30,8 +30,8 @@ const log = logger.getLogger('gmp.locale.detector'); const detectLanguageFromStorage = options => options.storage.locale; -const detectLanguageFromNavigator = () => { - if (typeof navigator !== 'undefined') { +const detectLanguageFromNavigator = ({navigator = global.navigator}) => { + if (navigator) { if (navigator.languages) { return [...navigator.languages]; } From 8e39e9858ad9b7280c9116cb66ecf88e51f426e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Fri, 21 Sep 2018 19:09:17 +0200 Subject: [PATCH 2/2] Add test for detection the language from a navigator --- gsa/src/gmp/locale/__tests__/detector.js | 92 ++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/gsa/src/gmp/locale/__tests__/detector.js b/gsa/src/gmp/locale/__tests__/detector.js index aa2911f9b4..bc5a8c6ffa 100644 --- a/gsa/src/gmp/locale/__tests__/detector.js +++ b/gsa/src/gmp/locale/__tests__/detector.js @@ -98,6 +98,98 @@ describe('LanguageDetector tests', () => { expect(languageUtils.isWhitelisted).toHaveBeenCalledWith('en-US'); }); + test('should return languages from fake navigator', () => { + const storage = {}; + const languageUtils = { + formatLanguageCode: jest.fn().mockImplementation(l => l), + isWhitelisted: jest.fn().mockReturnValue(true), + }; + + const detector = new LanguageDetector(); + + const navigator = {}; + const languages = jest.fn().mockReturnValue(['lorem', 'ipsum']); + Object.defineProperty(navigator, 'languages', { + get: languages, + }); + + detector.init({languageUtils}, {storage, navigator}, {fallbackLng: 'bar'}); + + expect(detector.detect()).toEqual('lorem'); + expect(languages).toHaveBeenCalled(); + expect(languageUtils.formatLanguageCode).toHaveBeenCalledTimes(1); + expect(languageUtils.formatLanguageCode).toHaveBeenCalledWith('lorem'); + expect(languageUtils.isWhitelisted).toHaveBeenCalledTimes(1); + expect(languageUtils.isWhitelisted).toHaveBeenCalledWith('lorem'); + }); + + test('should return language from fake navigator', () => { + const storage = {}; + const languageUtils = { + formatLanguageCode: jest.fn().mockImplementation(l => l), + isWhitelisted: jest.fn().mockReturnValue(true), + }; + + const detector = new LanguageDetector(); + + const navigator = {}; + const language = jest.fn().mockReturnValue('lorem'); + Object.defineProperty(navigator, 'language', { + get: language, + }); + + detector.init({languageUtils}, {storage, navigator}, {fallbackLng: 'bar'}); + + expect(detector.detect()).toEqual('lorem'); + expect(language).toHaveBeenCalled(); + expect(languageUtils.formatLanguageCode).toHaveBeenCalledTimes(1); + expect(languageUtils.formatLanguageCode).toHaveBeenCalledWith('lorem'); + expect(languageUtils.isWhitelisted).toHaveBeenCalledTimes(1); + expect(languageUtils.isWhitelisted).toHaveBeenCalledWith('lorem'); + }); + + test('should return userLanguage from fake navigator', () => { + const storage = {}; + const languageUtils = { + formatLanguageCode: jest.fn().mockImplementation(l => l), + isWhitelisted: jest.fn().mockReturnValue(true), + }; + + const detector = new LanguageDetector(); + + const navigator = {}; + const userLanguage = jest.fn().mockReturnValue('lorem'); + Object.defineProperty(navigator, 'userLanguage', { + get: userLanguage, + }); + + detector.init({languageUtils}, {storage, navigator}, {fallbackLng: 'bar'}); + + expect(detector.detect()).toEqual('lorem'); + expect(userLanguage).toHaveBeenCalled(); + expect(languageUtils.formatLanguageCode).toHaveBeenCalledTimes(1); + expect(languageUtils.formatLanguageCode).toHaveBeenCalledWith('lorem'); + expect(languageUtils.isWhitelisted).toHaveBeenCalledTimes(1); + expect(languageUtils.isWhitelisted).toHaveBeenCalledWith('lorem'); + }); + + test('should return fallback when navigator is not available', () => { + const storage = {}; + const languageUtils = { + formatLanguageCode: jest.fn().mockImplementation(l => l), + isWhitelisted: jest.fn().mockReturnValue(true), + }; + + const detector = new LanguageDetector(); + + detector.init({languageUtils}, {storage, navigator: null}, + {fallbackLng: 'bar'}); + + expect(detector.detect()).toEqual('bar'); + expect(languageUtils.formatLanguageCode).not.toHaveBeenCalled(); + expect(languageUtils.isWhitelisted).not.toHaveBeenCalled(); + }); + }); // vim: set ts=2 sw=2 tw=80: