Improve and test setCustomFieldInputSizes

This commit is contained in:
Rémi de Juvigny 2023-04-21 10:19:30 +02:00
parent 151e92e1a0
commit 70e60071e4
3 changed files with 88 additions and 43 deletions

View File

@ -6,5 +6,5 @@ module.exports = async () => {
await getService('components').syncConfigurations();
await getService('content-types').syncConfigurations();
await getService('permission').registerPermissions();
getService('field-sizes').registerCustomFields();
getService('field-sizes').setCustomFieldInputSizes();
};

View File

@ -1,10 +1,35 @@
'use strict';
const fieldSizesService = require('../field-sizes');
const createFieldSizesService = require('../field-sizes');
const strapi = {
container: {
// Mock container.get('custom-fields')
get: jest.fn(() => ({
// Mock container.get('custom-fields').getAll()
getAll: jest.fn(() => ({
'plugin::mycustomfields.color': {
name: 'color',
plugin: 'mycustomfields',
type: 'string',
},
'plugin::mycustomfields.smallColor': {
name: 'smallColor',
plugin: 'mycustomfields',
type: 'string',
inputSize: {
default: 4,
isResizable: false,
},
},
})),
})),
},
};
describe('field sizes service', () => {
it('should return the correct field sizes', () => {
const { getAllFieldSizes } = fieldSizesService();
const { getAllFieldSizes } = createFieldSizesService({ strapi });
const fieldSizes = getAllFieldSizes();
Object.values(fieldSizes).forEach((fieldSize) => {
expect(typeof fieldSize.isResizable).toBe('boolean');
@ -13,21 +38,32 @@ describe('field sizes service', () => {
});
it('should return the correct field size for a given type', () => {
const { getFieldSize } = fieldSizesService();
const { getFieldSize } = createFieldSizesService({ strapi });
const fieldSize = getFieldSize('string');
expect(fieldSize.isResizable).toBe(true);
expect(fieldSize.default).toBe(6);
});
it('should throw an error if the type is not found', () => {
const { getFieldSize } = fieldSizesService();
const { getFieldSize } = createFieldSizesService({ strapi });
expect(() => getFieldSize('not-found')).toThrowError(
'Could not find field size for type not-found'
);
});
it('should throw an error if the type is not provided', () => {
const { getFieldSize } = fieldSizesService();
const { getFieldSize } = createFieldSizesService({ strapi });
expect(() => getFieldSize()).toThrowError('The type is required');
});
it('should set the custom fields input sizes', () => {
const { setCustomFieldInputSizes, getAllFieldSizes } = createFieldSizesService({ strapi });
setCustomFieldInputSizes();
const fieldSizes = getAllFieldSizes();
console.log(fieldSizes);
expect(fieldSizes).not.toHaveProperty('plugin::mycustomfields.color');
expect(fieldSizes['plugin::mycustomfields.smallColor'].default).toBe(4);
expect(fieldSizes['plugin::mycustomfields.smallColor'].isResizable).toBe(false);
});
});

View File

@ -44,42 +44,51 @@ const fieldSizes = {
uid: defaultSize,
};
module.exports = ({ strapi }) => ({
getAllFieldSizes() {
return fieldSizes;
},
getFieldSize(type) {
if (!type) {
throw new Error('The type is required');
}
const createFieldSizesService = ({ strapi }) => {
const fieldSizesService = {
getAllFieldSizes() {
return fieldSizes;
},
const fieldSize = fieldSizes[type];
if (!fieldSize) {
throw new Error(`Could not find field size for type ${type}`);
}
return fieldSize;
},
setFieldSize(type, size) {
if (!type) {
throw new Error('The type is required');
}
if (!size) {
throw new Error('The size is required');
}
fieldSizes[type] = size;
},
registerCustomFields() {
// Find all custom fields already registered
const customFields = strapi.container.get('custom-fields').getAll();
// If they have a custom field size, register it
Object.entries(customFields).forEach(([uid, customField]) => {
if (customField.inputSize) {
this.setFieldSize(uid, customField.inputSize);
getFieldSize(type) {
if (!type) {
throw new Error('The type is required');
}
});
},
});
const fieldSize = fieldSizes[type];
if (!fieldSize) {
throw new Error(`Could not find field size for type ${type}`);
}
return fieldSize;
},
setFieldSize(type, size) {
if (!type) {
throw new Error('The type is required');
}
if (!size) {
throw new Error('The size is required');
}
fieldSizes[type] = size;
},
setCustomFieldInputSizes() {
// Find all custom fields already registered
const customFields = strapi.container.get('custom-fields').getAll();
// If they have a custom field size, register it
Object.entries(customFields).forEach(([uid, customField]) => {
if (customField.inputSize) {
fieldSizesService.setFieldSize(uid, customField.inputSize);
}
});
},
};
return fieldSizesService;
};
module.exports = createFieldSizesService;