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

104 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-07-28 09:03:11 +02:00
import {
searchAllComponents,
getExactComponent,
getAncestorProps
} from "../src/userInterface/pagesParsing/searchComponents";
2019-08-14 23:11:59 +02:00
import { allComponents } from "./testData";
2019-07-28 09:03:11 +02:00
describe("searchAllComponents", () => {
it("should match derived component by name", () => {
const results = searchAllComponents(
2019-08-14 23:11:59 +02:00
allComponents(),
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-14 23:11:59 +02:00
allComponents(),
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-14 23:11:59 +02:00
allComponents(),
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-14 23:11:59 +02:00
allComponents(),
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-14 23:11:59 +02:00
allComponents(),
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-14 23:11:59 +02:00
allComponents(),
2019-07-28 13:45:00 +02:00
"budibase-components/TextBox"
);
expect(result).toEqual([
2019-08-14 23:11:59 +02:00
allComponents()[0].props
2019-07-28 13:45:00 +02:00
]);
});
it("should return props of all ancestors and current component, in order", () => {
2019-08-14 23:11:59 +02:00
const components = allComponents();
2019-07-28 13:45:00 +02:00
const result = getAncestorProps(
2019-08-14 23:11:59 +02:00
components,
2019-07-28 13:45:00 +02:00
"common/PasswordBox"
);
expect(result).toEqual([
2019-08-14 23:11:59 +02:00
components[0].props,
{...components[4].props},
{...components[5].props}
2019-07-28 13:45:00 +02:00
]);
});
2019-07-28 09:03:11 +02:00
})
2019-08-14 23:11:59 +02:00