UNPKG

q42-cms-components

Version:

Front-end package that provides a UI on top of the QMS back-end

358 lines (338 loc) 8.64 kB
/* global describe, it, expect, spyOn */ import {initialValue, initialWithQuery} from './schema-scripts'; describe('intialValue', function() { it('initialize strings', function() { const schema = { type: 'object', properties: { foo: { type: 'string' }, bar: { type: 'string', default: 'bar' }, baz: { type: 'string', required: true } }, }; const initial = initialValue(schema); expect(initial.foo).toBe(null); expect(initial.bar).toBe('bar'); expect(initial.baz).toBe(''); expect(initial.isNew).toBe(true); }); it('initialize numbers', function () { const schema = { type: 'object', properties: { foo: { type: 'number' }, bar: { type: 'number', default: 42 }, baz: { type: 'number', required: true } }, }; const initial = initialValue(schema); expect(initial.foo).toBe(null); expect(initial.bar).toBe(42); expect(initial.baz).toBe(0); }); it('initialize booleans', function () { const schema = { type: 'object', properties: { foo: { type: 'boolean' }, bar: { type: 'boolean', default: true }, baz: { type: 'boolean', required: true } }, }; const initial = initialValue(schema); expect(initial.foo).toBe(false); expect(initial.bar).toBe(true); expect(initial.baz).toBe(false); }); it('initialize enums', function () { const schema = { type: 'object', properties: { foo: { enum: ['enum1', 'enum2'] }, }, }; const initial = initialValue(schema); expect(initial.foo).toBe('enum1'); }); it('initialize datetime-picker', function () { const schema = { type: 'object', properties: { foo: { type: 'string', format: 'datetime-picker', }, baz: { type: 'string', format: 'datetime-picker', required: true } }, }; const initial = initialValue(schema); expect(initial.foo).toBe(null); expect(initial.baz).toBeInstanceOf(Date); }); it('initialize optional object with null', function () { const schema = { type: 'object', required: false, properties: { foo: { type: 'object' } }, }; const initial = initialValue(schema); expect(initial.foo).toBeNull(); }); it('initialize required object', function () { const schema = { type: 'object', properties: { foo: { type: 'object', required: true, properties: { bar: { type: 'string', default: 'nested', } } } }, }; const initial = initialValue(schema); expect(initial.foo).toBeInstanceOf(Object); expect(initial.foo.bar).toBe('nested'); }); it('initialize array', function () { const schema = { type: 'object', properties: { foo: { type: 'array' }, bar: { type: 'array', default: [1, 2, 3] }, baz: { type: 'array', required: true } }, }; const initial = initialValue(schema); expect(initial.foo).toHaveLength(0); expect(initial.bar).toEqual([1, 2, 3]); expect(initial.baz).toHaveLength(0); }); it('initialize array with minLength items', function () { const schema = { type: 'object', properties: { foo: { type: 'array', minLength: 1, items: { type: 'object', properties: { bar: { type: 'string', required: true, } } } }, }, }; const initial = initialValue(schema); expect(initial.foo.length).toBe(1); expect(initial.foo[0].$type).toBe(null); expect(initial.foo[0].bar).toBe(''); expect(initial.foo[0].isNew).toBe(true); }); it('initialize array with oneOf items', function () { const schema = { type: 'object', properties: { foo: { type: 'array', minLength: 1, items: { oneOf: [ { type: 'object', properties: { $type: { enum: ['Q42.Cms.SomeType'], } } }, { type: 'object', properties: { $type: { enum: ['Q42.Cms.SomeOtherType'], } } } ] } }, }, }; const initial = initialValue(schema); expect(initial.foo).toHaveLength(0); }); }); describe('initialWithQuery', function () { it('should generate a default value', function() { const schema = { type: 'object', properties: { foo: { type: 'boolean' }, bar: { type: 'string', default: 'bar' }, baz: { type: 'string', format: 'datetime-picker', required: true } }, }; const query = {}; const initial = initialWithQuery(schema, query); expect(initial.foo).toBe(false); expect(initial.bar).toBe('bar'); expect(initial.baz).toBeInstanceOf(Date); }); it('should override initial value', function() { const schema = { type: 'object', properties: { foo: { type: 'boolean' }, bar: { type: 'string', default: 'bar' }, baz: { type: 'string', format: 'datetime-picker', required: true } }, }; const query = {foo: 'True', bar: 'BAR', baz: '2018-08-14T20:05:00+02:00'}; const initial = initialWithQuery(schema, query); expect(initial.foo).toBe(true); expect(initial.bar).toBe('BAR'); expect(initial.baz).toBeInstanceOf(Date); }); it('should ignore unknown properties in query string', function() { const schema = { type: 'object', properties: { foo: { type: 'boolean' }, }, }; const query = {unknown: 'should not be there'}; const initial = initialWithQuery(schema, query); expect(initial.unknown).toBeUndefined(); }); it('should initialize non-required nested object', function() { const schema = { type: 'object', properties: { foo: { type: 'object', properties: { bar: { type: 'string', required: true, } } }, }, }; const query = {'foo.bar': 'BAR!'}; const initial = initialWithQuery(schema, query); expect(initial.foo).toBeInstanceOf(Object); expect(initial.foo.bar).toBe('BAR!'); }); it('should not initialize non-required nested object when an unknown subproperty is given', function() { const schema = { type: 'object', properties: { foo: { type: 'object', properties: { bar: { type: 'string', required: true, } } }, }, }; const query = {'foo.unkown': 'should not be there'}; const initial = initialWithQuery(schema, query); expect(initial.foo).toBeNull(); }); it('should initialize required nested object when an unknown subproperty is given', function() { const schema = { type: 'object', properties: { foo: { type: 'object', required: true, properties: { bar: { type: 'string', required: true, } } }, }, }; const query = {'foo.unkown': 'should not be there'}; const initial = initialWithQuery(schema, query); expect(initial.foo).toBeInstanceOf(Object); expect(initial.foo.bar).toBe(''); }); });