Skip to content

Commit f4900c2

Browse files
committed
infiniteListController: fix
1 parent 2abda85 commit f4900c2

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

packages/ra-core/src/controller/list/useInfiniteListController.spec.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,5 +704,41 @@ describe('useInfiniteListController', () => {
704704
// Only called once by NavigationToFirstResource
705705
expect(authProvider.checkAuth).toHaveBeenCalledTimes(1);
706706
});
707+
708+
it('should not call checkAuth nor canAccess when disableAuthentication is true', async () => {
709+
const authProvider: AuthProvider = {
710+
checkAuth: jest.fn().mockResolvedValue(true),
711+
login: () => Promise.resolve(),
712+
logout: () => Promise.resolve(),
713+
checkError: () => Promise.resolve(),
714+
getPermissions: () => Promise.resolve(),
715+
canAccess: jest.fn().mockResolvedValue(false),
716+
};
717+
render(<DisableAuthentication authProvider={authProvider} />);
718+
await screen.findByText('Post #1 - 90 votes');
719+
expect(authProvider.checkAuth).not.toHaveBeenCalled();
720+
expect(authProvider.canAccess).not.toHaveBeenCalled();
721+
});
722+
723+
it('should not call checkAuth nor canAccess when disableAuthentication is true even if useAuthState was called before', async () => {
724+
const authProvider: AuthProvider = {
725+
checkAuth: jest.fn().mockResolvedValue(true),
726+
login: () => Promise.resolve(),
727+
logout: () => Promise.resolve(),
728+
checkError: () => Promise.resolve(),
729+
getPermissions: () => Promise.resolve(),
730+
canAccess: jest.fn().mockResolvedValue(false),
731+
};
732+
render(<DisableAuthentication authProvider={authProvider} />);
733+
await screen.findByText('Post #1 - 90 votes');
734+
fireEvent.click(await screen.findByText('Dashboard'));
735+
await screen.findByText('Dashboard view');
736+
fireEvent.click(await screen.findByText('List'));
737+
await screen.findByText('Post #1 - 90 votes');
738+
// checkAuth is called twice: once by RA (with different params)
739+
// and once by our custom Dashboard component
740+
expect(authProvider.checkAuth).toHaveBeenCalledTimes(2);
741+
expect(authProvider.canAccess).not.toHaveBeenCalled();
742+
});
707743
});
708744
});

packages/ra-core/src/controller/list/useInfiniteListController.stories.tsx

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from './useInfiniteListController';
1111
import { Browser } from '../../storybook/FakeBrowser';
1212
import { TestMemoryRouter } from '../../routing';
13+
import { useAuthState } from '../..';
1314

1415
export default {
1516
title: 'ra-core/controller/list/useInfiniteListController',
@@ -58,6 +59,17 @@ const List = params => {
5859
</ul>
5960
</div>
6061
)}
62+
<Link to="/">Dashboard</Link>
63+
</div>
64+
);
65+
};
66+
67+
const Dashboard = () => {
68+
useAuthState();
69+
return (
70+
<div style={styles.mainContainer}>
71+
<div>Dashboard view</div>
72+
<Link to="/posts">List</Link>
6173
</div>
6274
);
6375
};
@@ -177,7 +189,7 @@ export const Authenticated = ({
177189
dataProvider?: DataProvider;
178190
}) => {
179191
return (
180-
<TestMemoryRouter>
192+
<TestMemoryRouter initialEntries={['/posts']}>
181193
<CoreAdminContext
182194
dataProvider={dataProvider}
183195
authProvider={authProvider}
@@ -198,12 +210,13 @@ export const DisableAuthentication = ({
198210
dataProvider?: DataProvider;
199211
}) => {
200212
return (
201-
<TestMemoryRouter>
213+
<TestMemoryRouter initialEntries={['/posts']}>
202214
<CoreAdminContext
203215
dataProvider={dataProvider}
204216
authProvider={authProvider}
217+
dashboard={Dashboard}
205218
>
206-
<CoreAdminUI>
219+
<CoreAdminUI dashboard={Dashboard} accessDenied={AccessDenied}>
207220
<Resource
208221
name="posts"
209222
list={<Posts disableAuthentication />}
@@ -213,6 +226,22 @@ export const DisableAuthentication = ({
213226
</TestMemoryRouter>
214227
);
215228
};
229+
DisableAuthentication.args = {
230+
authProvider: undefined,
231+
};
232+
DisableAuthentication.argTypes = {
233+
authProvider: {
234+
options: ['default', 'canAccess'],
235+
mapping: {
236+
default: undefined,
237+
canAccess: {
238+
...defaultAuthProvider,
239+
canAccess: () => Promise.resolve(false),
240+
},
241+
},
242+
control: { type: 'inline-radio' },
243+
},
244+
};
216245

217246
export const CanAccess = ({
218247
authProviderDelay = 300,

packages/ra-core/src/controller/list/useInfiniteListController.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ export const useInfiniteListController = <
7979
const { isPending: isPendingCanAccess } = useRequireAccess<RecordType>({
8080
action: 'list',
8181
resource,
82-
// If disableAuthentication is true then isPendingAuthenticated will always be true so this hook is disabled
83-
enabled: !isPendingAuthenticated,
82+
enabled: !disableAuthentication && !isPendingAuthenticated,
8483
});
8584

8685
const translate = useTranslate();

0 commit comments

Comments
 (0)