2018-11-27 22:43:32 +01:00
|
|
|
import Form, { FormPropertyType } from 'common/form';
|
2018-07-27 17:31:39 +02:00
|
|
|
|
2019-08-13 08:11:06 +02:00
|
|
|
let chai = require('../../../node_modules/chai/chai');
|
|
|
|
let assert = chai.assert;
|
|
|
|
|
2018-07-27 17:31:39 +02:00
|
|
|
describe('common/form', () => {
|
2018-11-27 22:43:32 +01:00
|
|
|
it('setting and getting definition', () => {
|
|
|
|
const def = { foo: { type: FormPropertyType.String, caption: 'Foo' } };
|
2018-07-27 17:31:39 +02:00
|
|
|
const form = new Form();
|
|
|
|
|
|
|
|
form.setDefinition(def);
|
|
|
|
|
2018-11-27 22:43:32 +01:00
|
|
|
const result = form.getDefinition();
|
2018-07-27 17:31:39 +02:00
|
|
|
assert.equal(result, def);
|
|
|
|
});
|
2018-11-27 22:43:32 +01:00
|
|
|
|
|
|
|
it('setting and getting a value according to type', () => {
|
|
|
|
const def = {
|
|
|
|
foo: { type: FormPropertyType.String, caption: 'Foo' },
|
|
|
|
};
|
|
|
|
const form = new Form();
|
|
|
|
|
|
|
|
form.setDefinition(def);
|
|
|
|
form.setValue('foo', 'test');
|
|
|
|
|
|
|
|
const result = form.getValue('foo');
|
|
|
|
assert.equal(result, 'test');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('setting a value not according to type', () => {
|
|
|
|
const def = {
|
|
|
|
foo: { type: FormPropertyType.String, caption: 'Foo' },
|
|
|
|
};
|
|
|
|
const form = new Form();
|
|
|
|
|
|
|
|
form.setDefinition(def);
|
|
|
|
|
|
|
|
assert.throws(() => {
|
|
|
|
form.setValue('foo', 3);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('setting and getting a value for missing property throws exception', () => {
|
|
|
|
const def = {
|
|
|
|
foo: { type: FormPropertyType.String, caption: 'Foo' },
|
|
|
|
};
|
|
|
|
const form = new Form();
|
|
|
|
|
|
|
|
form.setDefinition(def);
|
|
|
|
|
|
|
|
assert.throws(() => {
|
|
|
|
form.setValue('bar', 3);
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.throws(() => {
|
|
|
|
form.getValue('bar');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('setting and getting a complex value', () => {
|
|
|
|
const complexValue = {
|
|
|
|
something: 'abc',
|
|
|
|
another: 'def',
|
|
|
|
};
|
|
|
|
const def = {
|
|
|
|
foo: {
|
|
|
|
type: FormPropertyType.Object,
|
|
|
|
caption: 'Foo',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const form = new Form();
|
|
|
|
|
|
|
|
form.setDefinition(def);
|
|
|
|
form.setValue('foo', complexValue);
|
|
|
|
|
|
|
|
const result = form.getValue('foo');
|
|
|
|
assert.deepEqual(result, complexValue);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('setting and getting more values at once', () => {
|
|
|
|
const def = {
|
|
|
|
foo: { type: FormPropertyType.String, caption: 'Foo' },
|
2019-07-26 08:41:39 +02:00
|
|
|
baz: { type: FormPropertyType.String, caption: 'XX' },
|
2018-11-27 22:43:32 +01:00
|
|
|
};
|
|
|
|
const form = new Form();
|
|
|
|
|
|
|
|
form.setDefinition(def);
|
2019-07-26 08:41:39 +02:00
|
|
|
form.setValues({ foo: 'test', baz: 'yyy'});
|
2018-11-27 22:43:32 +01:00
|
|
|
|
|
|
|
const result = form.getValues();
|
|
|
|
assert.equal(result.foo, 'test');
|
2019-07-26 08:41:39 +02:00
|
|
|
assert.equal(result.baz, 'yyy');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('getting options of fieldname', () => {
|
|
|
|
const def = {
|
|
|
|
search: {
|
|
|
|
type: FormPropertyType.String,
|
|
|
|
caption: 'Search',
|
|
|
|
label: {options: 'tiles', text: 'Tiles'},
|
|
|
|
options: [
|
|
|
|
{value: 'tiles', text: 'Tiles'},
|
|
|
|
{value: 'mosaic', text: 'Mosaic'},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const form = new Form();
|
|
|
|
|
|
|
|
form.setDefinition(def);
|
|
|
|
|
|
|
|
const result = form.getOptions("search");
|
|
|
|
assert.equal(result[0].value, "tiles");
|
|
|
|
assert.equal(result[1].text, "Mosaic");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('getting not existing options returns empty object', () => {
|
|
|
|
const def = {
|
|
|
|
foo: {
|
|
|
|
type: FormPropertyType.Object,
|
|
|
|
caption: 'Foo',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const form = new Form();
|
|
|
|
|
|
|
|
form.setDefinition(def);
|
|
|
|
|
|
|
|
const result = form.getOptions("foo");
|
|
|
|
assert.equal(result[0].option, "");
|
|
|
|
assert.equal(result[0].label, "");
|
2018-11-27 22:43:32 +01:00
|
|
|
});
|
|
|
|
});
|