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 Case for clusterUtils #2349

Merged
merged 1 commit into from
Jan 23, 2024

Conversation

uidoyen
Copy link
Contributor

@uidoyen uidoyen commented Jan 17, 2024

Closes: RHOAIENG-1897

Description

Unit Test Case for clusterUtils

How Has This Been Tested?

npm run test

Test Impact

Screenshot 2024-01-17 at 7 12 16 PM

Request review criteria:

Self checklist (all need to be checked):

  • The developer has manually tested the changes and verified that the changes work
  • Commits have been squashed into descriptive, self-contained units of work (e.g. 'WIP' and 'Implements feedback' style messages have been removed)
  • Testing instructions have been added in the PR body (for PRs involving changes that are not immediately obvious).
  • The developer has added tests or explained why testing cannot be added (unit tests & storybook for related changes)

If you have UI changes:

  • Included any necessary screenshots or gifs if it was a UI change.
  • Included tags to the UX team if it was a UI/UX change (find relevant UX in the SMEs section).

After the PR is posted & before it merges:

  • The developer has tested their solution on a cluster by using the image produced by the PR to main

@uidoyen
Copy link
Contributor Author

uidoyen commented Jan 23, 2024

@christianvogt updated accordingly, Can you plz look into this again?

frontend/src/utilities/__tests__/clusterUtils.spec.ts Outdated Show resolved Hide resolved
Comment on lines 30 to 35
afterAll(() => {
Object.defineProperty(window, 'location', {
writable: true,
value: originalLocation,
});
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Use afterEach instead or simply perform this in each describe block.
Also, can we simplify the assignment as well?

Suggested change
afterAll(() => {
Object.defineProperty(window, 'location', {
writable: true,
value: originalLocation,
});
});
afterEach(() => {
window.location = originalLocation
});

Comment on lines 26 to 28
beforeAll(() => {
originalLocation = { ...window.location };
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Unnecessary. Capture the original location once at the top.

Comment on lines 19 to 22
jest.mock('~/redux/selectors/clusterInfo', () => ({
...jest.requireActual('~/redux/selectors/clusterInfo'), // Use the actual module for other exports
useClusterInfo: jest.fn(),
}));
Copy link
Contributor

Choose a reason for hiding this comment

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

Use this method of mocking or spyOn but not both. Prefer the use of this mocking and only using spyOn for special circumstances.

Comment on lines 38 to 40
beforeEach(() => {
setupWindowLocation('localhost', 'https:', '443');
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove and call setupWindowLocation per test so that the values used by the test is clear to anyone reading the test.

Comment on lines 42 to 48
afterEach(() => {
// Restore window.location to its original state
Object.defineProperty(window, 'location', {
writable: true,
value: originalLocation,
});
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Unnecessary if you do it at the top level. See previous comment related to afterAll.

const serverURL = 'https://api.example.com';

// Manually mock the useClusterInfo function
const mockUseClusterInfo = jest.spyOn(clusterInfo, 'useClusterInfo');
Copy link
Contributor

Choose a reason for hiding this comment

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

Using multiple methods of mocking when one will do. No need to use spyOn here.


// Assert
expect(mockUseClusterInfo).toHaveBeenCalledTimes(1);
expect(result.current).toBe('https://console-openshift-console.apps.example.com');
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
expect(result.current).toBe('https://console-openshift-console.apps.example.com');
expect(result).hookToBe('https://console-openshift-console.apps.example.com');

const devModeMock = jest.spyOn(jest.requireMock('~/utilities/const'), 'DEV_MODE', 'get');

describe('getOpenShiftConsoleServerURL', () => {
setupWindowLocation('localhost', 'https:', '443');
Copy link
Contributor

Choose a reason for hiding this comment

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

This setup is only valid until the first test case changes the value.
Remove and make the call within the specific test case with values needed for that test case.

});

describe('useOpenShiftURL', () => {
setupWindowLocation('localhost', 'https:', '443');
Copy link
Contributor

Choose a reason for hiding this comment

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

Move into the first test case.

@uidoyen uidoyen force-pushed the RHOAIENG-1897 branch 2 times, most recently from 8be0330 to 64d2066 Compare January 23, 2024 20:54
@christianvogt
Copy link
Contributor

/lgtm
/approve

Copy link
Contributor

openshift-ci bot commented Jan 23, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: christianvogt

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-merge-bot openshift-merge-bot bot merged commit b715cdd into opendatahub-io:main Jan 23, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants