From e577fcdb70737fbaaf304da1d411ef5a20fb5dda Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 9 Apr 2024 20:14:29 +0000 Subject: [PATCH] Fix for checkForFunctionProperty so that order does not matter (#6248) * Fix for checkForFunctionProperty so that order does not matter Signed-off-by: Craig Perkins * Remove import for inspect Signed-off-by: Craig Perkins * Add to CHANGELOG Signed-off-by: Craig Perkins * replace with jest.fn() Signed-off-by: Craig Perkins * Update CHANGELOG Signed-off-by: Craig Perkins * Add scenarios where object does not contain fn Signed-off-by: Craig Perkins --------- Signed-off-by: Craig Perkins Signed-off-by: SuZhou-Joe Co-authored-by: SuZhou-Joe Co-authored-by: ZilongX <99905560+ZilongX@users.noreply.github.com> (cherry picked from commit b70c3273151759bf9131e1dc4b167ef4f822c880) Signed-off-by: github-actions[bot] # Conflicts: # CHANGELOG.md --- .../public/data_model/utils.test.js | 51 ++++++++++++++++--- .../vis_type_vega/public/data_model/utils.ts | 16 +++--- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/plugins/vis_type_vega/public/data_model/utils.test.js b/src/plugins/vis_type_vega/public/data_model/utils.test.js index 9fe648c71139..9d6d67d793bb 100644 --- a/src/plugins/vis_type_vega/public/data_model/utils.test.js +++ b/src/plugins/vis_type_vega/public/data_model/utils.test.js @@ -59,9 +59,7 @@ describe('Utils.handleInvalidQuery', () => { test('should return null if input object has function as property', async () => { const input = { key1: 'value1', - key2: () => { - alert('Hello!'); - }, + key2: () => jest.fn(), }; expect(Utils.handleInvalidQuery(input)).toBe(null); @@ -71,9 +69,7 @@ describe('Utils.handleInvalidQuery', () => { const input = { key1: 'value1', key2: { - func: () => { - alert('Hello!'); - }, + func: () => jest.fn(), }, }; expect(Utils.handleInvalidQuery(input)).toBe(null); @@ -91,4 +87,47 @@ describe('Utils.handleInvalidQuery', () => { }).toThrowError(); }); }); + + test('should identify object contains function properties', async () => { + const input1 = { + key1: 'value1', + key2: () => jest.fn(), + }; + + expect(Utils.checkForFunctionProperty(input1)).toBe(true); + + const input2 = { + key1: () => jest.fn(), + key2: 'value1', + }; + + expect(Utils.checkForFunctionProperty(input2)).toBe(true); + + const nestedInput = { + key1: { + nestedKey1: 'nestedValue1', + nestedKey2: () => jest.fn(), + }, + key2: 'value1', + }; + + expect(Utils.checkForFunctionProperty(nestedInput)).toBe(true); + + const inputWithoutFn = { + key1: 'value1', + key2: 'value2', + }; + + expect(Utils.checkForFunctionProperty(inputWithoutFn)).toBe(false); + + const nestedInputWithoutFn = { + key1: { + nestedKey1: 'nestedValue1', + nestedKey2: 'nestedValue2', + }, + key2: 'value2', + }; + + expect(Utils.checkForFunctionProperty(nestedInputWithoutFn)).toBe(false); + }); }); diff --git a/src/plugins/vis_type_vega/public/data_model/utils.ts b/src/plugins/vis_type_vega/public/data_model/utils.ts index 4e7a85062354..2aba9f9fa577 100644 --- a/src/plugins/vis_type_vega/public/data_model/utils.ts +++ b/src/plugins/vis_type_vega/public/data_model/utils.ts @@ -79,14 +79,14 @@ export class Utils { } static checkForFunctionProperty(object: object): boolean { - let result = false; - Object.values(object).forEach((value) => { - result = - typeof value === 'function' - ? true - : Utils.isObject(value) && Utils.checkForFunctionProperty(value); - }); - return result; + for (const value of Object.values(object)) { + if (typeof value === 'function') { + return true; + } else if (Utils.isObject(value) && Utils.checkForFunctionProperty(value)) { + return true; + } + } + return false; } static handleInvalidDate(date: unknown): number | string | Date | null {