Skip to content

Commit

Permalink
fix(util-endpoints) improve evaluateCondition's debug msg smithy-lang…
Browse files Browse the repository at this point in the history
  • Loading branch information
necisam committed May 9, 2024
1 parent 671aa70 commit e1dca57
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions packages/util-endpoints/src/utils/evaluateCondition.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { debugId, toDebugString } from "../debug";
import { EndpointError, EvaluateOptions } from "../types";
import { callFunction } from "./callFunction";
import { evaluateCondition } from "./evaluateCondition";
Expand Down Expand Up @@ -31,19 +32,33 @@ describe(evaluateCondition.name, () => {
[false, [false, 0, -0, null, undefined, NaN]],
])("returns %s for", (result, testCases) => {
it.each(testCases)(`value: '%s'`, (mockReturn) => {
const mockLogger = {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};
(callFunction as jest.Mock).mockReturnValue(mockReturn);
const { result, toAssign } = evaluateCondition(mockFnArgs, mockOptions);
const { result, toAssign } = evaluateCondition(mockFnArgs, {...mockOptions, logger:mockLogger});
expect(result).toBe(result);
expect(toAssign).toBeUndefined();
expect(mockLogger.debug).nthCalledWith(1, `${debugId} evaluateCondition: ${toDebugString(mockFnArgs)} = ${mockReturn}`)
});
});
});

it("returns assigned value if defined", () => {
const mockAssignedValue = "mockAssignedValue";
const mockLogger = {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};
(callFunction as jest.Mock).mockReturnValue(mockAssignedValue);
const { result, toAssign } = evaluateCondition({ assign: mockAssign, ...mockFnArgs }, mockOptions);
const { result, toAssign } = evaluateCondition({ assign: mockAssign, ...mockFnArgs }, {...mockOptions, logger:mockLogger});
expect(result).toBe(true);
expect(toAssign).toEqual({ name: mockAssign, value: mockAssignedValue });
expect(mockLogger.debug).nthCalledWith(1, `${debugId} evaluateCondition: ${toDebugString(mockFnArgs)} = ${mockAssignedValue}`)
});
});
2 changes: 1 addition & 1 deletion packages/util-endpoints/src/utils/evaluateCondition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const evaluateCondition = ({ assign, ...fnArgs }: ConditionObject, option
}
const value = callFunction(fnArgs, options);

options.logger?.debug?.(debugId, `evaluateCondition: ${toDebugString(fnArgs)} = ${toDebugString(value)}`);
options.logger?.debug?.(`${debugId} evaluateCondition: ${toDebugString(fnArgs)} = ${toDebugString(value)}`);

return {
result: value === "" ? true : !!value,
Expand Down

0 comments on commit e1dca57

Please sign in to comment.