Add getRoot method to API
authorFrank Bessou <frank.bessou@logilab.fr>
Fri, 07 Apr 2017 15:22:27 +0200
changeset 98 ef210fee86cc
parent 97 7744b1901e7b
child 99 6c5e7ccfd728
Add getRoot method to API
src/Api.js
test/index.js
--- a/src/Api.js	Fri Apr 07 14:29:44 2017 +0200
+++ b/src/Api.js	Fri Apr 07 15:22:27 2017 +0200
@@ -99,6 +99,10 @@
     }
 
 
+    getRoot() {
+        return this.getResource('');
+    }
+
     getRelatedSchema(etype, rtype, role = 'creation', targetType = null) {
         let url = `/${etype}/relationships/${rtype}/schema?role=${role}`;
         if (targetType !== null) {
--- a/test/index.js	Fri Apr 07 14:29:44 2017 +0200
+++ b/test/index.js	Fri Apr 07 15:22:27 2017 +0200
@@ -377,9 +377,9 @@
 });
 
 describe('Api', () => {
+    const baseUrl = 'http://example.com';
 
     describe('jsonFetch', () => {
-        const baseUrl = 'http://example.com';
         const anyUrl = 'http://xyz.com';
         const validResponse = new window.Response('{}', {headers: {'content-type': 'application/json'}});
 
@@ -393,6 +393,27 @@
         });
     });
 
+    describe('getRoot', () => {
+        it('should fetch the root schema ', done => {
+            const rootSchema = JSON.stringify({foo: 'bar'});
+            const fakeFetch = sinon.stub();
+            const options = {headers: {"Content-Type": "application/json"}};
+            fakeFetch.withArgs(baseUrl+'/schema', sinon.match({}))
+                    .resolves(new Response(rootSchema, options));
+            const api = new Api(baseUrl, fakeFetch);
+
+            api.getRoot().then(resource => {
+                const resourceSchema = resource.data.schemas()[0].data.value();
+
+                assert.calledWith(fakeFetch, baseUrl+'/schema', sinon.match({}));
+                expect(JSON.stringify(resourceSchema)).to.deep.equal(rootSchema);
+                done();
+            }).catch(done);
+
+        });
+
+    });
+
     describe('getResource', () => {
 
         const baseUrl = 'http://example.com';