Rename wrapEntityData to createResource
authorFrank Bessou <frank.bessou@logilab.fr>
Tue, 02 May 2017 10:06:38 +0200
changeset 165 d2c23dcb2cbb
parent 164 9edd5ed68ae1
child 166 81fb40f81007
Rename wrapEntityData to createResource
src/components/Entity.js
src/jsonaryutils.js
src/services/hypermedia.js
test/index.js
--- a/src/components/Entity.js	Fri Apr 28 17:26:56 2017 +0200
+++ b/src/components/Entity.js	Tue May 02 10:06:38 2017 +0200
@@ -7,7 +7,7 @@
 
 import Api from '../Api';
 import HypermediaClient from '../services/hypermedia';
-import {wrapEntityData, PropTypeJsonaryWrapper} from '../jsonaryutils'
+import {createResource, PropTypeJsonaryWrapper} from '../jsonaryutils'
 import {PropTypesEntityModel} from '../model';
 import {buildFormData, appendPath} from '../utils';
 import {ActionsDropDown, CollectionItemLink} from './BaseViews';
@@ -193,8 +193,8 @@
     }
 
     updateEntity(entity) {
-        const wrappedData = wrapEntityData(entity, this.state.entity.data.schemas()[0].data.value());
-        this.setState(merge({}, this.state, {entity: {data: wrappedData}}));
+        const resource = createResource(entity, this.state.entity.data.schemas()[0].data.value());
+        this.setState(merge({}, this.state, {entity: {data: resource}}));
     }
 
     deleteEntity() {
@@ -469,8 +469,8 @@
             .then(
                 (schema) => {
                     const {data} = this.props.entity;
-                    const wrappedEntity = wrapEntityData(data.value(), schema);
-                    const formData = buildFormData(wrappedEntity);
+                    const resource = createResource(data.value(), schema);
+                    const formData = buildFormData(resource);
                     this.setState({schema: schema, formData: formData});
                 }
             );
--- a/src/jsonaryutils.js	Fri Apr 28 17:26:56 2017 +0200
+++ b/src/jsonaryutils.js	Tue May 02 10:06:38 2017 +0200
@@ -2,8 +2,8 @@
 import "script-loader!jsonary/super-bundle/jsonary-super-bundle";
 import {PropTypes} from "prop-types";
 
-export function wrapEntityData(entity, schema) {
-    const wrappedData = Jsonary.create(entity);
+export function createResource(instance, schema) {
+    const wrappedData = Jsonary.create(instance);
     const dataSchema = Jsonary.createSchema(schema);
     wrappedData.addSchema(dataSchema);
     return wrappedData;
--- a/src/services/hypermedia.js	Fri Apr 28 17:26:56 2017 +0200
+++ b/src/services/hypermedia.js	Tue May 02 10:06:38 2017 +0200
@@ -4,7 +4,7 @@
 import LinkParser from 'http-link-header';
 import {defaultsDeep} from 'lodash/object';
 
-import {wrapEntityData} from '../jsonaryutils';
+import {createResource} from '../jsonaryutils';
 import {appendPath} from '../utils';
 
 export class HttpHypermediaClient {
@@ -33,7 +33,7 @@
 
             return Promise.all([dataPromise, schemaPromise]).then(
                     ([data, schema]) => {
-                        resource.data = wrapEntityData(data, schema);
+                        resource.data = createResource(data, schema);
                         return resource;
                     });
         });
--- a/test/index.js	Fri Apr 28 17:26:56 2017 +0200
+++ b/test/index.js	Tue May 02 10:06:38 2017 +0200
@@ -15,7 +15,7 @@
 import {Api} from '../src/Api';
 import {ActionLink, ActionsDropDown, CollectionItemLink} from '../src/components/BaseViews';
 import {EntityMeta, EntityAttributes} from '../src/components/Entity';
-import {wrapEntityData} from "../src/jsonaryutils";
+import {createResource} from "../src/jsonaryutils";
 import {ReactJsonSchemaAdapterFactory} from '../src/components/Form';
 import {HttpHypermediaClient} from '../src/services/hypermedia';
 import {expect} from 'chai';
@@ -88,8 +88,8 @@
         const expected = {
             "login": "admin",
         };
-        const data = wrapEntityData(entity, userEditionSchema);
-        const formData = buildFormData(data);
+        const resource = createResource(entity, userEditionSchema);
+        const formData = buildFormData(resource);
         expect(formData).to.deep.equal(expected);
     });
 
@@ -121,8 +121,8 @@
                 },
             ],
         };
-        const data = wrapEntityData(entity, userEditionSchema);
-        const formData = buildFormData(data)
+        const resource = createResource(entity, userEditionSchema);
+        const formData = buildFormData(resource)
         expect(formData).to.deep.equal(expected);
     });
 
