-
Notifications
You must be signed in to change notification settings - Fork 46
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
fix(SystemsPage): RHINENG-5977 - Fix URL params filters handling #2129
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,34 @@ | ||||||
import { useCallback } from 'react'; | ||||||
import { useSearchParams } from 'react-router-dom'; | ||||||
|
||||||
// TODO DRY:similar to constructParameters | ||||||
export function constructURLParameters(urlParams, allowedParams) { | ||||||
if (urlParams) { | ||||||
const params = { ...urlParams }; | ||||||
Object.keys(urlParams).forEach( | ||||||
key => ( | ||||||
params[key] === undefined | ||||||
|| params[key] === '' | ||||||
|| !allowedParams.includes(key) | ||||||
|| params[key] === false | ||||||
) | ||||||
&& delete params[key] | ||||||
|
||||||
); | ||||||
return params; | ||||||
} | ||||||
} | ||||||
|
||||||
export const useUrlParams = (allowedParams) => { | ||||||
const [searchParams, setSearchParams] = useSearchParams(); | ||||||
|
||||||
const setUrlParams = useCallback((parameters) => { | ||||||
const para = constructURLParameters(parameters, allowedParams); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NITPICK: It looks like a typo on the first look.
Suggested change
|
||||||
|
||||||
setSearchParams(para); | ||||||
}, [JSON.stringify(allowedParams)]); | ||||||
|
||||||
return [searchParams, setUrlParams]; | ||||||
}; | ||||||
|
||||||
export default useUrlParams; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { renderHook, act } from '@testing-library/react'; | ||
import { createTestWrapper } from '../Utilities/TestWrapper'; | ||
import { MemoryRouter } from 'react-router-dom'; | ||
|
||
import useUrlParams, { constructURLParameters } from './useUrlParams'; | ||
|
||
describe('MiscHelper', () => { | ||
it('useUrlParams', async () => { | ||
const { result } = renderHook(() => useUrlParams(['a', 'b']), { | ||
wrapper: createTestWrapper(MemoryRouter, { initialEntries: ['/'] }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use the TestWrapper which has already MemortRouter provided? |
||
}); | ||
|
||
await act(()=> { | ||
result.current[1]({ a: 'testValue', b: 'testValue', c: 'testValue' }); | ||
}); | ||
|
||
expect(result.current[0].get('a')).toEqual('testValue'); | ||
expect(result.current[0].get('b')).toEqual('testValue'); | ||
expect(result.current[0].get('c')).toEqual(null); | ||
}); | ||
|
||
it.each` | ||
urlParams | expected_data | ||
${undefined} | ${undefined} | ||
${{}} | ${{}} | ||
${{ a: 'testValue', b: undefined }} | ${{ a: 'testValue' }} | ||
${{ a: false, b: 'testValue' }} | ${{ b: 'testValue' }} | ||
${{ a: 'testValue', c: 'testValue' }} | ${{ a: 'testValue' }} | ||
${{ a: '', b: 'testValue' }} | ${{ b: 'testValue' }} | ||
`('constructURLParameters', ({ urlParams, expected_data }) => { // eslint-disable-line camelcase | ||
|
||
const result = constructURLParameters(urlParams, ['a', 'b']); | ||
expect(result).toEqual(expected_data); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,10 @@ TestWrapper.propTypes = { | |
store: propTypes.object | ||
}; | ||
|
||
export const createTestWrapper = (Wrapper = TestWrapper, props) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we use the shared wrapper, this is not needed. |
||
return function CreatedWrapper({ children }) { // eslint-disable-line | ||
return <Wrapper {...props}>{children}</Wrapper>; | ||
}; | ||
}; | ||
|
||
export default TestWrapper; |
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.
Maybe it is time to finally to implement this
TODO
. Can we try to consolidate this function withconstructParameters
and get rid of the duplicate code?