Extract tests related to Form.js
authorFrank Bessou <frank.bessou@logilab.fr>
Fri, 30 Jun 2017 17:01:36 +0200
changeset 240 6988c9d9bf09
parent 239 d651819fbe6a
child 241 e8544e97e811
Extract tests related to Form.js
src/components/Form.spec.js
test/index.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/components/Form.spec.js	Fri Jun 30 17:01:36 2017 +0200
@@ -0,0 +1,210 @@
+import {expect} from 'chai';
+import sinon, {assert} from 'sinon';
+
+import {ReactJsonSchemaAdapterFactory} from './Form';
+
+describe('ReactJsonSchemaAdapterFactory', () => {
+    const adapterFactory = new ReactJsonSchemaAdapterFactory();
+
+    describe('fromSchema', () => {
+        context('given a schema with oneOf including schemas which can be inferred to constants', () => {
+            it('should return an adapted schema', () => {
+                const schema = {
+                    oneOf: [{
+                        const: 'FIRST_VALUE',
+                    }, {
+                        enum: ['SECOND_VALUE'],
+                        title: 'SCHEMA_TITLE',
+                    }, {
+                        const: {valid: false},
+                    }],
+                };
+                const expectedAdapter = {
+                    enum: ['FIRST_VALUE', 'SECOND_VALUE', {valid: false}],
+                    enumNames: ['FIRST_VALUE', 'SCHEMA_TITLE', 'Option #2'],
+                };
+
+                const adapter = adapterFactory.fromSchema(schema);
+
+                expect(adapter).to.deep.equal(expectedAdapter);
+            });
+        });
+
+        context('given a schema with oneOf including one schema which cannot be inferred to constant', () => {
+            it('should return the same schema', () => {
+                const schema = {
+                    oneOf: [{
+                        const: 'FIRST_VALUE',
+                    }, {
+                        enum: ['SECOND_VALUE', 'THIRD_VALUE'],
+                    }],
+                };
+
+                const adapter = adapterFactory.fromSchema(schema);
+
+                expect(adapter).to.deep.equal(schema);
+            });
+        });
+    });
+
+    describe('adaptRecursively', () => {
+
+        beforeEach( () => {
+            sinon.stub(adapterFactory, 'getEnumFromOneOf');
+            sinon.stub(adapterFactory, 'adaptRecursively');
+            adapterFactory.adaptRecursively.callThrough();
+        });
+
+        afterEach( () => {
+            adapterFactory.getEnumFromOneOf.restore();
+            adapterFactory.adaptRecursively.restore();
+        });
+
+        it ('should call recursively on nested arrays and object', () => {
+            const schema = {a: '', b: 1, c: null, d: {e: [{f: 1}, {g: 2}, null]}};
+
+            adapterFactory.adaptRecursively(schema);
+
+            const expectedArgs = [
+                schema,
+                schema.d,
+                schema.d.e,
+                schema.d.e[0],
+                schema.d.e[1],
+            ];
+
+            for (const args of expectedArgs) {
+                assert.calledWith(adapterFactory.adaptRecursively, args);
+            }
+
+            assert.callCount(adapterFactory.adaptRecursively, 5);
+        });
+
+        context('getEnumFromOneOf returns values', () => {
+
+            it ('should replace oneOf by enumNames/enum', () => {
+                adapterFactory.getEnumFromOneOf.returns({enumNames: ['A'], enumValues: ['a']});
+                const schema = {oneOf: [{}, {}], title: 'TITLE'};
+
+                const adapter = adapterFactory.adaptRecursively(schema);
+
+                expect(adapter).to.deep.equal({enum: ['a'], enumNames: ['A'], title: 'TITLE'});
+            });
+        });
+
+        context('getEnumFromOneOf thrown', () => {
+
+            it ('should not replace oneOf when getEnumFromOneOf throws', () => {
+                adapterFactory.getEnumFromOneOf.throws();
+                const schema = {oneOf: []};
+
+                const adapter = adapterFactory.adaptRecursively(schema);
+
+                expect(adapter.oneOf).to.exist;
+                expect(adapter.enum).to.not.exist;
+                expect(adapter.enumNames).to.not.exist;
+            });
+        });
+    });
+
+    describe('getEnumFromOneOf', () => {
+
+        beforeEach( () => {
+            sinon.stub(adapterFactory, 'schemaToConstant');
+        });
+
+        afterEach( () => {
+            adapterFactory.schemaToConstant.restore();
+        });
+
+        it ('should return as many enum values as the number of alternative schemas', () => {
+            const schema = {oneOf: [{}, {}, {}]};
+            adapterFactory.schemaToConstant.returns({name: 'A', value: 'a'});
+            const expectedLength = 3;
+
+            const {enumNames, enumValues} = adapterFactory.getEnumFromOneOf(schema);
+
+            expect(enumNames).lengthOf(expectedLength);
+            expect(enumValues).lengthOf(expectedLength);
+        });
+    });
+
+    describe('schemaToConstant', () => {
+
+        beforeEach( () => {
+            sinon.spy(adapterFactory, 'schemaToConstant');
+        });
+
+        afterEach( () => {
+            adapterFactory.schemaToConstant.restore();
+        });
+
+        const CONST_VALUE = "THE_CONST_VALUE";
+        const ENUM_VALUE = "THE_ENUM_VALUE";
+
+        it('should get value from const when it exists', () => {
+            const schema = {const: CONST_VALUE};
+
+            const {value} = adapterFactory.schemaToConstant(schema, 0);
+
+            expect(value).to.equal(CONST_VALUE);
+        });
+
+        it('should get value from const when const and enum are present', () => {
+            const schema = {enum: [ENUM_VALUE], const: CONST_VALUE};
+
+            const {value} = adapterFactory.schemaToConstant(schema, 0);
+
+            expect(value).to.equal(CONST_VALUE);
+        });
+
+        it('should get value from enum when it exists and there is no const', () => {
+            const schema = {enum: [ENUM_VALUE]};
+
+            const {value} = adapterFactory.schemaToConstant(schema, 0);
+
+            expect(value).to.equal(ENUM_VALUE);
+        });
+
+        it('should throw when enum is not an array', () => {
+            const schema = {enum: ENUM_VALUE};
+
+            expect(() => adapterFactory.schemaToConstant(schema, 0)).to.throw();
+
+        });
+
+        it('should get name from title property when it exists', () => {
+            const schema = {enum: ['ANYTHING'], title: 'THE_TITLE'};
+
+            const {name} = adapterFactory.schemaToConstant(schema, 0);
+
+            expect(name).to.equal('THE_TITLE');
+        });
+
+        context('the schema does not contain a title', () => {
+
+            context('the inferred value is a string', () => {
+
+                it('should get name from inferred value', () => {
+                    const schema = {enum: ['THE_VALUE']};
+
+                    const {name} = adapterFactory.schemaToConstant(schema, 0);
+
+                    expect(name).to.equal('THE_VALUE');
+                });
+            });
+
+            context('the inferred value is not a string', () => {
+
+                it('build the name from the id', () => {
+                    const id = 7;
+                    const schema = {enum: [{color: 'red'}]};
+
+                    const {name} = adapterFactory.schemaToConstant(schema, id);
+
+                    expect(name).to.match(RegExp(`^[^0-9]*${id}[^0-9]*$`));
+                });
+            });
+        });
+    });
+});
--- a/test/index.js	Fri Jun 30 14:35:03 2017 +0200
+++ b/test/index.js	Fri Jun 30 17:01:36 2017 +0200
@@ -3,8 +3,6 @@
 import chai from 'chai';
 import chaiAsPromised from 'chai-as-promised';
 import {shallow, render} from 'enzyme';
-import sinon from 'sinon';
-import {assert} from 'sinon';
 import React from 'react';
 import {PropTypes} from 'prop-types';
 import createRouterContext from 'react-router-test-context';
@@ -13,7 +11,6 @@
 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 {
     Resource,
     ResourceCreationForm,
@@ -432,212 +429,6 @@
     });
 });
 
-describe('ReactJsonSchemaAdapterFactory', () => {
-    const adapterFactory = new ReactJsonSchemaAdapterFactory();
-
-    describe('fromSchema', () => {
-        context('given a schema with oneOf including schemas which can be inferred to constants', () => {
-            it('should return an adapted schema', () => {
-                const schema = {
-                    oneOf: [{
-                        const: 'FIRST_VALUE',
-                    }, {
-                        enum: ['SECOND_VALUE'],
-                        title: 'SCHEMA_TITLE',
-                    }, {
-                        const: {valid: false},
-                    }],
-                };
-                const expectedAdapter = {
-                    enum: ['FIRST_VALUE', 'SECOND_VALUE', {valid: false}],
-                    enumNames: ['FIRST_VALUE', 'SCHEMA_TITLE', 'Option #2'],
-                };
-
-                const adapter = adapterFactory.fromSchema(schema);
-
-                expect(adapter).to.deep.equal(expectedAdapter);
-            });
-        });
-
-        context('given a schema with oneOf including one schema which cannot be inferred to constant', () => {
-            it('should return the same schema', () => {
-                const schema = {
-                    oneOf: [{
-                        const: 'FIRST_VALUE',
-                    }, {
-                        enum: ['SECOND_VALUE', 'THIRD_VALUE'],
-                    }],
-                };
-
-                const adapter = adapterFactory.fromSchema(schema);
-
-                expect(adapter).to.deep.equal(schema);
-            });
-        });
-    });
-
-    describe('adaptRecursively', () => {
-
-        beforeEach( () => {
-            sinon.stub(adapterFactory, 'getEnumFromOneOf');
-            sinon.stub(adapterFactory, 'adaptRecursively');
-            adapterFactory.adaptRecursively.callThrough();
-        });
-
-        afterEach( () => {
-            adapterFactory.getEnumFromOneOf.restore();
-            adapterFactory.adaptRecursively.restore();
-        });
-
-        it ('should call recursively on nested arrays and object', () => {
-            const schema = {a: '', b: 1, c: null, d: {e: [{f: 1}, {g: 2}, null]}};
-
-            adapterFactory.adaptRecursively(schema);
-
-            const expectedArgs = [
-                schema,
-                schema.d,
-                schema.d.e,
-                schema.d.e[0],
-                schema.d.e[1],
-            ];
-
-            for (const args of expectedArgs) {
-                assert.calledWith(adapterFactory.adaptRecursively, args);
-            }
-
-            assert.callCount(adapterFactory.adaptRecursively, 5);
-        });
-
-        context('getEnumFromOneOf returns values', () => {
-
-            it ('should replace oneOf by enumNames/enum', () => {
-                adapterFactory.getEnumFromOneOf.returns({enumNames: ['A'], enumValues: ['a']});
-                const schema = {oneOf: [{}, {}], title: 'TITLE'};
-
-                const adapter = adapterFactory.adaptRecursively(schema);
-
-                expect(adapter).to.deep.equal({enum: ['a'], enumNames: ['A'], title: 'TITLE'});
-            });
-        });
-
-        context('getEnumFromOneOf thrown', () => {
-
-            it ('should not replace oneOf when getEnumFromOneOf throws', () => {
-                adapterFactory.getEnumFromOneOf.throws();
-                const schema = {oneOf: []};
-
-                const adapter = adapterFactory.adaptRecursively(schema);
-
-                expect(adapter.oneOf).to.exist;
-                expect(adapter.enum).to.not.exist;
-                expect(adapter.enumNames).to.not.exist;
-            });
-        });
-    });
-
-    describe('getEnumFromOneOf', () => {
-
-        beforeEach( () => {
-            sinon.stub(adapterFactory, 'schemaToConstant');
-        });
-
-        afterEach( () => {
-            adapterFactory.schemaToConstant.restore();
-        });
-
-        it ('should return as many enum values as the number of alternative schemas', () => {
-            const schema = {oneOf: [{}, {}, {}]};
-            adapterFactory.schemaToConstant.returns({name: 'A', value: 'a'});
-            const expectedLength = 3;
-
-            const {enumNames, enumValues} = adapterFactory.getEnumFromOneOf(schema);
-
-            expect(enumNames).lengthOf(expectedLength);
-            expect(enumValues).lengthOf(expectedLength);
-        });
-    });
-
-    describe('schemaToConstant', () => {
-
-        beforeEach( () => {
-            sinon.spy(adapterFactory, 'schemaToConstant');
-        });
-
-        afterEach( () => {
-            adapterFactory.schemaToConstant.restore();
-        });
-
-        const CONST_VALUE = "THE_CONST_VALUE";
-        const ENUM_VALUE = "THE_ENUM_VALUE";
-
-        it('should get value from const when it exists', () => {
-            const schema = {const: CONST_VALUE};
-
-            const {value} = adapterFactory.schemaToConstant(schema, 0);
-
-            expect(value).to.equal(CONST_VALUE);
-        });
-
-        it('should get value from const when const and enum are present', () => {
-            const schema = {enum: [ENUM_VALUE], const: CONST_VALUE};
-
-            const {value} = adapterFactory.schemaToConstant(schema, 0);
-
-            expect(value).to.equal(CONST_VALUE);
-        });
-
-        it('should get value from enum when it exists and there is no const', () => {
-            const schema = {enum: [ENUM_VALUE]};
-
-            const {value} = adapterFactory.schemaToConstant(schema, 0);
-
-            expect(value).to.equal(ENUM_VALUE);
-        });
-
-        it('should throw when enum is not an array', () => {
-            const schema = {enum: ENUM_VALUE};
-
-            expect(() => adapterFactory.schemaToConstant(schema, 0)).to.throw();
-
-        });
-
-        it('should get name from title property when it exists', () => {
-            const schema = {enum: ['ANYTHING'], title: 'THE_TITLE'};
-
-            const {name} = adapterFactory.schemaToConstant(schema, 0);
-
-            expect(name).to.equal('THE_TITLE');
-        });
-
-        context('the schema does not contain a title', () => {
-
-            context('the inferred value is a string', () => {
-
-                it('should get name from inferred value', () => {
-                    const schema = {enum: ['THE_VALUE']};
-
-                    const {name} = adapterFactory.schemaToConstant(schema, 0);
-
-                    expect(name).to.equal('THE_VALUE');
-                });
-            });
-
-            context('the inferred value is not a string', () => {
-
-                it('build the name from the id', () => {
-                    const id = 7;
-                    const schema = {enum: [{color: 'red'}]};
-
-                    const {name} = adapterFactory.schemaToConstant(schema, id);
-
-                    expect(name).to.match(RegExp(`^[^0-9]*${id}[^0-9]*$`));
-                });
-            });
-        });
-    });
-});
-
 describe('Resource', () => {
     it('should select "ResourceCreationForm" when action is "add"', () => {
         const params = {