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({ strapi.db.lifecycles.subscribe({
models: i18nModelUIDs, models: i18nModelUIDs,
async beforeCreate(event) { async beforeCreate(event) {
await getService('localizations').assignDefaultLocale(event.params.data); await getService('localizations').assignDefaultLocaleToEntries(event.params.data);
}, },
async beforeCreateMany(event) { 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'; 'use strict';
const { assignDefaultLocale, syncLocalizations, syncNonLocalizedAttributes } = const { assignDefaultLocaleToEntries, syncLocalizations, syncNonLocalizedAttributes } =
require('../localizations')(); require('../localizations')();
const locales = require('../locales')(); const locales = require('../locales')();
@ -69,16 +69,16 @@ const setGlobalStrapi = () => {
}; };
describe('localizations service', () => { describe('localizations service', () => {
describe('assignDefaultLocale', () => { describe('assignDefaultLocaleToEntries', () => {
test('Does not change the input if locale is already defined', async () => { test('Does not change the input if locale is already defined (single entry)', async () => {
setGlobalStrapi(); setGlobalStrapi();
const input = { locale: 'myLocale' }; const input = { locale: 'myLocale' };
await assignDefaultLocale(input); await assignDefaultLocaleToEntries(input);
expect(input).toStrictEqual({ locale: 'myLocale' }); 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(); setGlobalStrapi();
const getDefaultLocaleMock = jest.fn(() => 'defaultLocale'); const getDefaultLocaleMock = jest.fn(() => 'defaultLocale');
@ -86,11 +86,33 @@ describe('localizations service', () => {
global.strapi.plugins.i18n.services.locales.getDefaultLocale = getDefaultLocaleMock; global.strapi.plugins.i18n.services.locales.getDefaultLocale = getDefaultLocaleMock;
const input = {}; const input = {};
await assignDefaultLocale(input); await assignDefaultLocaleToEntries(input);
expect(input).toStrictEqual({ locale: 'defaultLocale' }); expect(input).toStrictEqual({ locale: 'defaultLocale' });
expect(getDefaultLocaleMock).toHaveBeenCalled(); 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', () => { 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 * Adds the default locale to an object if it isn't defined yet
* @param {Object} data a data object before being persisted into db * @param {Object} data a data object before being persisted into db
*/ */
const assignDefaultLocale = async (data) => { const assignDefaultLocaleToEntries = async (data) => {
const { getDefaultLocale } = getService('locales'); const { getDefaultLocale } = getService('locales');
if (isArray(data) && data.some((entry) => !entry.locale)) { if (isArray(data) && data.some((entry) => !entry.locale)) {
const defaultLocale = await getDefaultLocale(); const defaultLocale = await getDefaultLocale();
data.forEach((entry) => { data.forEach((entry) => {
if (isNil(entry.locale)) { entry.locale = entry.locale || defaultLocale;
entry.locale = defaultLocale;
}
}); });
} else if (isNil(data.locale)) { } else if (!isArray(data) && isNil(data.locale)) {
data.locale = await getDefaultLocale(); data.locale = await getDefaultLocale();
} }
}; };
@ -72,7 +70,7 @@ const syncNonLocalizedAttributes = async (entry, { model }) => {
}; };
module.exports = () => ({ module.exports = () => ({
assignDefaultLocale, assignDefaultLocaleToEntries,
syncLocalizations, syncLocalizations,
syncNonLocalizedAttributes, syncNonLocalizedAttributes,
}); });