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

Fix Filter fails to show compound filters with no default value #5657

Merged
merged 2 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Expand Down Expand Up @@ -182,4 +189,144 @@ describe('useListParams', () => {
expect(result).toEqual(0);
});
});
describe('showFilter', () => {
it('should initialize displayed filters', () => {
const TestedComponent = () => {
const [_, { showFilter }] = useListParams({
Copy link
Collaborator

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

Suggested change
const [_, { showFilter }] = useListParams({
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({
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same

Suggested change
const [_, { showFilter }] = useListParams({
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.bar': true }),
filter: '{}',
sort: 'id',
order: 'ASC',
page: 1,
perPage: 10,
}),
});
});

it('should initialize filters on compound filters', () => {
const TestedComponent = () => {
const [_, { showFilter }] = useListParams({
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same

Suggested change
const [_, { showFilter }] = useListParams({
const [, { showFilter }] = useListParams({

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,
}),
});
});
});
});
4 changes: 3 additions & 1 deletion packages/ra-core/src/controller/useListParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ const useListParams = ({
...displayedFilterValues,
[filterName]: true,
};
const filter = set(filterValues, filterName, defaultValue);
const filter = defaultValue
Copy link
Member Author

Choose a reason for hiding this comment

The 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 {comment: {}}, which is wrong in that case.

? set(filterValues, filterName, defaultValue)
: filterValues;
changeParams({
type: SET_FILTER,
payload: {
Expand Down