Extract tests related to utils.js
authorFrank Bessou <frank.bessou@logilab.fr>
Fri, 30 Jun 2017 17:06:25 +0200
changeset 241 e8544e97e811
parent 240 6988c9d9bf09
child 242 db082354de16
Extract tests related to utils.js
src/utils.spec.js
test/index.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/utils.spec.js	Fri Jun 30 17:06:25 2017 +0200
@@ -0,0 +1,141 @@
+import {expect} from 'chai';
+
+import {appendPath, buildFormData} from './utils';
+import {mapToSchema} from "./jsonaryutils";
+
+const userEditionSchema = {
+    "$ref": "#/definitions/CWUser",
+    "definitions": {
+        "CWUser": {
+            "additionalProperties": false,
+            "properties": {
+                "firstname": {
+                    "maxLength": 64,
+                    "type": "string",
+                },
+                "login": {
+                    "maxLength": 64,
+                    "type": "string",
+                },
+                "surname": {
+                    "maxLength": 64,
+                    "type": "string",
+                },
+                "upassword": {
+                    "type": "string",
+                },
+                "use_email": {
+                    "items": {
+                        "$ref": "#/definitions/EmailAddress",
+                    },
+                    "type": "array",
+                },
+            },
+            "required": [
+                "login",
+            ],
+            "type": "object",
+        },
+        "EmailAddress": {
+            "additionalProperties": false,
+            "properties": {
+                "address": {
+                    "maxLength": 128,
+                    "type": "string",
+                },
+                "alias": {
+                    "maxLength": 56,
+                    "type": "string",
+                },
+            },
+            "required": [
+                "address",
+            ],
+            "type": "object",
+        },
+    },
+};
+
+describe('appendPath', () => {
+    it('should add slash between prefix and suffix when it is not already there', () => {
+        const prefix = 'http://example.com';
+        const suffix = 'foo';
+
+        const result = appendPath(prefix, suffix);
+
+        expect(result).to.be.equal('http://example.com/foo');
+    });
+
+    it('should keep only one slash when prefix ends with "/" and suffix starts with "/"', () => {
+        const prefix = "http://example.com/";
+        const suffix = "/foo";
+
+        const result = appendPath(prefix, suffix);
+
+        expect(result).to.be.equal('http://example.com/foo');
+    });
+
+    it('should not alter empty strings', () => {
+        const prefix1 = '';
+        const suffix1 = '';
+        const prefix2 = 'a';
+        const suffix2 = 'b';
+
+        expect(appendPath(prefix1, suffix1)).to.be.equal('');
+        expect(appendPath(prefix1, suffix2)).to.be.equal('/b');
+        expect(appendPath(prefix2, suffix1)).to.be.equal('a');
+        expect(appendPath(prefix2, suffix2)).to.be.equal('a/b');
+    });
+});
+
+describe('buildFormData', () => {
+    it('entity alone', () => {
+        const instance = {
+            "cw_etype": "CWUser",
+            "dc_title": "admin",
+            "eid": 6,
+            "login": "admin",
+            "firstname": null,
+            "modification_date": "2016-12-20T16:36:12+00:00",
+        };
+        const expected = {
+            "login": "admin",
+        };
+        const entity = mapToSchema(instance, userEditionSchema);
+        const formData = buildFormData(entity);
+        expect(formData).to.deep.equal(expected);
+    });
+
+    it('entity with inlined related', () => {
+        const instance = {
+            "cw_etype": "CWUser",
+            "dc_title": "Sponge bob",
+            "eid": 99,
+            "login": "bob",
+            "firstname": null,
+            "modification_date": "2016-12-20T16:36:12+00:00",
+            "use_email": [
+                {
+                    "address": "bob@sponge.com",
+                    "alias": null,
+                    "creation_date": "2016-12-20T16:36:12+00:00",
+                    "cw_etype": "EmailAddress",
+                    "cwuri": "http://example.com/3076",
+                    "eid": 3076,
+                    "modification_date": "2016-12-20T16:36:12+00:00",
+                },
+            ],
+        };
+        const expected = {
+            "login": "bob",
+            "use_email": [
+                {
+                    "address": "bob@sponge.com",
+                },
+            ],
+        };
+        const entity = mapToSchema(instance, userEditionSchema);
+        const formData = buildFormData(entity);
+        expect(formData).to.deep.equal(expected);
+    });
+});
--- a/test/index.js	Fri Jun 30 17:01:36 2017 +0200
+++ b/test/index.js	Fri Jun 30 17:06:25 2017 +0200
@@ -7,7 +7,6 @@
 import {PropTypes} from 'prop-types';
 import createRouterContext from 'react-router-test-context';
 
