2020-04-01 12:38:07 +02:00
|
|
|
import { createProps } from "../src/components/userInterface/pagesParsing/createProps"
|
2020-02-03 10:24:25 +01:00
|
|
|
import { keys, some } from "lodash/fp"
|
2020-02-10 16:51:09 +01:00
|
|
|
import { stripStandardProps } from "./testData"
|
2019-07-19 13:52:08 +02:00
|
|
|
|
|
|
|
describe("createDefaultProps", () => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const getcomponent = () => ({
|
2020-05-07 11:53:34 +02:00
|
|
|
_component: "some_component",
|
2020-02-03 10:24:25 +01:00
|
|
|
name: "some_component",
|
|
|
|
props: {
|
|
|
|
fieldName: { type: "string", default: "something" },
|
|
|
|
},
|
|
|
|
})
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
it("should create a object with single string value, when default string field set", () => {
|
|
|
|
const { props, errors } = createProps(getcomponent())
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(errors).toEqual([])
|
|
|
|
expect(props.fieldName).toBeDefined()
|
|
|
|
expect(props.fieldName).toBe("something")
|
2020-02-10 16:51:09 +01:00
|
|
|
stripStandardProps(props)
|
2020-08-13 12:24:58 +02:00
|
|
|
expect(keys(props).length).toBe(3)
|
2020-02-03 10:24:25 +01:00
|
|
|
})
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-05-07 11:53:34 +02:00
|
|
|
it("should set component _component", () => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const { props, errors } = createProps(getcomponent())
|
2019-07-20 22:41:06 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(errors).toEqual([])
|
|
|
|
expect(props._component).toBe("some_component")
|
|
|
|
})
|
2019-07-20 22:41:06 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
it("should create a object with single blank string value, when prop definition is 'string' ", () => {
|
|
|
|
const comp = getcomponent()
|
|
|
|
comp.props.fieldName = "string"
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const { props, errors } = createProps(comp)
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(errors).toEqual([])
|
|
|
|
expect(props.fieldName).toBeDefined()
|
|
|
|
expect(props.fieldName).toBe("")
|
|
|
|
})
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
it("should create a object with single fals value, when prop definition is 'bool' ", () => {
|
|
|
|
const comp = getcomponent()
|
|
|
|
comp.props.isVisible = "bool"
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const { props, errors } = createProps(comp)
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(errors).toEqual([])
|
|
|
|
expect(props.isVisible).toBeDefined()
|
|
|
|
expect(props.isVisible).toBe(false)
|
|
|
|
})
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
it("should create a object with single 0 value, when prop definition is 'number' ", () => {
|
|
|
|
const comp = getcomponent()
|
|
|
|
comp.props.width = "number"
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const { props, errors } = createProps(comp)
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(errors).toEqual([])
|
|
|
|
expect(props.width).toBeDefined()
|
|
|
|
expect(props.width).toBe(0)
|
|
|
|
})
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
it("should create a object with empty _children array, when children===true ", () => {
|
|
|
|
const comp = getcomponent()
|
|
|
|
comp.children = true
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const { props, errors } = createProps(comp)
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(errors).toEqual([])
|
|
|
|
expect(props._children).toBeDefined()
|
|
|
|
expect(props._children).toEqual([])
|
|
|
|
})
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
it("should create a object with single empty array, when prop definition is 'event' ", () => {
|
|
|
|
const comp = getcomponent()
|
|
|
|
comp.props.onClick = "event"
|
2020-01-18 00:06:42 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const { props, errors } = createProps(comp)
|
2019-07-20 22:41:06 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(errors).toEqual([])
|
|
|
|
expect(props.onClick).toBeDefined()
|
|
|
|
expect(props.onClick).toEqual([])
|
|
|
|
})
|
2019-09-19 05:35:40 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
it("should create a object children array when children == true ", () => {
|
|
|
|
const comp = getcomponent()
|
|
|
|
comp.children = true
|
2019-09-27 18:03:31 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const { props, errors } = createProps(comp)
|
2020-01-18 00:06:42 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(errors).toEqual([])
|
|
|
|
expect(props._children).toBeDefined()
|
|
|
|
expect(props._children).toEqual([])
|
|
|
|
})
|
2019-09-27 18:03:31 +02:00
|
|
|
|
2020-08-13 12:24:58 +02:00
|
|
|
it("should always create _children ", () => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const comp = getcomponent()
|
2020-08-13 12:24:58 +02:00
|
|
|
comp.children = false
|
2019-09-27 18:03:31 +02:00
|
|
|
|
2020-08-13 12:24:58 +02:00
|
|
|
const createRes1 = createProps(comp)
|
2020-01-18 00:06:42 +01:00
|
|
|
|
2020-08-13 12:24:58 +02:00
|
|
|
expect(createRes1.errors).toEqual([])
|
|
|
|
expect(createRes1.props._children).toBeDefined()
|
2019-07-20 22:41:06 +02:00
|
|
|
|
2020-08-13 12:24:58 +02:00
|
|
|
const comp2 = getcomponent()
|
|
|
|
comp2.children = true
|
2019-07-20 22:41:06 +02:00
|
|
|
|
2020-08-13 12:24:58 +02:00
|
|
|
const createRes2 = createProps(comp)
|
2020-01-18 00:06:42 +01:00
|
|
|
|
2020-08-13 12:24:58 +02:00
|
|
|
expect(createRes2.errors).toEqual([])
|
|
|
|
expect(createRes2.props._children).toBeDefined()
|
2020-02-03 10:24:25 +01:00
|
|
|
})
|
2020-01-18 00:06:42 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
it("should create an object with multiple prop names", () => {
|
|
|
|
const comp = getcomponent()
|
|
|
|
comp.props.fieldName = "string"
|
|
|
|
comp.props.fieldLength = { type: "number", default: 500 }
|
2020-01-18 00:06:42 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const { props, errors } = createProps(comp)
|
2020-01-18 00:06:42 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(errors).toEqual([])
|
|
|
|
expect(props.fieldName).toBeDefined()
|
|
|
|
expect(props.fieldName).toBe("")
|
|
|
|
expect(props.fieldLength).toBeDefined()
|
|
|
|
expect(props.fieldLength).toBe(500)
|
|
|
|
})
|
2020-01-18 00:06:42 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
it("should return error when invalid type", () => {
|
|
|
|
const comp = getcomponent()
|
|
|
|
comp.props.fieldName = "invalid type name"
|
|
|
|
comp.props.fieldLength = { type: "invalid type name " }
|
2020-01-18 00:06:42 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const { errors } = createProps(comp)
|
2020-01-18 00:06:42 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(errors.length).toBe(2)
|
|
|
|
expect(some(e => e.propName === "fieldName")(errors)).toBeTruthy()
|
|
|
|
expect(some(e => e.propName === "fieldLength")(errors)).toBeTruthy()
|
|
|
|
})
|
2020-01-18 00:06:42 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
it("should merge in derived props", () => {
|
|
|
|
const comp = getcomponent()
|
|
|
|
comp.props.fieldName = "string"
|
|
|
|
comp.props.fieldLength = { type: "number", default: 500 }
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const derivedFrom = {
|
|
|
|
fieldName: "surname",
|
|
|
|
}
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const { props, errors } = createProps(comp, derivedFrom)
|
2019-07-19 13:52:08 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(errors.length).toBe(0)
|
|
|
|
expect(props.fieldName).toBe("surname")
|
|
|
|
expect(props.fieldLength).toBe(500)
|
|
|
|
})
|
2020-02-25 16:21:23 +01:00
|
|
|
|
2020-02-10 16:51:09 +01:00
|
|
|
it("should create standard props", () => {
|
|
|
|
const comp = getcomponent()
|
|
|
|
comp.props.fieldName = { type: "string", default: 1 }
|
|
|
|
const { props } = createProps(comp)
|
|
|
|
expect(props._code).toBeDefined()
|
|
|
|
expect(props._styles).toBeDefined()
|
|
|
|
expect(props._code).toBeDefined()
|
|
|
|
})
|
2019-08-14 23:11:59 +02:00
|
|
|
})
|