Add Api.buildUrl tests
authorFrank Bessou <frank.bessou@logilab.fr>
Fri, 31 Mar 2017 13:40:42 +0200
changeset 60 c7d925006198
parent 59 51b18c27cc76
child 61 dae8abf45c58
Add Api.buildUrl tests
test/index.js
--- a/test/index.js	Fri Mar 31 13:20:17 2017 +0200
+++ b/test/index.js	Fri Mar 31 13:40:42 2017 +0200
@@ -7,7 +7,8 @@
 import React from 'react';
 
 import {buildFormData} from '../src/utils';
-import Api from '../src/Api';
+import {Api} from '../src/Api';
+import defaultApi from '../src/Api';
 import {ActionLink, OneLineView} from '../src/components/BaseViews';
 import {EntityMeta} from '../src/components/Entity';
 import {wrapEntityData} from "../src/jsonaryutils";
@@ -123,11 +124,11 @@
 
 describe('<OneLineView />', () => {
     beforeEach(() => {
-        sinon.stub(Api, 'buildUrl').callsFake( x => `http://testing.com/${x}`);
+        sinon.stub(defaultApi, 'buildUrl').callsFake( x => `http://testing.com/${x}`);
     });
 
     afterEach(() => {
-        Api.buildUrl.restore();
+        defaultApi.buildUrl.restore();
     });
 
     it('renders a link with dc_title and proper URL', () => {
@@ -190,11 +191,11 @@
 
 describe('<ActionLink />', () => {
     beforeEach(() => {
-        sinon.stub(Api, 'buildUrl').callsFake(x => `http://testing.com${x}`);
+        sinon.stub(defaultApi, 'buildUrl').callsFake(x => `http://testing.com${x}`);
     });
 
     afterEach(() => {
-        Api.buildUrl.restore();
+        defaultApi.buildUrl.restore();
     });
 
     it('given a "create" hyperlink, it renders a link pointing to "new" view', () => {
@@ -356,5 +357,29 @@
 
     });
 
+});
 
+describe('Api', () => {
+
+    describe('buildUrl', () => {
+        it('should add slash between BASE_URL and path when it is not already there', () => {
+            const baseUrl = 'http://example.com';
+            const path = 'foo';
+            const api = new Api(baseUrl);
+
+            const url = api.buildUrl(path);
+
+            expect(url).to.be.equal('http://example.com/foo');
+        });
+
+        it('should keep only one slash when BASE_URL ends with "/" and path starts with "/"', () => {
+            const baseUrl = "http://example.com/";
+            const path = "/foo";
+            const api = new Api(baseUrl);
+
+            const url = api.buildUrl(path);
+
+            expect(url).to.be.equal('http://example.com/foo');
+        });
+    });
 });