Skip to content

Commit

Permalink
Merge branch 'master' into Icons
Browse files Browse the repository at this point in the history
  • Loading branch information
swaterkamp authored Sep 25, 2018
2 parents 800c755 + 3c9243f commit ce3183e
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
92 changes: 92 additions & 0 deletions gsa/src/gmp/locale/__tests__/detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
4 changes: 2 additions & 2 deletions gsa/src/gmp/locale/detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down

0 comments on commit ce3183e

Please sign in to comment.