-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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 Filter fails to show compound filters with no default value #5657
Changes from 1 commit
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 | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,11 @@ | ||||||
import { getQuery, getNumberOrDefault } from './useListParams'; | ||||||
import * as React from 'react'; | ||||||
import expect from 'expect'; | ||||||
import { render } from '@testing-library/react'; | ||||||
import { Router } from 'react-router-dom'; | ||||||
import { stringify } from 'query-string'; | ||||||
|
||||||
import TestContext from '../util/TestContext'; | ||||||
import useListParams, { getQuery, getNumberOrDefault } from './useListParams'; | ||||||
import { | ||||||
SORT_DESC, | ||||||
SORT_ASC, | ||||||
|
@@ -182,4 +189,144 @@ describe('useListParams', () => { | |||||
expect(result).toEqual(0); | ||||||
}); | ||||||
}); | ||||||
describe('showFilter', () => { | ||||||
it('should initialize displayed filters', () => { | ||||||
const TestedComponent = () => { | ||||||
const [_, { showFilter }] = useListParams({ | ||||||
resource: 'foo', | ||||||
location: {} as any, | ||||||
}); | ||||||
showFilter('foo'); | ||||||
return <span />; | ||||||
}; | ||||||
const history = { | ||||||
listen: jest.fn(), | ||||||
push: jest.fn(), | ||||||
location: { pathname: '', search: '' }, | ||||||
} as any; | ||||||
render( | ||||||
<Router history={history}> | ||||||
<TestContext history={history}> | ||||||
<TestedComponent /> | ||||||
</TestContext> | ||||||
</Router> | ||||||
); | ||||||
expect(history.push).toBeCalledWith({ | ||||||
search: | ||||||
'?' + | ||||||
stringify({ | ||||||
displayedFilters: JSON.stringify({ foo: true }), | ||||||
filter: '{}', | ||||||
sort: 'id', | ||||||
order: 'ASC', | ||||||
page: 1, | ||||||
perPage: 10, | ||||||
}), | ||||||
}); | ||||||
}); | ||||||
it('should initialize filters', () => { | ||||||
const TestedComponent = () => { | ||||||
const [_, { showFilter }] = useListParams({ | ||||||
resource: 'foo', | ||||||
location: {} as any, | ||||||
}); | ||||||
showFilter('foo', 'bar'); | ||||||
return <span />; | ||||||
}; | ||||||
const history = { | ||||||
listen: jest.fn(), | ||||||
push: jest.fn(), | ||||||
location: { pathname: '', search: '' }, | ||||||
} as any; | ||||||
render( | ||||||
<Router history={history}> | ||||||
<TestContext history={history}> | ||||||
<TestedComponent /> | ||||||
</TestContext> | ||||||
</Router> | ||||||
); | ||||||
expect(history.push).toBeCalledWith({ | ||||||
search: | ||||||
'?' + | ||||||
stringify({ | ||||||
displayedFilters: JSON.stringify({ foo: true }), | ||||||
filter: JSON.stringify({ foo: 'bar' }), | ||||||
sort: 'id', | ||||||
order: 'ASC', | ||||||
page: 1, | ||||||
perPage: 10, | ||||||
}), | ||||||
}); | ||||||
}); | ||||||
|
||||||
it('should initialize displayed filters on compound filters', () => { | ||||||
const TestedComponent = () => { | ||||||
const [_, { showFilter }] = useListParams({ | ||||||
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. Same
Suggested change
|
||||||
resource: 'foo', | ||||||
location: {} as any, | ||||||
}); | ||||||
showFilter('foo.bar'); | ||||||
return <span />; | ||||||
}; | ||||||
const history = { | ||||||
listen: jest.fn(), | ||||||
push: jest.fn(), | ||||||
location: { pathname: '', search: '' }, | ||||||
} as any; | ||||||
render( | ||||||
<Router history={history}> | ||||||
<TestContext history={history}> | ||||||
<TestedComponent /> | ||||||
</TestContext> | ||||||
</Router> | ||||||
); | ||||||
expect(history.push).toBeCalledWith({ | ||||||
search: | ||||||
'?' + | ||||||
stringify({ | ||||||
displayedFilters: JSON.stringify({ 'foo.bar': true }), | ||||||
filter: '{}', | ||||||
sort: 'id', | ||||||
order: 'ASC', | ||||||
page: 1, | ||||||
perPage: 10, | ||||||
}), | ||||||
}); | ||||||
}); | ||||||
|
||||||
it('should initialize filters on compound filters', () => { | ||||||
const TestedComponent = () => { | ||||||
const [_, { showFilter }] = useListParams({ | ||||||
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. Same
Suggested change
|
||||||
resource: 'foo', | ||||||
location: {} as any, | ||||||
}); | ||||||
showFilter('foo.bar', 'baz'); | ||||||
return <span />; | ||||||
}; | ||||||
const history = { | ||||||
listen: jest.fn(), | ||||||
push: jest.fn(), | ||||||
location: { pathname: '', search: '' }, | ||||||
} as any; | ||||||
render( | ||||||
<Router history={history}> | ||||||
<TestContext history={history}> | ||||||
<TestedComponent /> | ||||||
</TestContext> | ||||||
</Router> | ||||||
); | ||||||
expect(history.push).toBeCalledWith({ | ||||||
search: | ||||||
'?' + | ||||||
stringify({ | ||||||
displayedFilters: JSON.stringify({ 'foo.bar': true }), | ||||||
filter: JSON.stringify({ foo: { bar: 'baz' } }), | ||||||
sort: 'id', | ||||||
order: 'ASC', | ||||||
page: 1, | ||||||
perPage: 10, | ||||||
}), | ||||||
}); | ||||||
}); | ||||||
}); | ||||||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -246,7 +246,9 @@ const useListParams = ({ | |
...displayedFilterValues, | ||
[filterName]: true, | ||
}; | ||
const filter = set(filterValues, filterName, defaultValue); | ||
const filter = defaultValue | ||
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. The problem was with compound filters (e.f. 'comment.title') with no default value. set returned |
||
? set(filterValues, filterName, defaultValue) | ||
: filterValues; | ||
changeParams({ | ||
type: SET_FILTER, | ||
payload: { | ||
|
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.
You don't need the
_
, just leave the slot it empty