diff --git a/packages/ra-core/src/controller/useListParams.spec.ts b/packages/ra-core/src/controller/useListParams.spec.tsx
similarity index 51%
rename from packages/ra-core/src/controller/useListParams.spec.ts
rename to packages/ra-core/src/controller/useListParams.spec.tsx
index e878d49b665..31e32b31b24 100644
--- a/packages/ra-core/src/controller/useListParams.spec.ts
+++ b/packages/ra-core/src/controller/useListParams.spec.tsx
@@ -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 ;
+ };
+ const history = {
+ listen: jest.fn(),
+ push: jest.fn(),
+ location: { pathname: '', search: '' },
+ } as any;
+ render(
+
+
+
+
+
+ );
+ 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 ;
+ };
+ const history = {
+ listen: jest.fn(),
+ push: jest.fn(),
+ location: { pathname: '', search: '' },
+ } as any;
+ render(
+
+
+
+
+
+ );
+ 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({
+ resource: 'foo',
+ location: {} as any,
+ });
+ showFilter('foo.bar');
+ return ;
+ };
+ const history = {
+ listen: jest.fn(),
+ push: jest.fn(),
+ location: { pathname: '', search: '' },
+ } as any;
+ render(
+
+
+
+
+
+ );
+ 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({
+ resource: 'foo',
+ location: {} as any,
+ });
+ showFilter('foo.bar', 'baz');
+ return ;
+ };
+ const history = {
+ listen: jest.fn(),
+ push: jest.fn(),
+ location: { pathname: '', search: '' },
+ } as any;
+ render(
+
+
+
+
+
+ );
+ 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,
+ }),
+ });
+ });
+ });
});
diff --git a/packages/ra-core/src/controller/useListParams.ts b/packages/ra-core/src/controller/useListParams.ts
index 48a18febd68..0b961163dfe 100644
--- a/packages/ra-core/src/controller/useListParams.ts
+++ b/packages/ra-core/src/controller/useListParams.ts
@@ -246,7 +246,9 @@ const useListParams = ({
...displayedFilterValues,
[filterName]: true,
};
- const filter = set(filterValues, filterName, defaultValue);
+ const filter = defaultValue
+ ? set(filterValues, filterName, defaultValue)
+ : filterValues;
changeParams({
type: SET_FILTER,
payload: {