photoprism/frontend/tests/unit/common/form_test.js

132 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-01-09 04:41:33 +01:00
import Form, { FormPropertyType } from "common/form";
2020-07-02 10:03:00 +02:00
let chai = require("chai/chai");
2019-08-13 08:11:06 +02:00
let assert = chai.assert;
2021-01-09 04:41:33 +01:00
describe("common/form", () => {
it("setting and getting definition", () => {
const def = { foo: { type: FormPropertyType.String, caption: "Foo" } };
const form = new Form();
2021-01-09 04:41:33 +01:00
form.setDefinition(def);
2021-01-09 04:41:33 +01:00
const result = form.getDefinition();
assert.equal(result, def);
});
2021-01-09 04:41:33 +01:00
it("setting and getting a value according to type", () => {
const def = {
foo: { type: FormPropertyType.String, caption: "Foo" },
};
const form = new Form();
2021-01-09 04:41:33 +01:00
form.setDefinition(def);
form.setValue("foo", "test");
2021-01-09 04:41:33 +01:00
const result = form.getValue("foo");
assert.equal(result, "test");
});
2021-01-09 04:41:33 +01:00
it("setting a value not according to type", () => {
const def = {
foo: { type: FormPropertyType.String, caption: "Foo" },
};
const form = new Form();
2021-01-09 04:41:33 +01:00
form.setDefinition(def);
2021-01-09 04:41:33 +01:00
assert.throws(() => {
form.setValue("foo", 3);
});
2021-01-09 04:41:33 +01:00
});
2021-01-09 04:41:33 +01:00
it("setting and getting a value for missing property throws exception", () => {
const def = {
foo: { type: FormPropertyType.String, caption: "Foo" },
};
const form = new Form();
2021-01-09 04:41:33 +01:00
form.setDefinition(def);
2021-01-09 04:41:33 +01:00
assert.throws(() => {
form.setValue("bar", 3);
});
2021-01-09 04:41:33 +01:00
assert.throws(() => {
form.getValue("bar");
});
2021-01-09 04:41:33 +01:00
});
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" },
baz: { type: FormPropertyType.String, caption: "XX" },
};
const form = new Form();
form.setDefinition(def);
form.setValues({ foo: "test", baz: "yyy" });
const result = form.getValues();
assert.equal(result.foo, "test");
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, "");
});
});