diff --git a/CHANGELOG.md b/CHANGELOG.md index 290b0531f838..800b987d2f6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -107,6 +107,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [Multiple Datasource] Fix sslConfig for multiple datasource to handle when certificateAuthorities is unset ([#6282](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6282)) - [BUG][Multiple Datasource]Fix bug in data source aggregated view to change it to depend on displayAllCompatibleDataSources property to show the badge value ([#6291](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6291)) - [BUG][Multiple Datasource]Read hideLocalCluster setting from yml and set in data source selector and data source menu ([#6361](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6361)) +- [BUG] Fix for checkForFunctionProperty so that order does not matter ([#6248](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6248)) ### 🚞 Infrastructure 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 {