From e1dca579a7bb5fc7711de34c94e9b9898cf55f45 Mon Sep 17 00:00:00 2001 From: necisam Date: Thu, 9 May 2024 15:07:49 +0800 Subject: [PATCH] fix(util-endpoints) improve evaluateCondition's debug msg #1267 --- .../src/utils/evaluateCondition.spec.ts | 19 +++++++++++++++++-- .../src/utils/evaluateCondition.ts | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/util-endpoints/src/utils/evaluateCondition.spec.ts b/packages/util-endpoints/src/utils/evaluateCondition.spec.ts index 65348c2bf75..9d6dc119df8 100644 --- a/packages/util-endpoints/src/utils/evaluateCondition.spec.ts +++ b/packages/util-endpoints/src/utils/evaluateCondition.spec.ts @@ -1,3 +1,4 @@ +import { debugId, toDebugString } from "../debug"; import { EndpointError, EvaluateOptions } from "../types"; import { callFunction } from "./callFunction"; import { evaluateCondition } from "./evaluateCondition"; @@ -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}`) }); }); diff --git a/packages/util-endpoints/src/utils/evaluateCondition.ts b/packages/util-endpoints/src/utils/evaluateCondition.ts index 7f26abf0cdf..45d5f1b12a7 100644 --- a/packages/util-endpoints/src/utils/evaluateCondition.ts +++ b/packages/util-endpoints/src/utils/evaluateCondition.ts @@ -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,