mirror of
https://github.com/strapi/strapi.git
synced 2025-11-03 03:17:11 +00:00
Improve and test setCustomFieldInputSizes
This commit is contained in:
parent
151e92e1a0
commit
70e60071e4
@ -6,5 +6,5 @@ module.exports = async () => {
|
|||||||
await getService('components').syncConfigurations();
|
await getService('components').syncConfigurations();
|
||||||
await getService('content-types').syncConfigurations();
|
await getService('content-types').syncConfigurations();
|
||||||
await getService('permission').registerPermissions();
|
await getService('permission').registerPermissions();
|
||||||
getService('field-sizes').registerCustomFields();
|
getService('field-sizes').setCustomFieldInputSizes();
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,10 +1,35 @@
|
|||||||
'use strict';
|
'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', () => {
|
describe('field sizes service', () => {
|
||||||
it('should return the correct field sizes', () => {
|
it('should return the correct field sizes', () => {
|
||||||
const { getAllFieldSizes } = fieldSizesService();
|
const { getAllFieldSizes } = createFieldSizesService({ strapi });
|
||||||
const fieldSizes = getAllFieldSizes();
|
const fieldSizes = getAllFieldSizes();
|
||||||
Object.values(fieldSizes).forEach((fieldSize) => {
|
Object.values(fieldSizes).forEach((fieldSize) => {
|
||||||
expect(typeof fieldSize.isResizable).toBe('boolean');
|
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', () => {
|
it('should return the correct field size for a given type', () => {
|
||||||
const { getFieldSize } = fieldSizesService();
|
const { getFieldSize } = createFieldSizesService({ strapi });
|
||||||
const fieldSize = getFieldSize('string');
|
const fieldSize = getFieldSize('string');
|
||||||
expect(fieldSize.isResizable).toBe(true);
|
expect(fieldSize.isResizable).toBe(true);
|
||||||
expect(fieldSize.default).toBe(6);
|
expect(fieldSize.default).toBe(6);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an error if the type is not found', () => {
|
it('should throw an error if the type is not found', () => {
|
||||||
const { getFieldSize } = fieldSizesService();
|
const { getFieldSize } = createFieldSizesService({ strapi });
|
||||||
expect(() => getFieldSize('not-found')).toThrowError(
|
expect(() => getFieldSize('not-found')).toThrowError(
|
||||||
'Could not find field size for type not-found'
|
'Could not find field size for type not-found'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an error if the type is not provided', () => {
|
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');
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -44,42 +44,51 @@ const fieldSizes = {
|
|||||||
uid: defaultSize,
|
uid: defaultSize,
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = ({ strapi }) => ({
|
const createFieldSizesService = ({ strapi }) => {
|
||||||
getAllFieldSizes() {
|
const fieldSizesService = {
|
||||||
return fieldSizes;
|
getAllFieldSizes() {
|
||||||
},
|
return fieldSizes;
|
||||||
getFieldSize(type) {
|
},
|
||||||
if (!type) {
|
|
||||||
throw new Error('The type is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
const fieldSize = fieldSizes[type];
|
getFieldSize(type) {
|
||||||
if (!fieldSize) {
|
if (!type) {
|
||||||
throw new Error(`Could not find field size for type ${type}`);
|
throw new Error('The type is required');
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
},
|
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;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user