Add tests + rename + refactor

This commit is contained in:
Pierre Noël 2023-02-10 11:10:13 +01:00
parent b07ae9750d
commit 99c170e6bf
3 changed files with 34 additions and 14 deletions

View File

@ -14,10 +14,10 @@ const registerModelsHooks = () => {
strapi.db.lifecycles.subscribe({
models: i18nModelUIDs,
async beforeCreate(event) {
await getService('localizations').assignDefaultLocale(event.params.data);
await getService('localizations').assignDefaultLocaleToEntries(event.params.data);
},
async beforeCreateMany(event) {
await getService('localizations').assignDefaultLocale(event.params.data);
await getService('localizations').assignDefaultLocaleToEntries(event.params.data);
},
});
}

View File

@ -1,6 +1,6 @@
'use strict';
const { assignDefaultLocale, syncLocalizations, syncNonLocalizedAttributes } =
const { assignDefaultLocaleToEntries, syncLocalizations, syncNonLocalizedAttributes } =
require('../localizations')();
const locales = require('../locales')();
@ -69,16 +69,16 @@ const setGlobalStrapi = () => {
};
describe('localizations service', () => {
describe('assignDefaultLocale', () => {
test('Does not change the input if locale is already defined', async () => {
describe('assignDefaultLocaleToEntries', () => {
test('Does not change the input if locale is already defined (single entry)', async () => {
setGlobalStrapi();
const input = { locale: 'myLocale' };
await assignDefaultLocale(input);
await assignDefaultLocaleToEntries(input);
expect(input).toStrictEqual({ locale: 'myLocale' });
});
test('Use default locale to set the locale on the input data', async () => {
test('Use default locale to set the locale on the input data (single entry)', async () => {
setGlobalStrapi();
const getDefaultLocaleMock = jest.fn(() => 'defaultLocale');
@ -86,11 +86,33 @@ describe('localizations service', () => {
global.strapi.plugins.i18n.services.locales.getDefaultLocale = getDefaultLocaleMock;
const input = {};
await assignDefaultLocale(input);
await assignDefaultLocaleToEntries(input);
expect(input).toStrictEqual({ locale: 'defaultLocale' });
expect(getDefaultLocaleMock).toHaveBeenCalled();
});
test('Does not change the input if locale is already defined (multiple entries)', async () => {
setGlobalStrapi();
const input = [{ locale: 'myLocale' }, { locale: 'mylocalebis' }];
await assignDefaultLocaleToEntries(input);
expect(input).toStrictEqual([{ locale: 'myLocale' }, { locale: 'mylocalebis' }]);
});
test('Use default locale to set the locale on the entries missing it (multiple entries)', async () => {
setGlobalStrapi();
const getDefaultLocaleMock = jest.fn(() => 'defaultLocale');
global.strapi.plugins.i18n.services.locales.getDefaultLocale = getDefaultLocaleMock;
const input = [{ locale: 'mylocale' }, {}];
await assignDefaultLocaleToEntries(input);
expect(input).toStrictEqual([{ locale: 'mylocale' }, { locale: 'defaultLocale' }]);
expect(getDefaultLocaleMock).toHaveBeenCalled();
});
});
describe('syncLocalizations', () => {

View File

@ -8,17 +8,15 @@ const { getService } = require('../utils');
* Adds the default locale to an object if it isn't defined yet
* @param {Object} data a data object before being persisted into db
*/
const assignDefaultLocale = async (data) => {
const assignDefaultLocaleToEntries = async (data) => {
const { getDefaultLocale } = getService('locales');
if (isArray(data) && data.some((entry) => !entry.locale)) {
const defaultLocale = await getDefaultLocale();
data.forEach((entry) => {
if (isNil(entry.locale)) {
entry.locale = defaultLocale;
}
entry.locale = entry.locale || defaultLocale;
});
} else if (isNil(data.locale)) {
} else if (!isArray(data) && isNil(data.locale)) {
data.locale = await getDefaultLocale();
}
};
@ -72,7 +70,7 @@ const syncNonLocalizedAttributes = async (entry, { model }) => {
};
module.exports = () => ({
assignDefaultLocale,
assignDefaultLocaleToEntries,
syncLocalizations,
syncNonLocalizedAttributes,
});