Set global strapi in tests

This commit is contained in:
Alexandre Bodin 2021-03-16 15:51:00 +01:00
parent 228899a9b3
commit 221c3d40e4
3 changed files with 54 additions and 35 deletions

View File

@ -9,6 +9,8 @@ jest.mock('../localizations', () => {
const { decorator } = require('../entity-service-decorator'); const { decorator } = require('../entity-service-decorator');
const { syncLocalizations, syncNonLocalizedAttributes } = require('../localizations'); const { syncLocalizations, syncNonLocalizedAttributes } = require('../localizations');
const locales = require('../locales');
const contentTypes = require('../content-types');
const model = { const model = {
pluginOptions: { pluginOptions: {
@ -34,6 +36,14 @@ const models = {
describe('Entity service decorator', () => { describe('Entity service decorator', () => {
beforeAll(() => { beforeAll(() => {
global.strapi = { global.strapi = {
plugins: {
i18n: {
services: {
locales,
['content-types']: contentTypes,
},
},
},
query() { query() {
return { return {
create() {}, create() {},

View File

@ -6,6 +6,9 @@ const {
syncNonLocalizedAttributes, syncNonLocalizedAttributes,
} = require('../localizations'); } = require('../localizations');
const locales = require('../locales');
const contentTypes = require('../content-types');
const model = { const model = {
uid: 'test-model', uid: 'test-model',
pluginOptions: { pluginOptions: {
@ -55,9 +58,23 @@ const allLocalizedModel = {
}, },
}; };
const setGlobalStrapi = () => {
global.strapi = {
plugins: {
i18n: {
services: {
locales,
['content-types']: contentTypes,
},
},
},
};
};
describe('localizations service', () => { describe('localizations service', () => {
describe('assignDefaultLocale', () => { describe('assignDefaultLocale', () => {
test('Does not change the input if locale is already defined', async () => { test('Does not change the input if locale is already defined', async () => {
setGlobalStrapi();
const input = { locale: 'myLocale' }; const input = { locale: 'myLocale' };
await assignDefaultLocale(input); await assignDefaultLocale(input);
@ -65,19 +82,11 @@ describe('localizations service', () => {
}); });
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', async () => {
setGlobalStrapi();
const getDefaultLocaleMock = jest.fn(() => 'defaultLocale'); const getDefaultLocaleMock = jest.fn(() => 'defaultLocale');
global.strapi = { global.strapi.plugins.i18n.services.locales.getDefaultLocale = getDefaultLocaleMock;
plugins: {
i18n: {
services: {
locales: {
getDefaultLocale: getDefaultLocaleMock,
},
},
},
},
};
const input = {}; const input = {};
await assignDefaultLocale(input); await assignDefaultLocale(input);
@ -89,11 +98,11 @@ describe('localizations service', () => {
describe('syncLocalizations', () => { describe('syncLocalizations', () => {
test('Updates every other localizations with correct ids', async () => { test('Updates every other localizations with correct ids', async () => {
setGlobalStrapi();
const update = jest.fn(); const update = jest.fn();
global.strapi = { global.strapi.query = () => {
query() {
return { update }; return { update };
},
}; };
const localizations = [{ id: 2 }, { id: 3 }]; const localizations = [{ id: 2 }, { id: 3 }];
@ -109,11 +118,11 @@ describe('localizations service', () => {
describe('syncNonLocalizedAttributes', () => { describe('syncNonLocalizedAttributes', () => {
test('Does nothing if no localizations set', async () => { test('Does nothing if no localizations set', async () => {
setGlobalStrapi();
const update = jest.fn(); const update = jest.fn();
global.strapi = { global.strapi.query = () => {
query() {
return { update }; return { update };
},
}; };
const entry = { id: 1, locale: 'test' }; const entry = { id: 1, locale: 'test' };
@ -124,11 +133,11 @@ describe('localizations service', () => {
}); });
test('Does not update the current locale', async () => { test('Does not update the current locale', async () => {
setGlobalStrapi();
const update = jest.fn(); const update = jest.fn();
global.strapi = { global.strapi.query = () => {
query() {
return { update }; return { update };
},
}; };
const entry = { id: 1, locale: 'test', localizations: [] }; const entry = { id: 1, locale: 'test', localizations: [] };
@ -139,11 +148,11 @@ describe('localizations service', () => {
}); });
test('Does not update if all the fields are localized', async () => { test('Does not update if all the fields are localized', async () => {
setGlobalStrapi();
const update = jest.fn(); const update = jest.fn();
global.strapi = { global.strapi.query = () => {
query() {
return { update }; return { update };
},
}; };
const entry = { id: 1, locale: 'test', localizations: [] }; const entry = { id: 1, locale: 'test', localizations: [] };
@ -154,11 +163,11 @@ describe('localizations service', () => {
}); });
test('Updates locales with non localized fields only', async () => { test('Updates locales with non localized fields only', async () => {
setGlobalStrapi();
const update = jest.fn(); const update = jest.fn();
global.strapi = { global.strapi.query = () => {
query() {
return { update }; return { update };
},
}; };
const entry = { const entry = {

View File

@ -52,15 +52,15 @@ const createLocalizationHandler = contentType => {
const { findByCode } = getService('locales'); const { findByCode } = getService('locales');
if (!has('locale', data)) {
return ctx.badRequest('locale.missing');
}
const matchingLocale = await findByCode(data.locale); const matchingLocale = await findByCode(data.locale);
if (!matchingLocale) { if (!matchingLocale) {
return ctx.badRequest("This locale doesn't exist"); return ctx.badRequest("This locale doesn't exist");
} }
if (!has('locale', data)) {
return ctx.badRequest('locale.missing');
}
const usedLocales = getAllLocales(entry); const usedLocales = getAllLocales(entry);
if (usedLocales.includes(data.locale)) { if (usedLocales.includes(data.locale)) {
return ctx.badRequest('locale.already.used'); return ctx.badRequest('locale.already.used');