1
0
Fork 0
mirror of synced 2024-09-05 12:11:40 +12:00
budibase/packages/builder/tests/validateProps.spec.js

153 lines
3.9 KiB
JavaScript
Raw Normal View History

2019-07-20 05:03:58 +12:00
import {
validatePropsDefinition,
validateProps
} from "../src/userInterface/propsDefinitionParsing/validateProps";
2019-07-21 08:41:06 +12:00
import { createProps } from "../src/userInterface/propsDefinitionParsing/createProps";
2019-07-20 05:03:58 +12: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-21 08:41:06 +12:00
elementDefinition: {
2019-07-20 05:03:58 +12: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-21 08:41:06 +12:00
elementDefinition: {
2019-07-20 05:03:58 +12: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-21 08:41:06 +12:00
elementDefinition: {
2019-07-20 05:03:58 +12:00
width: "number",
units: {
type: "string",
default: "px"
}
}
}
};
const validProps = () => {
2019-07-21 08:41:06 +12:00
const { props } = createProps("some_component", validPropDef);
2019-07-20 05:03:58 +12:00
props.columns.push(
2019-07-21 08:41:06 +12:00
createProps("childcomponent", validPropDef.columns.elementDefinition).props);
2019-07-20 05:03:58 +12: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");
});
})