Skip to content

fix(util-endpoints): call multi-level functions from callFunction #3929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions packages/util-endpoints/src/utils/callFunction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ describe(callFunction.name, () => {
};
const mockFunctionName = "mockFunctionName";
const mockReturn = "mockReturn";
const mockArgReturn = "mockArgReturn";

beforeEach(() => {
lib[mockFunctionName] = jest.fn().mockReturnValue(mockReturn);
(evaluateExpression as jest.Mock).mockReturnValue(mockArgReturn);
});

afterEach(() => {
Expand All @@ -32,15 +34,26 @@ describe(callFunction.name, () => {
it.each(["string", { ref: "ref" }, { fn: "fn", argv: [] }])(
"calls evaluateExpression for non-boolean arg: %s",
(arg) => {
const mockArgReturn = "mockArgReturn";
const mockFn = { fn: mockFunctionName, argv: [arg] };

(evaluateExpression as jest.Mock).mockReturnValue(mockArgReturn);

const result = callFunction(mockFn, mockOptions);
expect(result).toBe(mockReturn);
expect(evaluateExpression).toHaveBeenCalledWith(arg, "arg", mockOptions);
expect(lib[mockFunctionName]).toHaveBeenCalledWith(mockArgReturn);
}
);

it("calls multi-level function", () => {
const mockSecondLevelFunctionName = "mockSecondLevelFunctionName";
lib[mockFunctionName][mockSecondLevelFunctionName] = jest.fn().mockReturnValue(mockReturn);

const mockArg = "mockArg";
const mockFn = { fn: `${mockFunctionName}.${mockSecondLevelFunctionName}`, argv: [mockArg] };

const result = callFunction(mockFn, mockOptions);
expect(result).toBe(mockReturn);
expect(evaluateExpression).toHaveBeenCalledWith(mockArg, "arg", mockOptions);
expect(lib[mockFunctionName]).not.toHaveBeenCalled();
expect(lib[mockFunctionName][mockSecondLevelFunctionName]).toHaveBeenCalledWith(mockArgReturn);
});
});
4 changes: 2 additions & 2 deletions packages/util-endpoints/src/utils/callFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { EvaluateOptions, FunctionObject, FunctionReturn } from "../types";
import { evaluateExpression } from "./evaluateExpression";

export const callFunction = ({ fn, argv }: FunctionObject, options: EvaluateOptions): FunctionReturn => {
const argvArray = argv.map((arg) => (typeof arg === "boolean" ? arg : evaluateExpression(arg, "arg", options)));
const evaluatedArgs = argv.map((arg) => (typeof arg === "boolean" ? arg : evaluateExpression(arg, "arg", options)));
// @ts-ignore Element implicitly has an 'any' type
return lib[fn](...argvArray);
return fn.split(".").reduce((acc, key) => acc[key], lib)(...evaluatedArgs);
};