2021-02-15 11:24:28 +01:00
|
|
|
'use strict';
|
|
|
|
|
2021-02-17 13:00:49 +01:00
|
|
|
const {
|
|
|
|
assignDefaultLocale,
|
2021-03-08 16:39:39 +01:00
|
|
|
syncLocalizations,
|
2021-03-14 11:23:13 +01:00
|
|
|
syncNonLocalizedAttributes,
|
2021-02-17 13:00:49 +01:00
|
|
|
} = require('../localizations');
|
2021-02-15 11:24:28 +01:00
|
|
|
|
2021-03-16 15:51:00 +01:00
|
|
|
const locales = require('../locales');
|
|
|
|
const contentTypes = require('../content-types');
|
|
|
|
|
2021-02-15 11:24:28 +01:00
|
|
|
const model = {
|
|
|
|
uid: 'test-model',
|
2021-03-08 16:39:39 +01:00
|
|
|
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',
|
2021-03-08 16:39:39 +01:00
|
|
|
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,
|
|
|
|
['content-types']: contentTypes,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-02-15 11:24:28 +01:00
|
|
|
describe('localizations service', () => {
|
2021-02-17 13:00:49 +01:00
|
|
|
describe('assignDefaultLocale', () => {
|
|
|
|
test('Does not change the input if locale is already defined', async () => {
|
2021-03-16 15:51:00 +01:00
|
|
|
setGlobalStrapi();
|
2021-02-17 13:00:49 +01:00
|
|
|
const input = { locale: 'myLocale' };
|
|
|
|
await assignDefaultLocale(input);
|
|
|
|
|
|
|
|
expect(input).toStrictEqual({ locale: 'myLocale' });
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Use default locale to set the locale on the input data', async () => {
|
2021-03-16 15:51:00 +01:00
|
|
|
setGlobalStrapi();
|
|
|
|
|
2021-02-17 13:00:49 +01:00
|
|
|
const getDefaultLocaleMock = jest.fn(() => 'defaultLocale');
|
|
|
|
|
2021-03-16 15:51:00 +01:00
|
|
|
global.strapi.plugins.i18n.services.locales.getDefaultLocale = getDefaultLocaleMock;
|
2021-02-17 13:00:49 +01:00
|
|
|
|
|
|
|
const input = {};
|
|
|
|
await assignDefaultLocale(input);
|
|
|
|
|
|
|
|
expect(input).toStrictEqual({ locale: 'defaultLocale' });
|
|
|
|
expect(getDefaultLocaleMock).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-08 16:39:39 +01:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2021-03-08 16:39:39 +01:00
|
|
|
const localizations = [{ id: 2 }, { id: 3 }];
|
|
|
|
const entry = { id: 1, locale: 'test', localizations };
|
2021-02-15 11:24:28 +01:00
|
|
|
|
2021-03-08 16:39:39 +01:00
|
|
|
await syncLocalizations(entry, { model });
|
2021-02-15 11:24:28 +01:00
|
|
|
|
2021-03-08 16:39:39 +01:00
|
|
|
expect(update).toHaveBeenCalledTimes(localizations.length);
|
2021-03-09 11:12:09 +01:00
|
|
|
expect(update).toHaveBeenNthCalledWith(1, { id: 2 }, { localizations: [1, 3] });
|
|
|
|
expect(update).toHaveBeenNthCalledWith(2, { id: 3 }, { localizations: [1, 2] });
|
2021-02-15 11:24:28 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-14 11:23:13 +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' };
|
|
|
|
|
2021-03-14 11:23:13 +01:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2021-03-08 16:39:39 +01:00
|
|
|
const entry = { id: 1, locale: 'test', localizations: [] };
|
2021-02-15 11:24:28 +01:00
|
|
|
|
2021-03-14 11:23:13 +01:00
|
|
|
await syncNonLocalizedAttributes(entry, { model });
|
2021-02-15 11:24:28 +01:00
|
|
|
|
|
|
|
expect(update).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2021-03-08 16:39:39 +01:00
|
|
|
test('Does not update if all the fields are localized', async () => {
|
2021-03-16 15:51:00 +01:00
|
|
|
setGlobalStrapi();
|
|
|
|
|
2021-02-17 13:00:49 +01:00
|
|
|
const update = jest.fn();
|
2021-03-16 15:51:00 +01:00
|
|
|
global.strapi.query = () => {
|
|
|
|
return { update };
|
2021-02-17 13:00:49 +01:00
|
|
|
};
|
|
|
|
|
2021-03-08 16:39:39 +01:00
|
|
|
const entry = { id: 1, locale: 'test', localizations: [] };
|
2021-02-17 13:00:49 +01:00
|
|
|
|
2021-03-14 11:23:13 +01:00
|
|
|
await syncNonLocalizedAttributes(entry, { model: allLocalizedModel });
|
2021-02-17 13:00:49 +01:00
|
|
|
|
|
|
|
expect(update).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2021-03-08 16:39:39 +01:00
|
|
|
test('Updates locales with non localized fields only', async () => {
|
2021-03-16 15:51:00 +01:00
|
|
|
setGlobalStrapi();
|
|
|
|
|
2021-02-17 13:00:49 +01:00
|
|
|
const update = jest.fn();
|
2021-03-16 15:51:00 +01:00
|
|
|
global.strapi.query = () => {
|
|
|
|
return { update };
|
2021-02-17 13:00:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const entry = {
|
|
|
|
id: 1,
|
2021-03-08 16:39:39 +01:00
|
|
|
locale: 'test',
|
|
|
|
title: 'Localized',
|
|
|
|
stars: 1,
|
|
|
|
localizations: [{ id: 2, locale: 'fr' }],
|
2021-02-17 13:00:49 +01:00
|
|
|
};
|
|
|
|
|
2021-03-14 11:23:13 +01:00
|
|
|
await syncNonLocalizedAttributes(entry, { model });
|
2021-02-17 13:00:49 +01:00
|
|
|
|
|
|
|
expect(update).toHaveBeenCalledTimes(1);
|
2021-03-08 16:39:39 +01:00
|
|
|
expect(update).toHaveBeenCalledWith({ id: 2 }, { stars: 1 });
|
2021-02-17 13:00:49 +01:00
|
|
|
});
|
|
|
|
});
|
2021-02-15 11:24:28 +01:00
|
|
|
});
|