Add tests for Resource and ResourView components.
authorFrank Bessou <frank.bessou@logilab.fr>
Thu, 11 May 2017 13:48:33 +0200
changeset 197 926f820c4216
parent 196 b30b72c99142
child 198 5b1b7472669e
Add tests for Resource and ResourView components.
test/index.js
--- a/test/index.js	Thu May 11 10:26:57 2017 +0200
+++ b/test/index.js	Thu May 11 13:48:33 2017 +0200
@@ -18,6 +18,15 @@
 import {mapToSchema} from "../src/jsonaryutils";
 import {ReactJsonSchemaAdapterFactory} from '../src/components/Form';
 import {HttpHypermediaClient} from '../src/services/hypermedia';
+import {
+    Resource,
+    ResourceCreationForm,
+    ResourceEditionForm,
+    ResourceView,
+    ResourceCollectionView,
+    ResourceEntityView,
+    ResourceDeletionView,
+} from '../src/components/Resource';
 import {expect} from 'chai';
 
 chai.use(chaiAsPromised);
@@ -759,3 +768,83 @@
         });
     });
 });
+
+describe('Resource', () => {
+    it('should select "ResourceCreationForm" when action is "new"', () => {
+        const params = {
+            apiUrl: '/book/',
+            action: 'new',
+        };
+        const wrapper = shallow(<Resource match={{params: params}}/>);
+        const component = wrapper.props().component;
+
+        expect(component).to.be.equal(ResourceCreationForm);
+    });
+
+    it('should select "ResourceEditionForm" when action is "edit"', () => {
+        const params = {
+            apiUrl: '/book/123',
+            action: 'edit',
+        };
+        const wrapper = shallow(<Resource match={{params: params}}/>);
+        const component = wrapper.props().component;
+
+        expect(component).to.be.equal(ResourceEditionForm);
+    });
+
+    it('should select "ResourceDeletionView" when action is "delete"', () => {
+        const params = {
+            apiUrl: '/book/123',
+            action: 'delete',
+        };
+        const wrapper = shallow(<Resource match={{params: params}}/>);
+        const component = wrapper.props().component;
+
+        expect(component).to.be.equal(ResourceDeletionView);
+    });
+
+    it('should select "ResourceView" when action is "view"', () => {
+        const params = {
+            apiUrl: '/book/123',
+            action: 'view',
+        };
+        const wrapper = shallow(<Resource match={{params: params}}/>);
+        const component = wrapper.props().component;
+
+        expect(component).to.be.equal(ResourceView);
+    });
+
+    it('should select "ResourceView" when action is not present', () => {
+        const params = {
+            apiUrl: '/book/123',
+            action: undefined,
+        };
+        const wrapper = shallow(<Resource match={{params: params}}/>);
+        const component = wrapper.props().component;
+
+        expect(component).to.be.equal(ResourceView);
+    });
+
+});
+
+describe('ResourceView', () => {
+    context('resource represents a collection', () => {
+        it('should render a ResourceCollectionView', () => {
+            const resource = {data: mapToSchema([], {type: 'array'})};
+            const wrapper = shallow(<ResourceView resource={resource}/>);
+
+            const view = <ResourceCollectionView collection={resource.data} />;
+            expect(wrapper.contains(view)).to.be.true;
+        });
+    });
+
+    context('resource represents an entity', () => {
+        it('should render a ResourceCollectionView', () => {
+            const resource = {data: mapToSchema({}, {type: 'object'})};
+            const wrapper = shallow(<ResourceView resource={resource}/>);
+
+            const view = <ResourceEntityView entity={resource.data} />;
+            expect(wrapper.contains(view)).to.be.true;
+        });
+    });
+});