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

146 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-07-28 09:03:11 +02:00
import {
searchAllComponents,
getExactComponent,
getAncestorProps
} from "../src/userInterface/pagesParsing/searchComponents";
describe("searchAllComponents", () => {
it("should match derived component by name", () => {
const results = searchAllComponents(
2019-08-04 23:21:16 +02:00
components(),
2019-07-28 13:45:00 +02:00
"password"
2019-07-28 09:03:11 +02:00
);
expect(results.length).toBe(1);
2019-07-28 13:45:00 +02:00
expect(results[0].name).toBe("common/PasswordBox");
2019-07-28 09:03:11 +02:00
2019-07-28 13:45:00 +02:00
});
it("should match derived component by tag", () => {
const results = searchAllComponents(
2019-08-04 23:21:16 +02:00
components(),
2019-07-28 13:45:00 +02:00
"mask"
);
expect(results.length).toBe(1);
expect(results[0].name).toBe("common/PasswordBox");
});
it("should match component if ancestor matches", () => {
const results = searchAllComponents(
2019-08-04 23:21:16 +02:00
components(),
2019-07-28 13:45:00 +02:00
"smalltext"
);
expect(results.length).toBe(2);
});
2019-07-28 09:03:11 +02:00
});
describe("getExactComponent", () => {
2019-07-28 13:45:00 +02:00
it("should get component by name", () => {
const result = getExactComponent(
2019-08-04 23:21:16 +02:00
components(),
2019-07-28 13:45:00 +02:00
"common/SmallTextbox"
)
expect(result).toBeDefined();
expect(result.name).toBe("common/SmallTextbox");
});
it("should return nothing when no result (should not fail)", () => {
const result = getExactComponent(
2019-08-04 23:21:16 +02:00
components(),
2019-07-28 13:45:00 +02:00
"bla/bla/bla"
)
expect(result).not.toBeDefined();
});
2019-07-28 09:03:11 +02:00
});
describe("getAncestorProps", () => {
2019-07-28 13:45:00 +02:00
it("should return props of root component", () => {
const result = getAncestorProps(
2019-08-04 23:21:16 +02:00
components(),
2019-07-28 13:45:00 +02:00
"budibase-components/TextBox"
);
expect(result).toEqual([
2019-08-04 23:21:16 +02:00
components()[0].props
2019-07-28 13:45:00 +02:00
]);
});
it("should return props of all ancestors and current component, in order", () => {
2019-08-04 23:21:16 +02:00
const components = components();
2019-07-28 13:45:00 +02:00
const result = getAncestorProps(
2019-08-04 23:21:16 +02:00
components,
2019-07-28 13:45:00 +02:00
"common/PasswordBox"
);
expect(result).toEqual([
root[0].props,
2019-08-04 23:21:16 +02:00
{_component: "budibase-components/TextBox", ...components[2].props},
{_component: "common/SmallTextbox", ...components[3].props}
2019-07-28 13:45:00 +02:00
]);
});
2019-07-28 09:03:11 +02:00
})
2019-08-04 23:21:16 +02:00
const components = () => ([
{
name: "budibase-components/TextBox",
tags: ["Text", "input"],
props: {
size: {type:"options", options:["small", "medium", "large"]},
isPassword: "boolean",
placeholder: "string",
label:"string"
}
},
{
name: "budibase-components/Button",
tags: ["input"],
props: {
size: {type:"options", options:["small", "medium", "large"]},
css: "string",
content: "component"
}
},
2019-07-28 09:03:11 +02:00
{
inherits:"budibase-components/TextBox",
name: "common/SmallTextbox",
props: {
size: "small"
}
},
{
inherits:"common/SmallTextbox",
name: "common/PasswordBox",
tags: ["mask"],
props: {
isPassword: true
}
},
{
inherits:"budibase-components/Button",
name:"PrimaryButton",
props: {
css:"btn-primary"
}
}
])