mirror of
				https://github.com/strapi/strapi.git
				synced 2025-10-31 01:47:13 +00:00 
			
		
		
		
	Add attributes tests
This commit is contained in:
		
							parent
							
								
									5f36278ec1
								
							
						
					
					
						commit
						1fa399cfd2
					
				| @ -1,9 +1,9 @@ | |||||||
| const { | const { | ||||||
|   isSortable, |   isSortable, | ||||||
|   // isEditable,
 |   isEditable, | ||||||
|   // hasEditableAttribute,
 |   hasEditableAttribute, | ||||||
|   // hasListableAttribute,
 |   hasListableAttribute, | ||||||
|   // hasRelationAttribute,
 |   hasRelationAttribute, | ||||||
| } = require('../attributes'); | } = require('../attributes'); | ||||||
| 
 | 
 | ||||||
| describe('attributesUtils', () => { | describe('attributesUtils', () => { | ||||||
| @ -11,5 +11,207 @@ describe('attributesUtils', () => { | |||||||
|     test('The id attribute is always sortable', () => { |     test('The id attribute is always sortable', () => { | ||||||
|       expect(isSortable({}, 'id')).toBe(true); |       expect(isSortable({}, 'id')).toBe(true); | ||||||
|     }); |     }); | ||||||
|  | 
 | ||||||
|  |     test('Group fields are not sortable', () => { | ||||||
|  |       expect( | ||||||
|  |         isSortable( | ||||||
|  |           { | ||||||
|  |             allAttributes: { | ||||||
|  |               someGroup: { | ||||||
|  |                 type: 'group', | ||||||
|  |               }, | ||||||
|  |             }, | ||||||
|  |           }, | ||||||
|  |           'someGroup' | ||||||
|  |         ) | ||||||
|  |       ).toBe(false); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test('Json fields are not sortable', () => { | ||||||
|  |       expect( | ||||||
|  |         isSortable( | ||||||
|  |           { | ||||||
|  |             allAttributes: { | ||||||
|  |               jsonInput: { | ||||||
|  |                 type: 'json', | ||||||
|  |               }, | ||||||
|  |             }, | ||||||
|  |           }, | ||||||
|  |           'jsonInput' | ||||||
|  |         ) | ||||||
|  |       ).toBe(false); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test('Relations are not sortable', () => { | ||||||
|  |       expect( | ||||||
|  |         isSortable( | ||||||
|  |           { | ||||||
|  |             allAttributes: { | ||||||
|  |               oneWayRel: { | ||||||
|  |                 model: 'someModel', | ||||||
|  |               }, | ||||||
|  |             }, | ||||||
|  |           }, | ||||||
|  |           'oneWayRel' | ||||||
|  |         ) | ||||||
|  |       ).toBe(false); | ||||||
|  | 
 | ||||||
|  |       expect( | ||||||
|  |         isSortable( | ||||||
|  |           { | ||||||
|  |             allAttributes: { | ||||||
|  |               manyWayRel: { | ||||||
|  |                 collection: 'someModel', | ||||||
|  |               }, | ||||||
|  |             }, | ||||||
|  |           }, | ||||||
|  |           'manyWayRel' | ||||||
|  |         ) | ||||||
|  |       ).toBe(false); | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|  |   describe('isEditable', () => { | ||||||
|  |     test('Check if the attribute is in a model attributes', () => { | ||||||
|  |       expect( | ||||||
|  |         isEditable( | ||||||
|  |           { | ||||||
|  |             attributes: { | ||||||
|  |               field: { | ||||||
|  |                 type: 'string', | ||||||
|  |               }, | ||||||
|  |             }, | ||||||
|  |           }, | ||||||
|  |           'field' | ||||||
|  |         ) | ||||||
|  |       ).toBe(true); | ||||||
|  | 
 | ||||||
|  |       expect( | ||||||
|  |         isEditable( | ||||||
|  |           { | ||||||
|  |             attributes: { | ||||||
|  |               field: { | ||||||
|  |                 type: 'string', | ||||||
|  |               }, | ||||||
|  |             }, | ||||||
|  |           }, | ||||||
|  |           'createdAt' | ||||||
|  |         ) | ||||||
|  |       ).toBe(false); | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|  |   describe('hasEditableAttribute', () => { | ||||||
|  |     test('Check if the attribute exists and is not a relation', () => { | ||||||
|  |       const model = { | ||||||
|  |         allAttributes: { | ||||||
|  |           rel1: { | ||||||
|  |             model: 'someModel', | ||||||
|  |           }, | ||||||
|  |           rel2: { | ||||||
|  |             collection: 'someModel', | ||||||
|  |           }, | ||||||
|  |           title: { | ||||||
|  |             type: 'string', | ||||||
|  |           }, | ||||||
|  |         }, | ||||||
|  |       }; | ||||||
|  | 
 | ||||||
|  |       expect(hasEditableAttribute(model, 'rel1')).toBe(false); | ||||||
|  |       expect(hasEditableAttribute(model, 'rel2')).toBe(false); | ||||||
|  |       expect(hasEditableAttribute(model, 'unkown')).toBe(false); | ||||||
|  |       expect(hasEditableAttribute(model, 'title')).toBe(true); | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|  |   describe('hasListableAttribute', () => { | ||||||
|  |     test('Ids are listable', () => { | ||||||
|  |       expect(hasListableAttribute({}, 'id')).toBe(true); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test('Unknown attributes are not listable', () => { | ||||||
|  |       const model = { | ||||||
|  |         allAttributes: { | ||||||
|  |           rel1: { | ||||||
|  |             model: 'someModel', | ||||||
|  |           }, | ||||||
|  |           rel2: { | ||||||
|  |             collection: 'someModel', | ||||||
|  |           }, | ||||||
|  |           title: { | ||||||
|  |             type: 'string', | ||||||
|  |           }, | ||||||
|  |         }, | ||||||
|  |       }; | ||||||
|  | 
 | ||||||
|  |       expect(hasListableAttribute(model, 'unkown')).toBe(false); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test('Group attributes are not listable', () => { | ||||||
|  |       const model = { | ||||||
|  |         allAttributes: { | ||||||
|  |           someGroup: { | ||||||
|  |             type: 'group', | ||||||
|  |           }, | ||||||
|  |         }, | ||||||
|  |       }; | ||||||
|  | 
 | ||||||
|  |       expect(hasListableAttribute(model, 'someGroup')).toBe(false); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test('JSON attributes are not listable', () => { | ||||||
|  |       const model = { | ||||||
|  |         allAttributes: { | ||||||
|  |           someJson: { | ||||||
|  |             type: 'json', | ||||||
|  |           }, | ||||||
|  |         }, | ||||||
|  |       }; | ||||||
|  | 
 | ||||||
|  |       expect(hasListableAttribute(model, 'someJson')).toBe(false); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test('Relations are not listable', () => { | ||||||
|  |       const model = { | ||||||
|  |         allAttributes: { | ||||||
|  |           rel1: { | ||||||
|  |             model: 'someModel', | ||||||
|  |           }, | ||||||
|  |           rel2: { | ||||||
|  |             collection: 'someModel', | ||||||
|  |           }, | ||||||
|  |           title: { | ||||||
|  |             type: 'string', | ||||||
|  |           }, | ||||||
|  |         }, | ||||||
|  |       }; | ||||||
|  | 
 | ||||||
|  |       expect(hasListableAttribute(model, 'rel1')).toBe(false); | ||||||
|  |       expect(hasListableAttribute(model, 'rel2')).toBe(false); | ||||||
|  |       expect(hasListableAttribute(model, 'title')).toBe(true); | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|  |   describe('hasRelationAttribute', () => { | ||||||
|  |     test('Only validate relational attributes', () => { | ||||||
|  |       const model = { | ||||||
|  |         allAttributes: { | ||||||
|  |           rel1: { | ||||||
|  |             model: 'someModel', | ||||||
|  |           }, | ||||||
|  |           rel2: { | ||||||
|  |             collection: 'someModel', | ||||||
|  |           }, | ||||||
|  |           title: { | ||||||
|  |             type: 'string', | ||||||
|  |           }, | ||||||
|  |         }, | ||||||
|  |       }; | ||||||
|  | 
 | ||||||
|  |       expect(hasRelationAttribute(model, 'rel1')).toBe(true); | ||||||
|  |       expect(hasRelationAttribute(model, 'rel2')).toBe(true); | ||||||
|  |       expect(hasRelationAttribute(model, 'unkown')).toBe(false); | ||||||
|  |       expect(hasRelationAttribute(model, 'title')).toBe(false); | ||||||
|  |     }); | ||||||
|   }); |   }); | ||||||
| }); | }); | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ | |||||||
| 
 | 
 | ||||||
| const _ = require('lodash'); | const _ = require('lodash'); | ||||||
| 
 | 
 | ||||||
| const NON_SORTABLES = ['group', 'json', 'array']; | const NON_SORTABLES = ['group', 'json']; | ||||||
| const isSortable = (model, name) => { | const isSortable = (model, name) => { | ||||||
|   if (name === 'id') return true; |   if (name === 'id') return true; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -7,7 +7,7 @@ const { | |||||||
|   hasListableAttribute, |   hasListableAttribute, | ||||||
| } = require('./attributes'); | } = require('./attributes'); | ||||||
| 
 | 
 | ||||||
| async function createDefaultMetadatas(model) { | function createDefaultMetadatas(model) { | ||||||
|   return { |   return { | ||||||
|     id: { |     id: { | ||||||
|       edit: {}, |       edit: {}, | ||||||
| @ -62,7 +62,7 @@ async function syncMetadatas(configuration, model) { | |||||||
|   // add new keys and missing fields
 |   // add new keys and missing fields
 | ||||||
|   const metasWithDefaults = _.merge( |   const metasWithDefaults = _.merge( | ||||||
|     {}, |     {}, | ||||||
|     await createDefaultMetadatas(model), |     createDefaultMetadatas(model), | ||||||
|     metasWithValidKeys |     metasWithValidKeys | ||||||
|   ); |   ); | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Alexandre Bodin
						Alexandre Bodin