188 lines
4.2 KiB
JavaScript
Raw Normal View History

2021-02-15 11:24:28 +01:00
'use strict';
const {
assignDefaultLocale,
syncLocalizations,
syncNonLocalizedAttributes,
} = 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',
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,
['content-types']: contentTypes,
},
},
},
};
};
2021-02-15 11:24:28 +01:00
describe('localizations service', () => {
describe('assignDefaultLocale', () => {
test('Does not change the input if locale is already defined', async () => {
2021-03-16 15:51:00 +01:00
setGlobalStrapi();
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();
const getDefaultLocaleMock = jest.fn(() => 'defaultLocale');
2021-03-16 15:51:00 +01:00
global.strapi.plugins.i18n.services.locales.getDefaultLocale = getDefaultLocaleMock;
const input = {};
await assignDefaultLocale(input);
expect(input).toStrictEqual({ 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-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
});
});
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-03-16 15:51:00 +01:00
global.strapi.query = () => {
return { update };
};
const entry = {
id: 1,
locale: 'test',
title: 'Localized',
stars: 1,
localizations: [{ id: 2, locale: 'fr' }],
};
await syncNonLocalizedAttributes(entry, { model });
expect(update).toHaveBeenCalledTimes(1);
expect(update).toHaveBeenCalledWith({ id: 2 }, { stars: 1 });
});
});
2021-02-15 11:24:28 +01:00
});