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

189 lines
5.6 KiB
JavaScript
Raw Normal View History

2019-08-14 23:11:59 +02:00
import {
createProps,
createPropsAndDefinition
} from "../src/userInterface/pagesParsing/createProps";
2019-07-19 13:52:08 +02:00
import {
keys, some
} from "lodash/fp";
2019-08-14 23:11:59 +02:00
import { allComponents } from "./testData";
2019-07-19 13:52:08 +02:00
describe("createDefaultProps", () => {
it("should create a object with single string value, when default string field set", () => {
const propDef = {
fieldName: {type:"string", default:"something"}
};
2019-07-20 22:41:06 +02:00
const { props, errors } = createProps("some_component",propDef);
2019-07-19 13:52:08 +02:00
expect(errors).toEqual([]);
expect(props.fieldName).toBeDefined();
expect(props.fieldName).toBe("something");
2019-07-20 22:41:06 +02:00
expect(keys(props).length).toBe(2);
});
it("should set component name", () => {
const propDef = {
fieldName: {type:"string", default:"something"}
};
const { props, errors } = createProps("some_component",propDef);
expect(errors).toEqual([]);
expect(props._component).toBe("some_component");
});
it("should return error when component name not supplied", () => {
const propDef = {
fieldName: {type:"string", default:"something"}
};
const { errors } = createProps("",propDef);
expect(errors.length).toEqual(1);
2019-07-19 13:52:08 +02:00
});
it("should create a object with single blank string value, when no default", () => {
const propDef = {
fieldName: {type:"string"}
};
2019-07-20 22:41:06 +02:00
const { props, errors } = createProps("some_component",propDef);
2019-07-19 13:52:08 +02:00
expect(errors).toEqual([]);
expect(props.fieldName).toBeDefined();
expect(props.fieldName).toBe("");
});
it("should create a object with single blank string value, when prop definition is 'string' ", () => {
const propDef = {
fieldName: "string"
};
2019-07-20 22:41:06 +02:00
const { props, errors } = createProps("some_component",propDef);
2019-07-19 13:52:08 +02:00
expect(errors).toEqual([]);
expect(props.fieldName).toBeDefined();
expect(props.fieldName).toBe("");
});
it("should create a object with single fals value, when prop definition is 'bool' ", () => {
const propDef = {
2019-07-20 22:41:06 +02:00
isVisible: "bool"
2019-07-19 13:52:08 +02:00
};
2019-07-20 22:41:06 +02:00
const { props, errors } = createProps("some_component",propDef);
2019-07-19 13:52:08 +02:00
expect(errors).toEqual([]);
2019-07-20 22:41:06 +02:00
expect(props.isVisible).toBeDefined();
expect(props.isVisible).toBe(false);
2019-07-19 13:52:08 +02:00
});
it("should create a object with single 0 value, when prop definition is 'number' ", () => {
const propDef = {
2019-07-20 22:41:06 +02:00
width: "number"
2019-07-19 13:52:08 +02:00
};
2019-07-20 22:41:06 +02:00
const { props, errors } = createProps("some_component",propDef);
2019-07-19 13:52:08 +02:00
expect(errors).toEqual([]);
2019-07-20 22:41:06 +02:00
expect(props.width).toBeDefined();
expect(props.width).toBe(0);
2019-07-19 13:52:08 +02:00
});
2019-07-20 22:41:06 +02:00
it("should create a object with single empty array, when prop definition is 'array' ", () => {
2019-07-19 13:52:08 +02:00
const propDef = {
2019-07-20 22:41:06 +02:00
columns: "array"
2019-07-19 13:52:08 +02:00
};
2019-07-20 22:41:06 +02:00
const { props, errors } = createProps("some_component",propDef);
2019-07-19 13:52:08 +02:00
expect(errors).toEqual([]);
2019-07-20 22:41:06 +02:00
expect(props.columns).toBeDefined();
expect(props.columns).toEqual([]);
});
it("should create a object with single empty array, when prop definition is 'event' ", () => {
const propDef = {
onClick: "event"
};
const { props, errors } = createProps("some_component",propDef);
expect(errors).toEqual([]);
expect(props.onClick).toBeDefined();
expect(props.onClick).toEqual([]);
});
2019-07-20 22:41:06 +02:00
it("should create a object with single empty component props, when prop definition is 'component' ", () => {
const propDef = {
content: "component"
};
const { props, errors } = createProps("some_component",propDef);
expect(errors).toEqual([]);
expect(props.content).toBeDefined();
expect(props.content).toEqual({_component:""});
2019-07-19 13:52:08 +02:00
});
it("should create an object with multiple prop names", () => {
const propDef = {
fieldName: "string",
fieldLength: { type: "number", default: 500 }
};
2019-07-20 22:41:06 +02:00
const { props, errors } = createProps("some_component",propDef);
2019-07-19 13:52:08 +02:00
expect(errors).toEqual([]);
expect(props.fieldName).toBeDefined();
expect(props.fieldName).toBe("");
expect(props.fieldLength).toBeDefined();
expect(props.fieldLength).toBe(500);
2019-07-20 22:41:06 +02:00
expect(keys(props).length).toBe(3);
2019-07-19 13:52:08 +02:00
})
it("should return error when invalid type", () => {
const propDef = {
fieldName: "invalid type name",
fieldLength: { type: "invalid type name "}
};
2019-07-20 22:41:06 +02:00
const { errors } = createProps("some_component",propDef);
2019-07-19 13:52:08 +02:00
expect(errors.length).toBe(2);
expect(some(e => e.propName === "fieldName")(errors)).toBeTruthy();
expect(some(e => e.propName === "fieldLength")(errors)).toBeTruthy();
});
it("should return error default value is not of declared type", () => {
const propDef = {
fieldName: {type:"string", default: 1}
};
2019-07-20 22:41:06 +02:00
const { errors } = createProps("some_component",propDef);
2019-07-19 13:52:08 +02:00
expect(errors.length).toBe(1);
expect(some(e => e.propName === "fieldName")(errors)).toBeTruthy();
});
it("should merge in derived props", () => {
const propDef = {
fieldName: "string",
fieldLength: { type: "number", default: 500}
};
const derivedFrom = {
fieldName: "surname"
};
2019-08-14 23:11:59 +02:00
const { props, errors } = createProps("some_component",propDef, derivedFrom);
2019-07-19 13:52:08 +02:00
expect(errors.length).toBe(0);
expect(props.fieldName).toBe("surname");
expect(props.fieldLength).toBe(500);
});
2019-08-14 23:11:59 +02:00
})