Add tests for Api.getEntity
authorFrank Bessou <frank.bessou@logilab.fr>
Tue, 04 Apr 2017 11:50:24 +0200
changeset 69 29faede0bd68
parent 68 1c9f3d4ad751
child 70 2d72f308d3c3
Add tests for Api.getEntity
test/index.js
--- a/test/index.js	Fri Mar 31 18:20:48 2017 +0200
+++ b/test/index.js	Tue Apr 04 11:50:24 2017 +0200
@@ -1,9 +1,10 @@
 /* eslint-env mocha */
-/* global describe, it */
+/* global describe, it, Jsonary*/
 
 import {expect} from 'chai';
 import {shallow} from 'enzyme';
 import sinon from 'sinon';
+import {assert} from 'sinon';
 import React from 'react';
 
 import {buildFormData} from '../src/utils';
@@ -382,4 +383,63 @@
             expect(fetch.calledOnce).to.be.true;
         });
     });
+
+    describe('getEntity', () => {
+        let api;
+        let fakeFetch;
+        let etype = 'foo';
+        let eid = 123;
+        const schemaJson = JSON.stringify({buzz: "fuzz"});
+        const dataJson =JSON.stringify({foo: "bar"});
+
+        beforeEach(() => {
+            fakeFetch = sinon.stub();
+            const options = {headers: {"Content-Type": "application/json"}};
+            fakeFetch.withArgs(sinon.match(/\/foo\/123$/), sinon.match({}))
+                .resolves(new Response(dataJson, options));
+
+            fakeFetch.withArgs(sinon.match(/\/foo\/123\/schema$/), sinon.match({}))
+                    .resolves(new Response(schemaJson, options));
+            api = new Api('http://example.com', fakeFetch);
+        });
+
+        it('should fetch the entity data', () => {
+            api.getEntity(etype, eid);
+
+            const dataUrl = 'http://example.com/foo/123';
+            const initOptions = {headers: {Accept: 'application/json'}};
+            assert.calledWith(fakeFetch, dataUrl, sinon.match(initOptions));
+        });
+
+        it('should fetch the entity schema', () => {
+            api.getEntity(etype, eid);
+
+            const schemaUrl = 'http://example.com/foo/123/schema';
+            const initOptions = {headers: {Accept: 'application/schema+json'}};
+            assert.calledWith(fakeFetch, schemaUrl, sinon.match(initOptions));
+        });
+
+        it('should return a Promise of EntityModel', done => {
+            api.getEntity(etype, eid).then(entity => {
+                expect(entity.url).to.exist;
+                expect(entity.route).to.exist;
+                expect(entity.etype).to.exist;
+                expect(entity.eid).to.exist;
+                expect(Jsonary.isData(entity.data)).to.be.true;
+                expect(entity.allowedActions).to.be.an('array');
+                done();
+            }).catch(done);
+        });
+
+        it('should wrap entity schema and data fetched from server into data property', done => {
+            api.getEntity(etype, eid).then( entity => {
+                const entityData = JSON.stringify(entity.data.value());
+                const entitySchema = JSON.stringify(entity.data.schemas()[0].data.value());
+                expect(entityData).to.equal(dataJson);
+                expect(entitySchema).to.equal(schemaJson);
+                done();
+            }).catch(done);
+        });
+    });
 });
+