Skip to content

Commit f98ac24

Browse files
author
Marcin Kwiatkowski
committed
refactor(middleware): i18n middleware: adjustements
1 parent 69722dc commit f98ac24

File tree

5 files changed

+53
-126
lines changed

5 files changed

+53
-126
lines changed

packages/api-client/src/helpers/apiClient/defaultSettings.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export const defaultSettings: ClientConfig = {
2121
setCurrency: () => {},
2222
getLocale: () => '',
2323
setLocale: () => {},
24+
getCountry: () => '',
25+
setCountry: () => {},
2426
},
2527
externalCheckout: {
2628
enable: false,

packages/composables/nuxt/plugin.js

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,73 +21,27 @@ export default integrationPlugin(({ app, res, req, integration }) => {
2121

2222
const getCartId = () => getCookies(cartCookieName);
2323

24-
const setCartId = (id) => {
25-
if (!id) {
26-
removeCookie(cartCookieName)
27-
// eslint-disable-next-line no-param-reassign
28-
return;
29-
}
30-
setCookie(cartCookieName, id);
31-
};
24+
const setCartId = (id) => !id ? removeCookie(cartCookieName) : setCookie(cartCookieName, id);
3225

3326
const getCustomerToken = () => getCookies(customerCookieName);
3427

35-
const setCustomerToken = (token) => {
36-
if (!token) {
37-
removeCookie(customerCookieName);
38-
return;
39-
}
40-
41-
setCookie(customerCookieName, token);
42-
};
28+
const setCustomerToken = (token) => !token ? removeCookie(customerCookieName) : setCookie(customerCookieName, token);
4329

4430
const getStore = () => getCookies(storeCookieName);
4531

46-
const setStore = (id) => {
47-
if (!id) {
48-
// eslint-disable-next-line no-param-reassign
49-
removeCookie(storeCookieName);
50-
return;
51-
}
52-
53-
setCookie(storeCookieName, id);
54-
};
32+
const setStore = (id) => !id ? removeCookie(storeCookieName) : setCookie(storeCookieName, id);
5533

5634
const getCurrency = () => getCookies(currencyCookieName);
5735

58-
const setCurrency = (id) => {
59-
if (!id) {
60-
// eslint-disable-next-line no-param-reassign
61-
removeCookie(currencyCookieName);
62-
return;
63-
}
64-
65-
setCookie(currencyCookieName, id);
66-
};
36+
const setCurrency = (id) => !id ? removeCookie(currencyCookieName) : setCookie(currencyCookieName, id);
6737

6838
const getLocale = () => getCookies(localeCookieName);
6939

70-
const setLocale = (id) => {
71-
if (!id) {
72-
// eslint-disable-next-line no-param-reassign
73-
removeCookie(localeCookieName);
74-
return;
75-
}
76-
77-
setCookie(localeCookieName, id);
78-
};
40+
const setLocale = (id) => !id ? removeCookie(localeCookieName) : setCookie(localeCookieName, id);
7941

8042
const getCountry = () => getCookies(countryCookieName);
8143

82-
const setCountry = (id) => {
83-
if (!id) {
84-
// eslint-disable-next-line no-param-reassign
85-
removeCookie(countryCookieName);
86-
return;
87-
}
88-
89-
setCookie(countryCookieName, id);
90-
};
44+
const setCountry = id => !id ? removeCookie(countryCookieName) : setCookie(countryCookieName, id);
9145

9246
const settings = mapConfigToSetupObject({
9347
moduleOptions,

packages/theme/middleware/__tests__/i18n.spec.js

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import i18nMiddleware, { readStoreCookie, findLocaleBasedOnStoreCode, setDefaultLocale, prepareNewCookieString } from '~/middleware/i18n';
1+
import i18nMiddleware from '~/middleware/i18n';
22

33
const localesMock = [
44
{
@@ -18,6 +18,13 @@ const localesMock = [
1818
},
1919
];
2020

21+
const apiStateMock = {
22+
getCurrency: () => 'USD',
23+
getCountry: () => 'PL',
24+
getCartId: () => '123',
25+
getCustomerToken: () => '12fg45',
26+
};
27+
2128
const appMock = {
2229
$cookies: {
2330
get: jest.fn(),
@@ -27,61 +34,50 @@ const appMock = {
2734
setLocale: jest.fn(),
2835
locales: localesMock,
2936
},
30-
};
31-
32-
const apiStateMock = {
33-
getCurrency: () => 'USD',
34-
getCountry: () => 'PL',
35-
getCartId: () => '123',
36-
getCustomerToken: () => '12fg45',
37+
$vsf: {
38+
$magento: {
39+
config: {
40+
state: {
41+
...apiStateMock,
42+
setStore: jest.fn(),
43+
setLocale: jest.fn(),
44+
},
45+
axios: {
46+
headers: {
47+
cookie: null,
48+
},
49+
},
50+
},
51+
},
52+
},
3753
};
3854

3955
describe('i18n middleware', () => {
56+
beforeEach(() => {
57+
jest.resetAllMocks();
58+
});
59+
4060
it('Should read vsf-store cookie value', () => {
41-
readStoreCookie(appMock);
61+
i18nMiddleware({ app: appMock });
4262

4363
expect(appMock.$cookies.get).toHaveBeenCalledWith('vsf-store');
4464
});
4565

4666
it('Should find locale based on magento store code', () => {
47-
const storeCode = 'nl_NL';
48-
const localeObject = findLocaleBasedOnStoreCode(storeCode, localesMock);
49-
50-
expect(localeObject.code).toEqual('nl_NL');
51-
});
52-
53-
it('Should not find when there is no locale that match given magento store code', () => {
54-
const storeCode = 'pl_PL';
55-
const localeObject = findLocaleBasedOnStoreCode(storeCode, localesMock);
56-
57-
expect(localeObject).toBeFalsy();
58-
});
59-
60-
it('Should set default locale', async () => {
61-
await setDefaultLocale(appMock.i18n);
62-
63-
expect(appMock.i18n.setLocale).toHaveBeenCalledWith('en');
64-
});
65-
66-
it('Should prepare new cookie string', () => {
67-
const cookie = prepareNewCookieString(apiStateMock, 'brandZ');
67+
appMock.$cookies.get.mockReturnValue('default');
68+
i18nMiddleware({ app: appMock });
6869

69-
expect(cookie).toMatchInlineSnapshot(
70-
`"vsf-store=brandZ; vsf-locale=brandZ; vsf-currency=USD; vsf-country=PL; vsf-customer=12fg45; vsf-cart=123 "`
71-
);
70+
expect(appMock.i18n.setLocale).not.toHaveBeenCalled();
7271
});
7372

74-
it('Should set default locale if store cookie is not exist', () => {
75-
appMock.$cookies.get.mockReturnValueOnce(null);
76-
77-
i18nMiddleware({ app: appMock });
73+
it('Should set default locale when there is no locale that match given magento store code', () => {
74+
appMock.$cookies.get.mockReturnValue('pl_PL');
7875

79-
expect(appMock.i18n.setLocale).toHaveBeenCalledWith('en');
76+
expect(appMock.i18n.setLocale).not.toHaveBeenCalledTimes(1);
8077
});
8178

82-
it('Should set default locale if passed store code does not match aby locale from nuxt config', () => {
83-
appMock.$cookies.get.mockReturnValueOnce('test');
84-
79+
it('Should set default locale when vsf-store cookie is not exist', () => {
80+
appMock.$cookies.get.mockReturnValue(null);
8581
i18nMiddleware({ app: appMock });
8682

8783
expect(appMock.i18n.setLocale).toHaveBeenCalledWith('en');
@@ -94,22 +90,6 @@ describe('i18n middleware', () => {
9490
...appMock.i18n,
9591
locale: 'de_DE',
9692
},
97-
$vsf: {
98-
$magento: {
99-
config: {
100-
state: {
101-
...apiStateMock,
102-
setStore: jest.fn(),
103-
setLocale: jest.fn(),
104-
},
105-
axios: {
106-
headers: {
107-
cookie: null,
108-
},
109-
},
110-
},
111-
},
112-
},
11393
};
11494

11595
testCaseAppMock.$cookies.get.mockReturnValueOnce('de_DE').mockReturnValueOnce('default');
@@ -119,7 +99,6 @@ describe('i18n middleware', () => {
11999
expect(testCaseAppMock.$vsf.$magento.config.state.setLocale).toHaveBeenCalledWith('de_DE');
120100
expect(testCaseAppMock.$vsf.$magento.config.state.setStore).toHaveBeenCalledWith('de_DE');
121101
expect(testCaseAppMock.$vsf.$magento.config.axios.headers.cookie).toMatchInlineSnapshot(
122-
'"vsf-store=de_DE vsf-locale=de_DE vsf-currency=USD vsf-country=PL vsf-cart=123 vsf-customer=12fg45 "',
123102
`"vsf-store=de_DE; vsf-locale=de_DE; vsf-currency=USD; vsf-country=PL; vsf-customer=12fg45; vsf-cart=123 "`
124103
);
125104
});

packages/theme/middleware/i18n.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ import cookieNames from '~/enums/cookieNameEnum';
66
* @param app {NuxtAppOptions} - Nuxt App options object
77
* @returns {string} - vsf-store cookie value
88
*/
9-
export const readStoreCookie = (app) => app.$cookies.get(cookieNames.storeCookieName);
9+
const readStoreCookie = (app) => app.$cookies.get(cookieNames.storeCookieName);
1010

1111
/**
1212
* Find locale code based on magento store code
1313
* @param storeCode {string} - magento store code
1414
* @param locales {array} - array with locales
1515
* @returns boolean
1616
*/
17-
export const findLocaleBasedOnStoreCode = (storeCode, locales) => locales.find((locale) => locale.code === storeCode);
17+
const findLocaleBasedOnStoreCode = (storeCode, locales) => locales.find((locale) => locale.code === storeCode);
1818

1919
/**
2020
* Set default locale
2121
* @param i18n {i18n} i18n API
2222
* @returns {Promise<void>}
2323
*/
24-
export const setDefaultLocale = async (i18n) => {
24+
const setDefaultLocale = async (i18n) => {
2525
await i18n.setLocale(i18n.defaultLocale);
2626
};
2727

@@ -31,7 +31,7 @@ export const setDefaultLocale = async (i18n) => {
3131
* @param apiState {ConfigState}
3232
* @returns {string}
3333
*/
34-
export const prepareNewCookieString = (apiState, newStoreCode) => {
34+
const prepareNewCookieString = (apiState, newStoreCode) => {
3535
let cookie = `vsf-store=${newStoreCode}; `;
3636

3737
cookie += `vsf-locale=${newStoreCode}; `;
@@ -50,24 +50,15 @@ export const prepareNewCookieString = (apiState, newStoreCode) => {
5050
cookie += `vsf-cart=${cartIdCookie} `;
5151
}
5252

53-
console.log(cookie)
54-
5553
return cookie;
5654
};
5755

5856
export default async ({ app }) => {
5957
const { i18n } = app;
6058
const currentStoreCode = readStoreCookie(app);
6159

62-
if (!currentStoreCode) {
63-
await setDefaultLocale(i18n);
64-
65-
return;
66-
}
67-
68-
const i18nLocaleObject = findLocaleBasedOnStoreCode(currentStoreCode, i18n.locales);
69-
70-
if (!i18nLocaleObject) {
60+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
61+
if (!currentStoreCode || !findLocaleBasedOnStoreCode(currentStoreCode, i18n.locales)) {
7162
await setDefaultLocale(i18n);
7263

7364
return;

packages/theme/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"scripts": {
1111
"build": "nuxt build -m",
1212
"build:analyze": "nuxt build -a -m",
13-
"dev": "nuxt --inspect",
13+
"dev": "nuxt",
14+
"dev-debug": "nuxt --inspect",
1415
"generate": "nuxt generate",
1516
"lint": "eslint . --ext .ts,.vue",
1617
"lint:fix": "eslint . --ext .ts,.vue --fix",

0 commit comments

Comments
 (0)