-
Notifications
You must be signed in to change notification settings - Fork 167
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
Unit Test Case for clusterUtils #2349
Conversation
56e6417
to
69434d4
Compare
69434d4
to
fc16e25
Compare
fc16e25
to
05b1ce4
Compare
@christianvogt updated accordingly, Can you plz look into this again? |
05b1ce4
to
32bef4f
Compare
afterAll(() => { | ||
Object.defineProperty(window, 'location', { | ||
writable: true, | ||
value: originalLocation, | ||
}); | ||
}); |
There was a problem hiding this comment.
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?
afterAll(() => { | |
Object.defineProperty(window, 'location', { | |
writable: true, | |
value: originalLocation, | |
}); | |
}); | |
afterEach(() => { | |
window.location = originalLocation | |
}); |
beforeAll(() => { | ||
originalLocation = { ...window.location }; | ||
}); |
There was a problem hiding this comment.
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.
jest.mock('~/redux/selectors/clusterInfo', () => ({ | ||
...jest.requireActual('~/redux/selectors/clusterInfo'), // Use the actual module for other exports | ||
useClusterInfo: jest.fn(), | ||
})); |
There was a problem hiding this comment.
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.
beforeEach(() => { | ||
setupWindowLocation('localhost', 'https:', '443'); | ||
}); |
There was a problem hiding this comment.
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.
afterEach(() => { | ||
// Restore window.location to its original state | ||
Object.defineProperty(window, 'location', { | ||
writable: true, | ||
value: originalLocation, | ||
}); | ||
}); |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expect(result.current).toBe('https://console-openshift-console.apps.example.com'); | |
expect(result).hookToBe('https://console-openshift-console.apps.example.com'); |
32bef4f
to
a670d6b
Compare
const devModeMock = jest.spyOn(jest.requireMock('~/utilities/const'), 'DEV_MODE', 'get'); | ||
|
||
describe('getOpenShiftConsoleServerURL', () => { | ||
setupWindowLocation('localhost', 'https:', '443'); |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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.
8be0330
to
64d2066
Compare
4a7b06a
to
9287948
Compare
/lgtm |
[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 |
Closes: RHOAIENG-1897
Description
Unit Test Case for clusterUtils
How Has This Been Tested?
npm run test
Test Impact
Request review criteria:
Self checklist (all need to be checked):
If you have UI changes:
After the PR is posted & before it merges:
main