Skip to content

Commit

Permalink
refactor(userServices): ent-4669 locale func (#913)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcabrera committed Jun 14, 2022
1 parent 18a4158 commit ac3e273
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,21 @@

exports[`UserServices should return a specific locale cookie value 1`] = `
Object {
"data": Object {
"key": "English",
"value": "en-US",
},
"key": "English",
"value": "en-US",
}
`;

exports[`UserServices should return default locale if no locale cookie is present 1`] = `
Object {
"data": Object {
"key": "English",
"value": "en-US",
},
"key": "English",
"value": "en-US",
}
`;

exports[`UserServices should return the default locale with an invalid ISO_639 code 1`] = `
Object {
"data": Object {
"key": "English",
"value": "en-US",
},
"key": "English",
"value": "en-US",
}
`;
9 changes: 4 additions & 5 deletions src/services/user/__tests__/userServices.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ describe('UserServices', () => {
});

it('should export a specific number of methods and classes', () => {
expect(Object.keys(userServices)).toHaveLength(5);
expect(Object.keys(userServices)).toHaveLength(4);
});

it('should have specific methods', () => {
expect(userServices.getLocale).toBeDefined();
expect(userServices.logoutUser).toBeDefined();
expect(userServices.deleteAccountOptIn).toBeDefined();
expect(userServices.getAccountOptIn).toBeDefined();
expect(userServices.updateAccountOptIn).toBeDefined();
Expand All @@ -43,20 +42,20 @@ describe('UserServices', () => {
it('should return default locale if no locale cookie is present', async () => {
const response = await userServices.getLocale();

expect(response).toMatchSnapshot();
expect(response.data).toMatchSnapshot();
});

it('should return a specific locale cookie value', async () => {
Cookies.get = jest.fn().mockImplementation(() => 'en_US');
const response = await userServices.getLocale();

expect(response).toMatchSnapshot();
expect(response.data).toMatchSnapshot();
});

it('should return the default locale with an invalid ISO_639 code', async () => {
Cookies.get = jest.fn().mockImplementation(() => 'test_US');
const response = await userServices.getLocale();

expect(response).toMatchSnapshot();
expect(response.data).toMatchSnapshot();
});
});
53 changes: 20 additions & 33 deletions src/services/user/userServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,35 @@ import { serviceCall } from '../config';
import { helpers } from '../../common';

/**
* Return a platform locale value from a cookie.
*
* @private
* @returns {{value: string, key: string | null}|null}
* ToDo: Review moving the getLocale function under platformServices.
* Also review using window.navigator.language as the primary pull for language.
*/
const getLocaleFromCookie = () => {
const value = (Cookies.get(process.env.REACT_APP_CONFIG_SERVICE_LOCALES_COOKIE) || '').replace('_', '-');
const key = (value && LocaleCode.getLanguageName(value)) || null;

return (key && { value, key }) || null;
};

/**
* Return platform locale.
* Return a browser locale, or fallback towards the platform locale cookie
*
* @returns {Promise<{data: void}>}
* @returns {Promise<*>}
*/
const getLocale = () => {
const defaultLocale = {
const defaultLang = {
value: helpers.UI_LOCALE_DEFAULT,
key: helpers.UI_LOCALE_DEFAULT_DESC
};
const parseLang = value => {
const key = (value && LocaleCode.getLanguageName(value)) || null;
return (key && { value, key }) || undefined;
};

return new Promise(resolve =>
resolve({
data: getLocaleFromCookie() || defaultLocale
})
);
};
return serviceCall({
url: async () => {
const cookieLang = await (Cookies.get(process.env.REACT_APP_CONFIG_SERVICE_LOCALES_COOKIE) || '').replace(
'_',
'-'
);

const logoutUser = () =>
new Promise(resolve => {
resolve({});
return parseLang(cookieLang) || defaultLang;
}
});
};

/**
* @apiMock {DelayResponse} 2000
Expand Down Expand Up @@ -243,19 +238,11 @@ const updateAccountOptIn = (params = {}) =>
params
});

const userServices = { getLocale, logoutUser, deleteAccountOptIn, getAccountOptIn, updateAccountOptIn };
const userServices = { getLocale, deleteAccountOptIn, getAccountOptIn, updateAccountOptIn };

/**
* Expose services to the browser's developer console.
*/
helpers.browserExpose({ userServices });

export {
userServices as default,
userServices,
getLocale,
logoutUser,
deleteAccountOptIn,
getAccountOptIn,
updateAccountOptIn
};
export { userServices as default, userServices, getLocale, deleteAccountOptIn, getAccountOptIn, updateAccountOptIn };

0 comments on commit ac3e273

Please sign in to comment.