From 0d49181a83574fa4056e14829ab7a66d0804a6dd Mon Sep 17 00:00:00 2001
From: Gildas Garcia <1122076+djhi@users.noreply.github.com>
Date: Tue, 7 Jun 2022 11:33:02 +0200
Subject: [PATCH] Fix edit and show controllers do not handle falsy identifiers
---
.../edit/useEditController.spec.tsx | 38 ++++++++++++++++++
.../src/controller/edit/useEditController.ts | 2 +-
.../show/useShowController.spec.tsx | 39 +++++++++++++++++++
.../src/controller/show/useShowController.ts | 2 +-
4 files changed, 79 insertions(+), 2 deletions(-)
diff --git a/packages/ra-core/src/controller/edit/useEditController.spec.tsx b/packages/ra-core/src/controller/edit/useEditController.spec.tsx
index ba2e84bf6c3..e8c7781d5ff 100644
--- a/packages/ra-core/src/controller/edit/useEditController.spec.tsx
+++ b/packages/ra-core/src/controller/edit/useEditController.spec.tsx
@@ -69,6 +69,44 @@ describe('useEditController', () => {
await waitFor(() => {
expect(getOne).toHaveBeenCalledWith('posts', { id: 'test?' });
});
+ await waitFor(() => {
+ expect(screen.queryAllByText('hello')).toHaveLength(1);
+ });
+ });
+
+ it('should use the id provided through props if any', async () => {
+ const getOne = jest
+ .fn()
+ .mockImplementationOnce(() =>
+ Promise.resolve({ data: { id: 0, title: 'hello' } })
+ );
+ const dataProvider = ({ getOne } as unknown) as DataProvider;
+ const history = createMemoryHistory({
+ initialEntries: ['/posts/test%3F'],
+ });
+
+ render(
+
+
+
+ {({ record }) => (
+ {record && record.title}
+ )}
+
+ }
+ />
+
+
+ );
+ await waitFor(() => {
+ expect(getOne).toHaveBeenCalledWith('posts', { id: 0 });
+ });
+ await waitFor(() => {
+ expect(screen.queryAllByText('hello')).toHaveLength(1);
+ });
});
it('should accept custom client query options', async () => {
diff --git a/packages/ra-core/src/controller/edit/useEditController.ts b/packages/ra-core/src/controller/edit/useEditController.ts
index 014063930cc..4784256bbe7 100644
--- a/packages/ra-core/src/controller/edit/useEditController.ts
+++ b/packages/ra-core/src/controller/edit/useEditController.ts
@@ -61,7 +61,7 @@ export const useEditController = <
const redirect = useRedirect();
const refresh = useRefresh();
const { id: routeId } = useParams<'id'>();
- const id = propsId || decodeURIComponent(routeId);
+ const id = propsId != null ? propsId : decodeURIComponent(routeId);
const { onSuccess, onError, ...otherMutationOptions } = mutationOptions;
const {
registerMutationMiddleware,
diff --git a/packages/ra-core/src/controller/show/useShowController.spec.tsx b/packages/ra-core/src/controller/show/useShowController.spec.tsx
index c690f12e291..df380fd5b4e 100644
--- a/packages/ra-core/src/controller/show/useShowController.spec.tsx
+++ b/packages/ra-core/src/controller/show/useShowController.spec.tsx
@@ -66,6 +66,45 @@ describe('useShowController', () => {
await waitFor(() => {
expect(getOne).toHaveBeenCalledWith('posts', { id: 'test?' });
});
+ await waitFor(() => {
+ expect(screen.queryAllByText('hello')).toHaveLength(1);
+ });
+ });
+
+ it('should use the id provided through props if any', async () => {
+ const getOne = jest
+ .fn()
+ .mockImplementationOnce(() =>
+ Promise.resolve({ data: { id: 0, title: 'hello' } })
+ );
+ const dataProvider = ({ getOne } as unknown) as DataProvider;
+ render(
+
+
+
+ {({ record }) => (
+ {record && record.title}
+ )}
+
+ }
+ />
+
+
+ );
+ await waitFor(() => {
+ expect(getOne).toHaveBeenCalledWith('posts', { id: 0 });
+ });
+ await waitFor(() => {
+ expect(screen.queryAllByText('hello')).toHaveLength(1);
+ });
});
it('should accept custom client query options', async () => {
diff --git a/packages/ra-core/src/controller/show/useShowController.ts b/packages/ra-core/src/controller/show/useShowController.ts
index 51df73ec75a..b22dbfa134c 100644
--- a/packages/ra-core/src/controller/show/useShowController.ts
+++ b/packages/ra-core/src/controller/show/useShowController.ts
@@ -54,7 +54,7 @@ export const useShowController = (
const redirect = useRedirect();
const refresh = useRefresh();
const { id: routeId } = useParams<'id'>();
- const id = propsId || decodeURIComponent(routeId);
+ const id = propsId != null ? propsId : decodeURIComponent(routeId);
const { data: record, error, isLoading, isFetching, refetch } = useGetOne<
RecordType