budibase/packages/builder/tests/validateProps.spec.js

153 lines
3.9 KiB
JavaScript
Raw Normal View History

2019-07-19 19:03:58 +02:00
import {
validatePropsDefinition,
validateProps
} from "../src/userInterface/propsDefinitionParsing/validateProps";
2019-07-20 22:41:06 +02:00
import { createProps } from "../src/userInterface/propsDefinitionParsing/createProps";
2019-07-19 19:03:58 +02:00
// not that allot of this functionality is covered
// in createDefaultProps - as validate props uses that.
describe("validatePropsDefinition", () => {
it("should recursively validate array props and return no errors when valid", () => {
const propsDef = {
columns : {
type: "array",
2019-07-20 22:41:06 +02:00
elementDefinition: {
2019-07-19 19:03:58 +02:00
width: "number",
units: {
type: "string",
default: "px"
}
}
}
}
const errors = validatePropsDefinition(propsDef);
expect(errors).toEqual([]);
});
it("should recursively validate array props and return errors when invalid", () => {
const propsDef = {
columns : {
type: "array",
2019-07-20 22:41:06 +02:00
elementDefinition: {
2019-07-19 19:03:58 +02:00
width: "invlid type",
units: {
type: "string",
default: "px"
}
}
}
}
const errors = validatePropsDefinition(propsDef);
expect(errors.length).toEqual(1);
expect(errors[0].propName).toBe("width");
});
it("should return error when no options for options field", () => {
const propsDef = {
size: {
type: "options",
options: []
}
}
const errors = validatePropsDefinition(propsDef);
expect(errors.length).toEqual(1);
expect(errors[0].propName).toBe("size");
});
it("should not return error when options field has options", () => {
const propsDef = {
size: {
type: "options",
options: ["small", "medium", "large"]
}
}
const errors = validatePropsDefinition(propsDef);
expect(errors).toEqual([]);
});
});
const validPropDef = {
size: {
type: "options",
options: ["small", "medium", "large"],
default:"medium"
},
rowCount : "number",
columns : {
type: "array",
2019-07-20 22:41:06 +02:00
elementDefinition: {
2019-07-19 19:03:58 +02:00
width: "number",
units: {
type: "string",
default: "px"
}
}
}
};
const validProps = () => {
2019-07-20 22:41:06 +02:00
const { props } = createProps("some_component", validPropDef);
2019-07-19 19:03:58 +02:00
props.columns.push(
2019-07-20 22:41:06 +02:00
createProps("childcomponent", validPropDef.columns.elementDefinition).props);
2019-07-19 19:03:58 +02:00
return props;
}
describe("validateProps", () => {
it("should have no errors with a big list of valid props", () => {
const errors = validateProps(validPropDef, validProps(), true);
expect(errors).toEqual([]);
});
it("should return error with invalid value", () => {
const props = validProps();
props.rowCount = "1";
const errors = validateProps(validPropDef, props, true);
expect(errors.length).toEqual(1);
expect(errors[0].propName).toBe("rowCount");
});
it("should return error with invalid option", () => {
const props = validProps();
props.size = "really_small";
const errors = validateProps(validPropDef, props, true);
expect(errors.length).toEqual(1);
expect(errors[0].propName).toBe("size");
});
it("should return error with invalid array item", () => {
const props = validProps();
props.columns[0].width = "seven";
const errors = validateProps(validPropDef, props, true);
expect(errors.length).toEqual(1);
expect(errors[0].propName).toBe("width");
});
})