217 lines
5.3 KiB
JavaScript
Raw Normal View History

2021-02-15 11:24:28 +01:00
'use strict';
2023-02-10 11:10:13 +01:00
const { assignDefaultLocaleToEntries, syncLocalizations, syncNonLocalizedAttributes } =
2022-08-08 23:33:39 +02:00
require('../localizations')();
2021-02-15 11:24:28 +01:00
2021-08-02 18:09:29 +02:00
const locales = require('../locales')();
const contentTypes = require('../content-types')();
2021-03-16 15:51:00 +01:00
2021-02-15 11:24:28 +01:00
const model = {
uid: 'test-model',
pluginOptions: {
i18n: {
localized: true,
},
},
attributes: {
title: {
type: 'string',
pluginOptions: {
i18n: {
localized: true,
},
},
},
stars: {
type: 'integer',
},
},
};
const allLocalizedModel = {
uid: 'test-model',
pluginOptions: {
i18n: {
localized: true,
},
},
2021-02-15 11:24:28 +01:00
attributes: {
title: {
type: 'string',
pluginOptions: {
i18n: {
localized: true,
},
},
},
stars: {
2021-02-16 12:23:51 +01:00
type: 'integer',
pluginOptions: {
i18n: {
localized: true,
},
},
2021-02-15 11:24:28 +01:00
},
},
};
2021-03-16 15:51:00 +01:00
const setGlobalStrapi = () => {
global.strapi = {
plugins: {
i18n: {
services: {
locales,
2021-03-16 17:33:10 +01:00
'content-types': contentTypes,
2021-03-16 15:51:00 +01:00
},
},
},
db: {
dialect: {
client: 'sqlite',
},
},
2021-03-16 15:51:00 +01:00
};
};
2021-02-15 11:24:28 +01:00
describe('localizations service', () => {
2023-02-10 11:10:13 +01:00
describe('assignDefaultLocaleToEntries', () => {
test('Does not change the input if locale is already defined (single entry)', async () => {
2021-03-16 15:51:00 +01:00
setGlobalStrapi();
const input = { locale: 'myLocale' };
2023-02-10 11:10:13 +01:00
await assignDefaultLocaleToEntries(input);
expect(input).toStrictEqual({ locale: 'myLocale' });
});
2023-02-10 11:10:13 +01:00
test('Use default locale to set the locale on the input data (single entry)', async () => {
2021-03-16 15:51:00 +01:00
setGlobalStrapi();
const getDefaultLocaleMock = jest.fn(() => 'defaultLocale');
2021-03-16 15:51:00 +01:00
global.strapi.plugins.i18n.services.locales.getDefaultLocale = getDefaultLocaleMock;
const input = {};
2023-02-10 11:10:13 +01:00
await assignDefaultLocaleToEntries(input);
expect(input).toStrictEqual({ locale: 'defaultLocale' });
expect(getDefaultLocaleMock).toHaveBeenCalled();
});
2023-02-10 11:10:13 +01:00
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', () => {
test('Updates every other localizations with correct ids', async () => {
2021-03-16 15:51:00 +01:00
setGlobalStrapi();
2021-02-15 11:24:28 +01:00
const update = jest.fn();
2021-03-16 15:51:00 +01:00
global.strapi.query = () => {
return { update };
2021-02-15 11:24:28 +01:00
};
const localizations = [{ id: 2 }, { id: 3 }];
const entry = { id: 1, locale: 'test', localizations };
2021-02-15 11:24:28 +01:00
await syncLocalizations(entry, { model });
2021-02-15 11:24:28 +01:00
expect(update).toHaveBeenCalledTimes(localizations.length);
2021-07-19 19:36:40 +02:00
expect(update).toHaveBeenNthCalledWith(1, {
where: { id: 2 },
data: { localizations: [1, 3] },
});
expect(update).toHaveBeenNthCalledWith(2, {
where: { id: 3 },
data: { localizations: [1, 2] },
});
2021-02-15 11:24:28 +01:00
});
});
describe('syncNonLocalizedAttributes', () => {
2021-02-15 11:24:28 +01:00
test('Does nothing if no localizations set', async () => {
2021-03-16 15:51:00 +01:00
setGlobalStrapi();
2021-02-15 11:24:28 +01:00
const update = jest.fn();
2021-03-16 15:51:00 +01:00
global.strapi.query = () => {
return { update };
2021-02-15 11:24:28 +01:00
};
const entry = { id: 1, locale: 'test' };
await syncNonLocalizedAttributes(entry, { model });
2021-02-15 11:24:28 +01:00
expect(update).not.toHaveBeenCalled();
});
test('Does not update the current locale', async () => {
2021-03-16 15:51:00 +01:00
setGlobalStrapi();
2021-02-15 11:24:28 +01:00
const update = jest.fn();
2021-03-16 15:51:00 +01:00
global.strapi.query = () => {
return { update };
2021-02-15 11:24:28 +01:00
};
const entry = { id: 1, locale: 'test', localizations: [] };
2021-02-15 11:24:28 +01:00
await syncNonLocalizedAttributes(entry, { model });
2021-02-15 11:24:28 +01:00
expect(update).not.toHaveBeenCalled();
});
test('Does not update if all the fields are localized', async () => {
2021-03-16 15:51:00 +01:00
setGlobalStrapi();
const update = jest.fn();
2021-03-16 15:51:00 +01:00
global.strapi.query = () => {
return { update };
};
const entry = { id: 1, locale: 'test', localizations: [] };
await syncNonLocalizedAttributes(entry, { model: allLocalizedModel });
expect(update).not.toHaveBeenCalled();
});
test('Updates locales with non localized fields only', async () => {
2021-03-16 15:51:00 +01:00
setGlobalStrapi();
const update = jest.fn();
2021-08-18 16:57:18 +02:00
global.strapi.entityService = { update };
const entry = {
id: 1,
locale: 'test',
title: 'Localized',
stars: 1,
localizations: [{ id: 2, locale: 'fr' }],
};
await syncNonLocalizedAttributes(entry, { model });
expect(update).toHaveBeenCalledTimes(1);
2021-08-18 16:57:18 +02:00
expect(update).toHaveBeenCalledWith(model.uid, 2, { data: { stars: 1 } });
});
});
2021-02-15 11:24:28 +01:00
});