Extract HttpHypermediaClient tests from the 'test' directory
authorFrank Bessou <frank.bessou@logilab.fr>
Thu, 29 Jun 2017 12:16:33 +0200
changeset 237 dd1cf7e02866
parent 236 9e5eb5c240ca
child 238 dec96013fe33
Extract HttpHypermediaClient tests from the 'test' directory We want to extract the HttpHypermediaClient from this library, lets separate the tests first.
karma.conf.js
src/services/hypermedia.spec.js
test/index.js
--- a/karma.conf.js	Fri Jun 30 11:03:11 2017 +0200
+++ b/karma.conf.js	Thu Jun 29 12:16:33 2017 +0200
@@ -17,6 +17,7 @@
         files: [
             'node_modules/babel-polyfill/dist/polyfill.js',
             path.join(paths.test, '*.js'),
+            path.join(paths.source, '**/*.spec.js'),
         ],
 
         // list of files to exclude
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/services/hypermedia.spec.js	Thu Jun 29 12:16:33 2017 +0200
@@ -0,0 +1,125 @@
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+import sinon, {assert} from 'sinon';
+import {merge} from 'lodash/object';
+
+import {appendPath} from '../utils';
+import {HttpHypermediaClient} from '../services/hypermedia';
+import {expect} from 'chai';
+
+chai.use(chaiAsPromised);
+
+describe('HttpHypermediaClient', () => {
+
+    describe('getResource', () => {
+        const baseUrl = 'http://example.com';
+        let fakeFetch;
+
+        function configureBackend(config) {
+
+            const noContent = config.noContent || false;
+            const noLink = config.noLink || false;
+            const customLink = config.customLink || null;
+            const data = noContent ? undefined : (config.data || {});
+            const resourceRoute = config.resourceRoute || '/';
+            const schema = null;
+            const schemaRoute = config.schemaRoute || appendPath(resourceRoute, 'schema');
+            const allow = config.allow || 'GET';
+            const options = config.options || {};
+
+            fakeFetch = sinon.stub();
+            const client = new HttpHypermediaClient(baseUrl, fakeFetch);
+            sinon.stub(client, 'jsonFetchResponse').callsFake(() => {
+                const resourceOptions = merge({headers: {Allow: allow}}, options);
+                if (!noLink) {
+                    const linkHeader = customLink || `<${schemaRoute}>; rel=describedby`;
+                    merge(resourceOptions, {headers: {Link: linkHeader}});
+                }
+                if (noContent) {
+                    merge(resourceOptions, {status: 204});
+                } else {
+                    merge(resourceOptions, {status: 200, headers: {"Content-Type": "application/json"}});
+                }
+                const response = new Response(JSON.stringify(data), resourceOptions);
+                return Promise.resolve(response);
+            });
+
+            sinon.stub(client, 'getSchema').callsFake(() => {
+                const schemaOptions = {headers: {"Content-Type": "application/json"}};
+                const response = new Response(JSON.stringify(schema), schemaOptions);
+                return Promise.resolve(response);
+            });
+
+            return client;
+        }
+
+        it('should fetch resource', done => {
+            const resourceData = {name: 'John Doe'};
+            const resourceRoute = '/users/john';
+
+            const client = configureBackend({data: resourceData, resourceRoute: resourceRoute});
+
+            client.getResource(resourceRoute).then( resource => {
+                expect(resource.data.value()).to.deep.equal(resourceData);
+                done();
+            }).catch(done);
+
+        });
+
+        it('should fetch schema provided in HTTP "Link" header', done => {
+            const config = {
+                resourceRoute: '/users/john',
+                schema: {type: 'object'},
+                schemaRoute: '/schema/any',
+
+            };
+            const client = configureBackend(config);
+
+            client.getResource(config.resourceRoute).then(resource => {
+                const schema = resource.data.schemas()[0].data.value();
+
+                expect(schema).to.deep.equal(schema);
+                assert.calledWith(client.getSchema, sinon.match(config.schemaRoute));
+                done();
+            }).catch(done);
+        });
+
+        context('the resource\'s HTTP "Link" header is not present', () => {
+            it('should reject with a descriptive message', () => {
+                const config = {
+                    resourceRoute: '/',
+                    noLink: true,
+                };
+                const client = configureBackend(config);
+
+                return expect(client.getResource('/')).to.eventually.be.rejectedWith(/"Link" header does not exist./);
+            });
+        });
+
+        context('the resource\'s HTTP "Link" does not contain a describedby link', () => {
+            it('should reject with a descriptive message', () => {
+                const config = {
+                    resourceRoute: '/',
+                    customLink: '</> ; rel="collection"',
+                };
+                const client = configureBackend(config);
+
+                return expect(client.getResource('/')).to.eventually.be.rejectedWith(/Cannot find 'describedby'/);
+            });
+        });
+
+        describe('jsonFetch', () => {
+            const anyUrl = 'http://xyz.com';
+            const validResponse = new window.Response('{}', {headers: {'content-type': 'application/json'}});
+
+            it('should call fetch function provided in constructor', () => {
+                const fetch = sinon.stub().resolves(validResponse);
+                const client = new HttpHypermediaClient(baseUrl, fetch);
+
+                client.jsonFetch(anyUrl);
+
+                expect(fetch.calledOnce).to.be.true;
+            });
+        });
+    });
+});
--- a/test/index.js	Fri Jun 30 11:03:11 2017 +0200
+++ b/test/index.js	Thu Jun 29 12:16:33 2017 +0200
@@ -8,14 +8,12 @@
 import React from 'react';
 import {PropTypes} from 'prop-types';
 import createRouterContext from 'react-router-test-context';