-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";
@@ -30,112 +29,6 @@
 const testsContext = require.context("../src", true, /.spec.js$/);
 testsContext.keys().forEach(testsContext);
 
-const userEditionSchema = {
-    "$ref": "#/definitions/CWUser",
-    "definitions": {
-        "CWUser": {
-            "additionalProperties": false,
-            "properties": {
-                "firstname": {
-                    "maxLength": 64,
-                    "type": "string",
-                },
-                "login": {
-                    "maxLength": 64,
-                    "type": "string",
-                },
-                "surname": {
-                    "maxLength": 64,
-                    "type": "string",
-                },
-                "upassword": {
-                    "type": "string",
-                },
-                "use_email": {
-                    "items": {
-                        "$ref": "#/definitions/EmailAddress",
-                    },
-                    "type": "array",
-                },
-            },
-            "required": [
-                "login",
-            ],
-            "type": "object",
-        },
-        "EmailAddress": {
-            "additionalProperties": false,
-            "properties": {
-                "address": {
-                    "maxLength": 128,
-                    "type": "string",
-                },
-                "alias": {
-                    "maxLength": 56,
-                    "type": "string",
-                },
-            },
-            "required": [
-                "address",
-            ],
-            "type": "object",
-        },
-    },
-};
-
-describe('utils.buildFormData', () => {
-    it('entity alone', () => {
-        const instance = {
-            "cw_etype": "CWUser",
-            "dc_title": "admin",
-            "eid": 6,
-            "login": "admin",
-            "firstname": null,
-            "modification_date": "2016-12-20T16:36:12+00:00",
-        };
-        const expected = {
-            "login": "admin",
-        };
-        const entity = mapToSchema(instance, userEditionSchema);
-        const formData = buildFormData(entity);
-        expect(formData).to.deep.equal(expected);
-    });
-
-    it('entity with inlined related', () => {
-        const instance = {
-            "cw_etype": "CWUser",
-            "dc_title": "Sponge bob",
-            "eid": 99,
-            "login": "bob",
-            "firstname": null,
-            "modification_date": "2016-12-20T16:36:12+00:00",
-            "use_email": [
-                {
-                    "address": "bob@sponge.com",
-                    "alias": null,
-                    "creation_date": "2016-12-20T16:36:12+00:00",
-                    "cw_etype": "EmailAddress",
-                    "cwuri": "http://example.com/3076",
-                    "eid": 3076,
-                    "modification_date": "2016-12-20T16:36:12+00:00",
-                },
-            ],
-        };
-        const expected = {
-            "login": "bob",
-            "use_email": [
-                {
-                    "address": "bob@sponge.com",
-                },
-            ],
-        };
-        const entity = mapToSchema(instance, userEditionSchema);
-        const formData = buildFormData(entity);
-        expect(formData).to.deep.equal(expected);
-    });
-
-});
-
 describe('<CollectionItemLink />', () => {
     const renderOptions = {
         context: createRouterContext(),
@@ -395,40 +288,6 @@
     });
 });
 
-describe('Utils', () => {
-    describe('appendPath', () => {
-        it('should add slash between prefix and suffix when it is not already there', () => {
-            const prefix = 'http://example.com';
-            const suffix = 'foo';
-
-            const result = appendPath(prefix, suffix);
-
-            expect(result).to.be.equal('http://example.com/foo');
-        });
-
-        it('should keep only one slash when prefix ends with "/" and suffix starts with "/"', () => {
-            const prefix = "http://example.com/";
-            const suffix = "/foo";
-
-            const result = appendPath(prefix, suffix);
-
-            expect(result).to.be.equal('http://example.com/foo');
-        });
-
-        it('should not alter empty strings', () => {
-            const prefix1 = '';
-            const suffix1 = '';
-            const prefix2 = 'a';
-            const suffix2 = 'b';
-
-            expect(appendPath(prefix1, suffix1)).to.be.equal('');
-            expect(appendPath(prefix1, suffix2)).to.be.equal('/b');
-            expect(appendPath(prefix2, suffix1)).to.be.equal('a');
-            expect(appendPath(prefix2, suffix2)).to.be.equal('a/b');
-        });
-    });
-});
-
 describe('Resource', () => {
     it('should select "ResourceCreationForm" when action is "add"', () => {
         const params = {