[fix] Display correct title for attributes which are not present in an instance
authorFrank Bessou <frank.bessou@logilab.fr>
Thu, 27 Apr 2017 16:01:14 +0200
changeset 147 62686dc26af0
parent 146 93e839b8c619
child 148 8c0ca4705fab
[fix] Display correct title for attributes which are not present in an instance When the schema was describing a property which was not in an instance, the displayed title was the property name instead of the property's schema's title.
src/components/Entity.js
test/index.js
--- a/src/components/Entity.js	Thu Apr 27 15:54:13 2017 +0200
+++ b/src/components/Entity.js	Thu Apr 27 16:01:14 2017 +0200
@@ -14,16 +14,18 @@
 import {WorkflowActions} from './Workflow';
 import {AttributeValue} from './Attribute';
 
-function EntityAttributes(props) {
+export function EntityAttributes(props) {
     const {data} = props;
     const schema = data.schemas();
     const properties = schema.definedProperties();
     const attributes = properties.map(
         (name)  => {
+            const attributeSchema = schema.propertySchemas(name);
+            const attributeTitle = attributeSchema.title() || name;
             const attribute = data.property(name);
             return (
                 <tr key={name}>
-                    <th>{attribute.schemas().title() || name}</th>
+                    <th>{attributeTitle}</th>
                     <td><AttributeValue attribute={attribute} /></td>
                 </tr>
             );
--- a/test/index.js	Thu Apr 27 15:54:13 2017 +0200
+++ b/test/index.js	Thu Apr 27 16:01:14 2017 +0200
@@ -13,7 +13,7 @@
 import {buildFormData, appendPath} from '../src/utils';
 import {Api} from '../src/Api';
 import {ActionLink, ActionsDropDown, CollectionItemLink} from '../src/components/BaseViews';
-import {EntityMeta} from '../src/components/Entity';
+import {EntityMeta, EntityAttributes} from '../src/components/Entity';
 import {wrapEntityData} from "../src/jsonaryutils";
 import {ReactJsonSchemaAdapterFactory} from '../src/components/Form';
 import {HttpHypermediaClient} from '../src/services/hypermedia';
@@ -239,6 +239,26 @@
 
 });
 
+describe('<EntityAttributes />', () => {
+
+    it('Use the schema title of an attribute when it exists', () => {
+        const instance = {
+            name: 'A_NAME',
+        }
+        const schema = {
+            properties: {
+                name: {type: 'string', title: 'Title'},
+                description: {type: 'string', title: 'Description'},
+            },
+            type: 'object',
+        }
+        const data= wrapEntityData(instance, schema);
+        const wrapper = render(<EntityAttributes data={data} />);
+        expect(wrapper.find('th').eq(0).text()).to.be.equal('Title');
+        expect(wrapper.find('th').eq(1).text()).to.be.equal('Description');
+    });
+});
+
 describe('<EntityMeta />', () => {
 
     const schemaWithoutDates = {