Skip to content

Commit

Permalink
Fix for checkForFunctionProperty so that order does not matter (#6248)
Browse files Browse the repository at this point in the history
* Fix for checkForFunctionProperty so that order does not matter

Signed-off-by: Craig Perkins <cwperx@amazon.com>

* Remove import for inspect

Signed-off-by: Craig Perkins <cwperx@amazon.com>

* Add to CHANGELOG

Signed-off-by: Craig Perkins <cwperx@amazon.com>

* replace with jest.fn()

Signed-off-by: Craig Perkins <cwperx@amazon.com>

* Update CHANGELOG

Signed-off-by: Craig Perkins <cwperx@amazon.com>

* Add scenarios where object does not contain fn

Signed-off-by: Craig Perkins <cwperx@amazon.com>

---------

Signed-off-by: Craig Perkins <cwperx@amazon.com>
Signed-off-by: SuZhou-Joe <suzhou@amazon.com>
Co-authored-by: SuZhou-Joe <suzhou@amazon.com>
Co-authored-by: ZilongX <99905560+ZilongX@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 9, 2024
1 parent 87deeda commit b70c327
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
51 changes: 45 additions & 6 deletions src/plugins/vis_type_vega/public/data_model/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -71,9 +69,7 @@ describe('Utils.handleInvalidQuery', () => {
const input = {
key1: 'value1',
key2: {
func: () => {
alert('Hello!');
},
func: () => jest.fn(),
},
};
expect(Utils.handleInvalidQuery(input)).toBe(null);
Expand All @@ -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);
});
});
16 changes: 8 additions & 8 deletions src/plugins/vis_type_vega/public/data_model/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b70c327

Please sign in to comment.