Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Fix for checkForFunctionProperty so that order does not matter #6384

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading