mirror of
				https://github.com/strapi/strapi.git
				synced 2025-10-30 17:37:26 +00:00 
			
		
		
		
	Test custom field exists
This commit is contained in:
		
							parent
							
								
									05c8b0f66e
								
							
						
					
					
						commit
						6456a33bf1
					
				| @ -70,10 +70,6 @@ const formatAttribute = (key, attribute) => { | ||||
|   if (attribute.type === 'customField') { | ||||
|     const customField = strapi.container.get('custom-fields').get(attribute.customField); | ||||
| 
 | ||||
|     if (!customField) { | ||||
|       throw new Error(`Could not find Custom Field: ${attribute.customField}`); | ||||
|     } | ||||
| 
 | ||||
|     return { | ||||
|       ...attribute, | ||||
|       type: customField.type, | ||||
|  | ||||
| @ -8,99 +8,123 @@ const strapi = { | ||||
| }; | ||||
| 
 | ||||
| describe('Custom fields registry', () => { | ||||
|   it('adds a custom field registered in a plugin', () => { | ||||
|     const mockCF = { | ||||
|       name: 'test', | ||||
|       plugin: 'plugintest', | ||||
|       type: 'text', | ||||
|     }; | ||||
|   describe('add', () => { | ||||
|     it('adds a custom field registered in a plugin', () => { | ||||
|       const mockCF = { | ||||
|         name: 'test', | ||||
|         plugin: 'plugintest', | ||||
|         type: 'text', | ||||
|       }; | ||||
| 
 | ||||
|     const customFields = customFieldsRegistry(strapi); | ||||
|     customFields.add(mockCF); | ||||
|       const customFields = customFieldsRegistry(strapi); | ||||
|       customFields.add(mockCF); | ||||
| 
 | ||||
|     const expected = { | ||||
|       'plugin::plugintest.test': mockCF, | ||||
|     }; | ||||
|     expect(customFields.getAll()).toEqual(expected); | ||||
|       const expected = { | ||||
|         'plugin::plugintest.test': mockCF, | ||||
|       }; | ||||
|       expect(customFields.getAll()).toEqual(expected); | ||||
|     }); | ||||
| 
 | ||||
|     it('adds a custom field not registered in a plugin', () => { | ||||
|       const mockCF = { | ||||
|         name: 'test', | ||||
|         type: 'text', | ||||
|       }; | ||||
| 
 | ||||
|       const customFields = customFieldsRegistry(strapi); | ||||
|       customFields.add(mockCF); | ||||
| 
 | ||||
|       const expected = { | ||||
|         'global::test': mockCF, | ||||
|       }; | ||||
|       expect(customFields.getAll()).toEqual(expected); | ||||
|     }); | ||||
| 
 | ||||
|     it('requires a name key on the custom field', () => { | ||||
|       const mockCF = { | ||||
|         type: 'test', | ||||
|       }; | ||||
| 
 | ||||
|       const customFields = customFieldsRegistry(strapi); | ||||
| 
 | ||||
|       expect(() => customFields.add(mockCF)).toThrowError( | ||||
|         `Custom fields require a 'name' and 'type' key` | ||||
|       ); | ||||
|     }); | ||||
| 
 | ||||
|     it('requires a type key on the custom field', () => { | ||||
|       const mockCF = { | ||||
|         name: 'test', | ||||
|       }; | ||||
| 
 | ||||
|       const customFields = customFieldsRegistry(strapi); | ||||
| 
 | ||||
|       expect(() => customFields.add(mockCF)).toThrowError( | ||||
|         `Custom fields require a 'name' and 'type' key` | ||||
|       ); | ||||
|     }); | ||||
| 
 | ||||
|     it('validates the name can be used as an object key', () => { | ||||
|       const mockCF = { | ||||
|         name: 'test.boom', | ||||
|         type: 'text', | ||||
|       }; | ||||
| 
 | ||||
|       const customFields = customFieldsRegistry(strapi); | ||||
| 
 | ||||
|       expect(() => customFields.add(mockCF)).toThrowError( | ||||
|         `Custom field name: 'test.boom' is not a valid object key` | ||||
|       ); | ||||
|     }); | ||||
| 
 | ||||
|     it('validates the type is a Strapi type', () => { | ||||
|       const mockCF = { | ||||
|         name: 'test', | ||||
|         type: 'geojson', | ||||
|       }; | ||||
| 
 | ||||
|       const customFields = customFieldsRegistry(strapi); | ||||
| 
 | ||||
|       expect(() => customFields.add(mockCF)).toThrowError( | ||||
|         `Custom field type: 'geojson' is not a valid Strapi type` | ||||
|       ); | ||||
|     }); | ||||
| 
 | ||||
|     it('confirms the custom field does not already exist', () => { | ||||
|       const mockCF = { | ||||
|         name: 'test', | ||||
|         plugin: 'plugintest', | ||||
|         type: 'text', | ||||
|       }; | ||||
| 
 | ||||
|       const customFields = customFieldsRegistry(strapi); | ||||
| 
 | ||||
|       customFields.add(mockCF); | ||||
|       expect(() => customFields.add(mockCF)).toThrowError( | ||||
|         `Custom field: 'plugin::plugintest.test' has already been registered` | ||||
|       ); | ||||
|     }); | ||||
|   }); | ||||
|   describe('get', () => { | ||||
|     it('gets a registered custom field', () => { | ||||
|       const mockCF = { | ||||
|         name: 'test', | ||||
|         plugin: 'plugintest', | ||||
|         type: 'text', | ||||
|       }; | ||||
| 
 | ||||
|   it('adds a custom field not registered in a plugin', () => { | ||||
|     const mockCF = { | ||||
|       name: 'test', | ||||
|       type: 'text', | ||||
|     }; | ||||
|       const customFields = customFieldsRegistry(strapi); | ||||
|       customFields.add(mockCF); | ||||
| 
 | ||||
|     const customFields = customFieldsRegistry(strapi); | ||||
|     customFields.add(mockCF); | ||||
|       expect(customFields.get('plugin::plugintest.test')).toEqual(mockCF); | ||||
|     }); | ||||
| 
 | ||||
|     const expected = { | ||||
|       'global::test': mockCF, | ||||
|     }; | ||||
|     expect(customFields.getAll()).toEqual(expected); | ||||
|   }); | ||||
|     it('throws when a custom field is not registered', () => { | ||||
|       const customFields = customFieldsRegistry(strapi); | ||||
| 
 | ||||
|   it('requires a name key on the custom field', () => { | ||||
|     const mockCF = { | ||||
|       type: 'test', | ||||
|     }; | ||||
| 
 | ||||
|     const customFields = customFieldsRegistry(strapi); | ||||
| 
 | ||||
|     expect(() => customFields.add(mockCF)).toThrowError( | ||||
|       `Custom fields require a 'name' and 'type' key` | ||||
|     ); | ||||
|   }); | ||||
| 
 | ||||
|   it('requires a type key on the custom field', () => { | ||||
|     const mockCF = { | ||||
|       name: 'test', | ||||
|     }; | ||||
| 
 | ||||
|     const customFields = customFieldsRegistry(strapi); | ||||
| 
 | ||||
|     expect(() => customFields.add(mockCF)).toThrowError( | ||||
|       `Custom fields require a 'name' and 'type' key` | ||||
|     ); | ||||
|   }); | ||||
| 
 | ||||
|   it('validates the name can be used as an object key', () => { | ||||
|     const mockCF = { | ||||
|       name: 'test.boom', | ||||
|       type: 'text', | ||||
|     }; | ||||
| 
 | ||||
|     const customFields = customFieldsRegistry(strapi); | ||||
| 
 | ||||
|     expect(() => customFields.add(mockCF)).toThrowError( | ||||
|       `Custom field name: 'test.boom' is not a valid object key` | ||||
|     ); | ||||
|   }); | ||||
| 
 | ||||
|   it('validates the type is a Strapi type', () => { | ||||
|     const mockCF = { | ||||
|       name: 'test', | ||||
|       type: 'geojson', | ||||
|     }; | ||||
| 
 | ||||
|     const customFields = customFieldsRegistry(strapi); | ||||
| 
 | ||||
|     expect(() => customFields.add(mockCF)).toThrowError( | ||||
|       `Custom field type: 'geojson' is not a valid Strapi type` | ||||
|     ); | ||||
|   }); | ||||
| 
 | ||||
|   it('confirms the custom field does not already exist', () => { | ||||
|     const mockCF = { | ||||
|       name: 'test', | ||||
|       plugin: 'plugintest', | ||||
|       type: 'text', | ||||
|     }; | ||||
| 
 | ||||
|     const customFields = customFieldsRegistry(strapi); | ||||
| 
 | ||||
|     customFields.add(mockCF); | ||||
|     expect(() => customFields.add(mockCF)).toThrowError( | ||||
|       `Custom field: 'plugin::plugintest.test' has already been registered` | ||||
|     ); | ||||
|       expect(() => customFields.get('plugin::plugintest.test')).toThrowError( | ||||
|         `Could not find Custom Field: plugin::plugintest.test` | ||||
|       ); | ||||
|     }); | ||||
|   }); | ||||
| }); | ||||
|  | ||||
| @ -11,7 +11,12 @@ const customFieldsRegistry = strapi => { | ||||
|       return customFields; | ||||
|     }, | ||||
|     get(customField) { | ||||
|       return customFields[customField]; | ||||
|       const registeredCustomField = customFields[customField]; | ||||
|       if (!registeredCustomField) { | ||||
|         throw new Error(`Could not find Custom Field: ${customField}`); | ||||
|       } | ||||
| 
 | ||||
|       return registeredCustomField; | ||||
|     }, | ||||
|     add(customField) { | ||||
|       const customFieldList = Array.isArray(customField) ? customField : [customField]; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Mark Kaylor
						Mark Kaylor