-import {merge} from 'lodash/object';
 
 import {buildFormData, appendPath} from '../src/utils';
 import {ActionLink, ActionsDropDown, CollectionItemLink} from '../src/components/BaseViews';
 import {EntityMeta, EntityAttributes} from '../src/components/Entity';
 import {mapToSchema} from "../src/jsonaryutils";
 import {ReactJsonSchemaAdapterFactory} from '../src/components/Form';
-import {HttpHypermediaClient} from '../src/services/hypermedia';
 import {
     Resource,
     ResourceCreationForm,
@@ -431,121 +429,6 @@
     });
 });
 
-describe('HttpHypermediaClient', () => {
-
-    describe('getResource', () => {
-        const baseUrl = 'http://example.com';
-        let fakeFetch;
-
-        function configureBackend(config) {
-
-            const noContent = config.noContent || false;
-            const noLink = config.noLink || false;
-            const customLink = config.customLink || null;
-            const data = noContent ? undefined : (config.data || {});
-            const resourceRoute = config.resourceRoute || '/';
-            const schema = null;
-            const schemaRoute = config.schemaRoute || appendPath(resourceRoute, 'schema');
-            const allow = config.allow || 'GET';
-            const options = config.options || {};
-
-            fakeFetch = sinon.stub();
-            const client = new HttpHypermediaClient(baseUrl, fakeFetch);
-            sinon.stub(client, 'jsonFetchResponse').callsFake(() => {
-                const resourceOptions = merge({headers: {Allow: allow}}, options);
-                if (!noLink) {
-                    const linkHeader = customLink || `<${schemaRoute}>; rel=describedby`;
-                    merge(resourceOptions, {headers: {Link: linkHeader}});
-                }
-                if (noContent) {
-                    merge(resourceOptions, {status: 204});
-                } else {
-                    merge(resourceOptions, {status: 200, headers: {"Content-Type": "application/json"}});
-                }
-                const response = new Response(JSON.stringify(data), resourceOptions);
-                return Promise.resolve(response);
-            });
-
-            sinon.stub(client, 'getSchema').callsFake(() => {
-                const schemaOptions = {headers: {"Content-Type": "application/json"}};
-                const response = new Response(JSON.stringify(schema), schemaOptions);
-                return Promise.resolve(response);
-            });
-
-            return client;
-        }
-
-        it('should fetch resource', done => {
-            const resourceData = {name: 'John Doe'};
-            const resourceRoute = '/users/john';
-
-            const client = configureBackend({data: resourceData, resourceRoute: resourceRoute});
-
-            client.getResource(resourceRoute).then( resource => {
-                expect(resource.data.value()).to.deep.equal(resourceData);
-                done();
-            }).catch(done);
-
-        });
-
-        it('should fetch schema provided in HTTP "Link" header', done => {
-            const config = {
-                resourceRoute: '/users/john',
-                schema: {type: 'object'},
-                schemaRoute: '/schema/any',
-
-            };
-            const client = configureBackend(config);
-
-            client.getResource(config.resourceRoute).then(resource => {
-                const schema = resource.data.schemas()[0].data.value();
-
-                expect(schema).to.deep.equal(schema);
-                assert.calledWith(client.getSchema, sinon.match(config.schemaRoute));
-                done();
-            }).catch(done);
-        });
-
-        context('the resource\'s HTTP "Link" header is not present', () => {
-            it('should reject with a descriptive message', () => {
-                const config = {
-                    resourceRoute: '/',
-                    noLink: true,
-                };
-                const client = configureBackend(config);
-
-                return expect(client.getResource('/')).to.eventually.be.rejectedWith(/"Link" header does not exist./);
-            });
-        });
-
-        context('the resource\'s HTTP "Link" does not contain a describedby link', () => {
-            it('should reject with a descriptive message', () => {
-                const config = {
-                    resourceRoute: '/',
-                    customLink: '</> ; rel="collection"',
-                };
-                const client = configureBackend(config);
-
-                return expect(client.getResource('/')).to.eventually.be.rejectedWith(/Cannot find 'describedby'/);
-            });
-        });
-
-        describe('jsonFetch', () => {
-            const anyUrl = 'http://xyz.com';
-            const validResponse = new window.Response('{}', {headers: {'content-type': 'application/json'}});
-
-            it('should call fetch function provided in constructor', () => {
-                const fetch = sinon.stub().resolves(validResponse);
-                const client = new HttpHypermediaClient(baseUrl, fetch);
-
-                client.jsonFetch(anyUrl);
-
-                expect(fetch.calledOnce).to.be.true;
-            });
-        });
-    });
-});
-
 describe('ReactJsonSchemaAdapterFactory', () => {
     const adapterFactory = new ReactJsonSchemaAdapterFactory();