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
#6384)

* Fix for checkForFunctionProperty so that order does not matter
* Remove import for inspect
* replace with jest.fn()
* Add scenarios where object does not contain fn

---------

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>
(cherry picked from commit b70c327)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Apr 12, 2024
1 parent 696c927 commit c3c0b08
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
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 c3c0b08

Please sign in to comment.