157 lines
4.2 KiB
JavaScript
Raw Normal View History

2021-02-25 17:40:14 +01:00
'use strict';
2021-03-11 10:41:35 +01:00
const { after } = require('../field');
2021-02-25 17:40:14 +01:00
describe('i18n - Migration - disable localization on a field', () => {
2021-03-11 10:41:35 +01:00
describe('after', () => {
2021-02-25 17:40:14 +01:00
describe('Should not migrate', () => {
test("Doesn't migrate if model isn't localized", async () => {
const find = jest.fn();
global.strapi = {
query: () => {
find;
},
2021-03-11 10:41:35 +01:00
plugins: {
i18n: {
services: {
'content-types': {
isLocalized: () => false,
},
},
},
2021-02-25 17:40:14 +01:00
},
};
2021-03-11 10:41:35 +01:00
const model = {};
const previousDefinition = {};
2021-02-25 17:40:14 +01:00
2021-03-11 10:41:35 +01:00
await after({ model, definition: model, previousDefinition });
2021-02-25 17:40:14 +01:00
expect(find).not.toHaveBeenCalled();
});
test("Doesn't migrate if no attribute changed (without i18n)", async () => {
const find = jest.fn();
2021-03-11 10:41:35 +01:00
const getLocalizedFields = jest
.fn()
.mockReturnValueOnce([])
.mockReturnValueOnce([]);
2021-02-25 17:40:14 +01:00
global.strapi = {
query: () => {
find;
},
2021-03-11 10:41:35 +01:00
plugins: {
i18n: {
services: {
'content-types': {
isLocalized: () => true,
getLocalizedFields,
},
},
},
2021-02-25 17:40:14 +01:00
},
};
2021-03-11 10:41:35 +01:00
const model = { attributes: { name: {} } };
const previousDefinition = { attributes: { name: {} } };
2021-02-25 17:40:14 +01:00
2021-03-11 10:41:35 +01:00
await after({ model, definition: model, previousDefinition });
expect(getLocalizedFields).toHaveBeenCalledTimes(2);
2021-02-25 17:40:14 +01:00
expect(find).not.toHaveBeenCalled();
});
test("Doesn't migrate if no attribute changed (with i18n)", async () => {
const find = jest.fn();
2021-03-11 10:41:35 +01:00
const getLocalizedFields = jest
.fn()
.mockReturnValueOnce(['name'])
.mockReturnValueOnce(['name']);
2021-02-25 17:40:14 +01:00
global.strapi = {
query: () => {
find;
},
2021-03-11 10:41:35 +01:00
plugins: {
i18n: {
services: {
'content-types': {
isLocalized: () => true,
getLocalizedFields,
},
},
2021-02-25 17:40:14 +01:00
},
},
};
2021-03-11 10:41:35 +01:00
const model = { attributes: { name: {} } };
const previousDefinition = { attributes: { name: {} } };
2021-02-25 17:40:14 +01:00
2021-03-11 10:41:35 +01:00
await after({ model, definition: model, previousDefinition });
expect(getLocalizedFields).toHaveBeenCalledTimes(2);
2021-02-25 17:40:14 +01:00
expect(find).not.toHaveBeenCalled();
});
2021-03-11 10:41:35 +01:00
test("Doesn't migrate if field become localized", async () => {
2021-02-25 17:40:14 +01:00
const find = jest.fn();
2021-03-11 10:41:35 +01:00
const getLocalizedFields = jest
.fn()
.mockReturnValueOnce(['name'])
.mockReturnValueOnce([]);
2021-02-25 17:40:14 +01:00
global.strapi = {
query: () => {
find;
},
2021-03-11 10:41:35 +01:00
plugins: {
i18n: {
services: {
'content-types': {
isLocalized: () => true,
getLocalizedFields,
},
},
2021-02-25 17:40:14 +01:00
},
},
};
2021-03-11 10:41:35 +01:00
const model = { attributes: { name: {} } };
const previousDefinition = { attributes: { name: {} } };
2021-02-25 17:40:14 +01:00
2021-03-11 10:41:35 +01:00
await after({ model, definition: model, previousDefinition });
expect(getLocalizedFields).toHaveBeenCalledTimes(2);
2021-02-25 17:40:14 +01:00
expect(find).not.toHaveBeenCalled();
});
2021-03-11 10:41:35 +01:00
test("Doesn't migrate if field is deleted", async () => {
2021-02-25 17:40:14 +01:00
const find = jest.fn();
2021-03-11 10:41:35 +01:00
const getLocalizedFields = jest
.fn()
.mockReturnValueOnce([])
.mockReturnValueOnce(['name']);
2021-02-25 17:40:14 +01:00
global.strapi = {
query: () => {
find;
},
2021-03-11 10:41:35 +01:00
plugins: {
i18n: {
services: {
'content-types': {
isLocalized: () => true,
getLocalizedFields,
},
},
2021-02-25 17:40:14 +01:00
},
},
};
2021-03-11 10:41:35 +01:00
const model = { attributes: {} };
const previousDefinition = { attributes: { name: {} } };
2021-02-25 17:40:14 +01:00
2021-03-11 10:41:35 +01:00
await after({ model, definition: model, previousDefinition });
expect(getLocalizedFields).toHaveBeenCalledTimes(2);
2021-02-25 17:40:14 +01:00
expect(find).not.toHaveBeenCalled();
});
});
});
});