@@ -137,7 +137,7 @@
     it('renders a link with title and proper URL', () => {
         const item = {title: 'bob', id:'123'};
         const itemSchema = {links: [{rel: 'item', href: '/user/{id}'}]};
-        const wrappedItem = wrapEntityData(item, itemSchema);
+        const wrappedItem = createResource(item, itemSchema);
         const component = <CollectionItemLink item={wrappedItem}/>;
 
         const wrapper = shallow(component);
@@ -253,8 +253,8 @@
             },
             type: 'object',
         }
-        const data= wrapEntityData(instance, schema);
-        const wrapper = render(<EntityAttributes data={data} />);
+        const resource = createResource(instance, schema);
+        const wrapper = render(<EntityAttributes data={resource} />);
         expect(wrapper.find('th').eq(0).text()).to.be.equal('Title');
         expect(wrapper.find('th').eq(1).text()).to.be.equal('Description');
     });
@@ -308,57 +308,57 @@
     describe('with data containing "created_at" and "updated_at" properties', () => {
 
         it('renders something', () => {
-            const data =  wrapEntityData(dataWithDates, schemaWithoutDates);
+            const resource =  createResource(dataWithDates, schemaWithoutDates);
 
-            const wrapper = shallow(<EntityMeta data={data}/>);
+            const wrapper = shallow(<EntityMeta data={resource}/>);
             expect(wrapper.html()).to.not.be.equal(null);
 
         });
 
         it('renders the dates', () => {
 
-            const data=  wrapEntityData(dataWithDates, schemaWithoutDates);
+            const resource = createResource(dataWithDates, schemaWithoutDates);
 
-            const wrapper = shallow(<EntityMeta data={data}/>);
+            const wrapper = shallow(<EntityMeta data={resource}/>);
             expect(wrapper.html()).to.contain('2017');
             expect(wrapper.html()).to.contain('2015');
 
         });
 
         it('renders "created at" when the "created_at" property has no title', () => {
-            const data =  wrapEntityData(dataWithDates, schemaWithDatesWithoutTitles);
+            const resource = createResource(dataWithDates, schemaWithDatesWithoutTitles);
 
-            const wrapper = shallow(<EntityMeta data={data}/>);
+            const wrapper = shallow(<EntityMeta data={resource}/>);
             expect(wrapper.html()).to.contain('created at');
         });
 
         it('renders "updated at" when the "updated_at" property has no title', () => {
-            const data =  wrapEntityData(dataWithDates, schemaWithDatesWithoutTitles);
+            const resource = createResource(dataWithDates, schemaWithDatesWithoutTitles);
 
-            const wrapper = shallow(<EntityMeta data={data}/>);
+            const wrapper = shallow(<EntityMeta data={resource}/>);
             expect(wrapper.html()).to.contain('updated at');
         });
 
         it('renders the "created_at" property\'s title when it exists', () => {
-            const data =  wrapEntityData(dataWithDates, schemaWithDatesWithTitles);
+            const resource = createResource(dataWithDates, schemaWithDatesWithTitles);
 
-            const wrapper = shallow(<EntityMeta data={data}/>);
+            const wrapper = shallow(<EntityMeta data={resource}/>);
             expect(wrapper.html()).to.contain(createdAtTitle);
         });
 
         it('renders the "updated_at" property\'s title when it exists', () => {
-            const data =  wrapEntityData(dataWithDates, schemaWithDatesWithTitles);
+            const resource = createResource(dataWithDates, schemaWithDatesWithTitles);
 
-            const wrapper = shallow(<EntityMeta data={data}/>);
+            const wrapper = shallow(<EntityMeta data={resource}/>);
             expect(wrapper.html()).to.contain(updatedAtTitle);
         });
 
     });
 
     it('renders nothing when data contains neither "created_at" nor "updated_at" property', () => {
-        const data =  wrapEntityData(dataWithoutDates, schemaWithoutDates);
+        const resource = createResource(dataWithoutDates, schemaWithoutDates);
 
-        const wrapper = shallow(<EntityMeta data={data}/>);
+        const wrapper = shallow(<EntityMeta data={resource}/>);
         expect(wrapper.html()).to.be.equal(null);
 
     });
@@ -535,7 +535,7 @@
         const etype = 'foo';
         const eid = 123;
         const route = `/${etype}/${eid}`;
-        const resource = {url: route, route: route, allowedActions:['view'], data: wrapEntityData('{}', '{}')};
+        const resource = {url: route, route: route, allowedActions:['view'], data: createResource('{}', '{}')};
 
         beforeEach(() => {
             sinon.stub(hypermediaClient, 'getResource').resolves(resource);