search tests

This commit is contained in:
michael shanks 2019-07-28 12:45:00 +01:00
parent 1413ee6f2c
commit fe67c21627
2 changed files with 85 additions and 6 deletions

View File

@ -19,9 +19,10 @@ const isRootComponent = c => isUndefined(c.inherits);
export const searchAllComponents = (derivedComponents, rootComponents, phrase) => { export const searchAllComponents = (derivedComponents, rootComponents, phrase) => {
const hasPhrase = (...vals) => pipe(vals, [ const hasPhrase = (...vals) =>
some(v => includes(phrase)(v)) pipe(vals, [
]); some(v => includes(normalString(phrase))(normalString(v)))
]);
const rootComponentMatches = c => const rootComponentMatches = c =>
hasPhrase(c.name, ...(c.tags || [])); hasPhrase(c.name, ...(c.tags || []));

View File

@ -12,22 +12,100 @@ describe("searchAllComponents", () => {
const results = searchAllComponents( const results = searchAllComponents(
derivedComponents(), derivedComponents(),
rootComponents(), rootComponents(),
"smalltextbox" "password"
); );
expect(results.length).toBe(1); expect(results.length).toBe(1);
expect(results[0].name).toBe("common/SmallTextbox"); expect(results[0].name).toBe("common/PasswordBox");
}) });
it("should match derived component by tag", () => {
const results = searchAllComponents(
derivedComponents(),
rootComponents(),
"mask"
);
expect(results.length).toBe(1);
expect(results[0].name).toBe("common/PasswordBox");
});
it("should match component if ancestor matches", () => {
const results = searchAllComponents(
derivedComponents(),
rootComponents(),
"smalltext"
);
expect(results.length).toBe(2);
});
}); });
describe("getExactComponent", () => { describe("getExactComponent", () => {
it("should get component by name", () => {
const result = getExactComponent(
derivedComponents(),
rootComponents(),
"common/SmallTextbox"
)
expect(result).toBeDefined();
expect(result.name).toBe("common/SmallTextbox");
});
it("should return nothing when no result (should not fail)", () => {
const result = getExactComponent(
derivedComponents(),
rootComponents(),
"bla/bla/bla"
)
expect(result).not.toBeDefined();
});
}); });
describe("getAncestorProps", () => { describe("getAncestorProps", () => {
it("should return props of root component", () => {
const result = getAncestorProps(
derivedComponents(),
rootComponents(),
"budibase-components/TextBox"
);
expect(result).toEqual([
rootComponents()[0].props
]);
});
it("should return props of all ancestors and current component, in order", () => {
const derived = derivedComponents();
const root = rootComponents();
const result = getAncestorProps(
derived,
root,
"common/PasswordBox"
);
expect(result).toEqual([
root[0].props,
{_component: "budibase-components/TextBox", ...derived[0].props},
{_component: "common/SmallTextbox", ...derived[1].props}
]);
});
}) })
const derivedComponents = () => ([ const derivedComponents = () => ([