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

Unit test for QueryUtils, application, plugin #96

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

KishoreKicha14
Copy link
Contributor

Description

  • Implemented unit tests for the following components:
    • QueryUtils.ts
    • application.tsx
    • plugin.ts
  • Improved test coverage across the codebase:
    • QueryUtils.ts coverage increased from 0% to 85.71%.
    • Achieved 100% coverage for application.tsx and plugin.ts.
    • Overall Query Insights coverage improved from 82.57% to 85.71%.
  • Enhanced test reliability and ensured comprehensive validation.

Issues Resolved

List any issues this PR will resolve, e.g. Closes [...].

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Signed-off-by: Kishore Kumaar Natarajan <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/

import { retrieveQueryById } from './QueryUtils'; // Update with the correct path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this?

// Update with the correct path

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

Copy link
Member

@ansjcy ansjcy Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see it removed, please don't resolve my comments - the reviewer should resolve the comments once they verifies

common/utils/QueryUtils.test.ts Outdated Show resolved Hide resolved
common/utils/QueryUtils.test.ts Outdated Show resolved Hide resolved
common/utils/QueryUtils.test.ts Outdated Show resolved Hide resolved
public/application.test.tsx Outdated Show resolved Hide resolved
expect(addNavLinksMock).toHaveBeenCalled();
});

it('should return empty start and stop methods', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this test needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verifies no unnecessary logic in start and stop which prevents future bugs

Kishore Kumaar Natarajan added 2 commits February 11, 2025 17:32
Signed-off-by: Kishore Kumaar Natarajan <[email protected]>
Signed-off-by: Kishore Kumaar Natarajan <[email protected]>
@KishoreKicha14 KishoreKicha14 force-pushed the unit-test-Applications-tsx branch from d6b4c09 to df9e021 Compare February 12, 2025 01:33
* SPDX-License-Identifier: Apache-2.0
*/

import { retrieveQueryById } from './QueryUtils'; // Update with the correct path
Copy link
Member

@ansjcy ansjcy Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see it removed, please don't resolve my comments - the reviewer should resolve the comments once they verifies

if (endpoint.includes('latency')) {
return Promise.resolve({
response: {
top_queries: [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just use the mockQueries in the utils? instead of creating it here.

paramsMock.element
);

expect(typeof unmount).toBe('function');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any value of thisexpect here, especially when we have the tests on line 48.

const mockCore = { http: { get: mockHttpGet } };

describe('retrieveQueryById', () => {
const dataSourceId = 'test-ds';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add comments here on what those values represents?
Also. I believe those will be used across different tests. Can you move them into a shared util?

const mockHttpGet = jest.fn();
const mockCore = { http: { get: mockHttpGet } };

describe('retrieveQueryById', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please improve this test description (retrieveQueryById ). even I'm confused on what this test is about by looking at the description.

},
});
}
return Promise.resolve({ response: { top_queries: [] } });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you assert that the mockHttpGet is called with the exact expected endpoint url?

query: JSON.stringify({ match: { user_action: 'login_attempt' } }),
},
],
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to test how many times / type of requests (GET? POST?) the endpoint is called

});

it('should return null if no top queries are found', async () => {
mockHttpGet.mockResolvedValue({ response: { top_queries: [] } });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above: Could you assert that the mockHttpGet is called with the exact expected endpoint url?

},
},
getStartServices: jest.fn().mockResolvedValue([coreStartMock, {}]),
} as unknown) as jest.Mocked<CoreSetup>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not cast to unknown, maybe reuse the coreMock in OSD

getStartServices: jest.fn().mockResolvedValue([coreStartMock, {}]),
} as unknown) as jest.Mocked<CoreSetup>;

coreStartMock = {} as jest.Mocked<CoreStart>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same - reuse the coreMock in OSD

expect(registerMock).toHaveBeenCalledWith(
expect.objectContaining({
id: PLUGIN_NAME,
title: 'Query Insights',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There a several other fields being registered, could you include them all?


await mountFunction(paramsMock);

expect(renderApp).toHaveBeenCalled();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no validation check that renderApp receives valid arguments.


it('should add the navigation link to nav group', () => {
plugin.setup(coreSetupMock, {} as any);
expect(addNavLinksMock).toHaveBeenCalled();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add property checks here as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants