Rename createResource to mapToSchema
authorFrank Bessou <frank.bessou@logilab.fr>
Thu, 04 May 2017 09:55:36 +0200
changeset 178 6d904376b32e
parent 177 929a7262c5cc
child 179 fbb644088c11
Rename createResource to mapToSchema
src/components/Entity.js
src/jsonaryutils.js
src/services/hypermedia.js
test/index.js
--- a/src/components/Entity.js	Thu May 04 11:07:08 2017 +0200
+++ b/src/components/Entity.js	Thu May 04 09:55:36 2017 +0200
@@ -6,7 +6,7 @@
 
 import Api from '../Api';
 import HypermediaClient from '../services/hypermedia';
-import {createResource, PropTypeJsonaryWrapper} from '../jsonaryutils';
+import {mapToSchema, PropTypeJsonaryWrapper} from '../jsonaryutils';
 import {PropTypesEntityModel} from '../model';
 import {buildFormData, appendPath} from '../utils';
 import {ActionsDropDown, CollectionView} from './BaseViews';
@@ -188,9 +188,9 @@
             );
     }
 
-    updateEntity(entity) {
-        const resource = createResource(entity, this.state.entity.data.schemas()[0].data.value());
-        this.setState(merge({}, this.state, {entity: {data: resource}}));
+    updateEntity(instance) {
+        const entity = mapToSchema(instance, this.state.entity.data.schemas()[0].data.value());
+        this.setState(merge({}, this.state, {entity: {data: entity}}));
     }
 
     deleteEntity() {
@@ -464,8 +464,8 @@
             .then(
                 (schema) => {
                     const {data} = this.props.entity;
-                    const resource = createResource(data.value(), schema);
-                    const formData = buildFormData(resource);
+                    const entity = mapToSchema(data.value(), schema);
+                    const formData = buildFormData(entity);
                     this.setState({schema: schema, formData: formData});
                 }
             );
--- a/src/jsonaryutils.js	Thu May 04 11:07:08 2017 +0200
+++ b/src/jsonaryutils.js	Thu May 04 09:55:36 2017 +0200
@@ -2,7 +2,7 @@
 import "script-loader!jsonary/super-bundle/jsonary-super-bundle";
 import {PropTypes} from "prop-types";
 
-export function createResource(instance, schema) {
+export function mapToSchema(instance, schema) {
     const wrappedData = Jsonary.create(instance);
     const dataSchema = Jsonary.createSchema(schema);
     wrappedData.addSchema(dataSchema);
--- a/src/services/hypermedia.js	Thu May 04 11:07:08 2017 +0200
+++ b/src/services/hypermedia.js	Thu May 04 09:55:36 2017 +0200
@@ -4,7 +4,7 @@
 import LinkParser from 'http-link-header';
 import {defaultsDeep} from 'lodash/object';
 
-import {createResource} from '../jsonaryutils';
+import {mapToSchema} from '../jsonaryutils';
 import {appendPath} from '../utils';
 
 export class HttpHypermediaClient {
@@ -33,7 +33,7 @@
 
             return Promise.all([dataPromise, schemaPromise]).then(
                     ([data, schema]) => {
-                        resource.data = createResource(data, schema);
+                        resource.data = mapToSchema(data, schema);
                         return resource;
                     });
         });
--- a/test/index.js	Thu May 04 11:07:08 2017 +0200
+++ b/test/index.js	Thu May 04 09:55:36 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 {createResource} from "../src/jsonaryutils";
+import {mapToSchema} from "../src/jsonaryutils";
 import {ReactJsonSchemaAdapterFactory} from '../src/components/Form';
 import {HttpHypermediaClient} from '../src/services/hypermedia';
 import {expect} from 'chai';
@@ -77,7 +77,7 @@
 
 describe('utils.buildFormData', () => {
     it('entity alone', () => {
-        const entity = {
+        const instance = {
             "cw_etype": "CWUser",
             "dc_title": "admin",
             "eid": 6,
@@ -88,13 +88,13 @@
         const expected = {
             "login": "admin",
         };
-        const resource = createResource(entity, userEditionSchema);
-        const formData = buildFormData(resource);
+        const entity = mapToSchema(instance, userEditionSchema);
+        const formData = buildFormData(entity);
         expect(formData).to.deep.equal(expected);
     });
 
     it('entity with inlined related', () => {
-        const entity = {
+        const instance = {
             "cw_etype": "CWUser",
             "dc_title": "Sponge bob",
             "eid": 99,
@@ -121,8 +121,8 @@
                 },
             ],
         };
-        const resource = createResource(entity, userEditionSchema);
-        const formData = buildFormData(resource);
+        const entity = mapToSchema(instance, userEditionSchema);
+        const formData = buildFormData(entity);
         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 = createResource(item, itemSchema);
+        const wrappedItem = mapToSchema(item, itemSchema);
         const component = <CollectionItemLink item={wrappedItem}/>;
 
         const wrapper = shallow(component);
@@ -253,8 +253,8 @@
             },
             type: 'object',
         };
-        const resource = createResource(instance, schema);
-        const wrapper = render(<EntityAttributes data={resource} />);
+        const entity = mapToSchema(instance, schema);
+        const wrapper = render(<EntityAttributes data={entity} />);
         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 resource = createResource(dataWithDates, schemaWithoutDates);
+            const entity = mapToSchema(dataWithDates, schemaWithoutDates);
 
-            const wrapper = shallow(<EntityMeta data={resource}/>);
+            const wrapper = shallow(<EntityMeta data={entity}/>);
             expect(wrapper.html()).to.not.be.equal(null);
 
         });
 
         it('renders the dates', () => {
 
-            const resource = createResource(dataWithDates, schemaWithoutDates);
+            const entity = mapToSchema(dataWithDates, schemaWithoutDates);
 
-            const wrapper = shallow(<EntityMeta data={resource}/>);
+            const wrapper = shallow(<EntityMeta data={entity}/>);
             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 resource = createResource(dataWithDates, schemaWithDatesWithoutTitles);
+            const entity = mapToSchema(dataWithDates, schemaWithDatesWithoutTitles);
 
-            const wrapper = shallow(<EntityMeta data={resource}/>);
+            const wrapper = shallow(<EntityMeta data={entity}/>);
             expect(wrapper.html()).to.contain('created at');
         });
 
         it('renders "updated at" when the "updated_at" property has no title', () => {
-            const resource = createResource(dataWithDates, schemaWithDatesWithoutTitles);
+            const entity = mapToSchema(dataWithDates, schemaWithDatesWithoutTitles);
 
-            const wrapper = shallow(<EntityMeta data={resource}/>);
+            const wrapper = shallow(<EntityMeta data={entity}/>);
             expect(wrapper.html()).to.contain('updated at');
         });
 
         it('renders the "created_at" property\'s title when it exists', () => {
-            const resource = createResource(dataWithDates, schemaWithDatesWithTitles);
+            const entity = mapToSchema(dataWithDates, schemaWithDatesWithTitles);
 
-            const wrapper = shallow(<EntityMeta data={resource}/>);
+            const wrapper = shallow(<EntityMeta data={entity}/>);
             expect(wrapper.html()).to.contain(createdAtTitle);
         });
 
         it('renders the "updated_at" property\'s title when it exists', () => {
-            const resource = createResource(dataWithDates, schemaWithDatesWithTitles);
+            const entity = mapToSchema(dataWithDates, schemaWithDatesWithTitles);
 
-            const wrapper = shallow(<EntityMeta data={resource}/>);
+            const wrapper = shallow(<EntityMeta data={entity}/>);
             expect(wrapper.html()).to.contain(updatedAtTitle);
         });
 
     });
 
     it('renders nothing when data contains neither "created_at" nor "updated_at" property', () => {
-        const resource = createResource(dataWithoutDates, schemaWithoutDates);
+        const entity = mapToSchema(dataWithoutDates, schemaWithoutDates);
 
-        const wrapper = shallow(<EntityMeta data={resource}/>);
+        const wrapper = shallow(<EntityMeta data={entity}/>);
         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: createResource('{}', '{}')};
+        const resource = {url: route, route: route, allowedActions: ['view'], data: mapToSchema('{}', '{}')};
 
         beforeEach(() => {
             sinon.stub(hypermediaClient, 'getResource').resolves(resource);