Skip to content

Commit 6778fd1

Browse files
committed
Fix tests and stories
1 parent 9f418bf commit 6778fd1

File tree

6 files changed

+48
-39
lines changed

6 files changed

+48
-39
lines changed

packages/ra-core/src/controller/useReference.spec.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,19 @@ describe('useReference', () => {
130130
referenceRecord: undefined,
131131
isFetching: true,
132132
isLoading: true,
133+
isPaused: false,
133134
isPending: true,
135+
isPlaceholderData: false,
134136
error: null,
135137
refetch: expect.any(Function),
136138
});
137139
expect(hookValue.mock.calls[1][0]).toEqual({
138140
referenceRecord: { id: 1, title: 'foo' },
139141
isFetching: false,
140142
isLoading: false,
143+
isPaused: false,
141144
isPending: false,
145+
isPlaceholderData: false,
142146
error: null,
143147
refetch: expect.any(Function),
144148
});
@@ -170,15 +174,19 @@ describe('useReference', () => {
170174
referenceRecord: { id: 1, title: 'foo' },
171175
isFetching: true,
172176
isLoading: false,
177+
isPaused: false,
173178
isPending: false,
179+
isPlaceholderData: false,
174180
error: null,
175181
refetch: expect.any(Function),
176182
});
177183
expect(hookValue.mock.calls[1][0]).toEqual({
178184
referenceRecord: { id: 1, title: 'foo' },
179185
isFetching: false,
180186
isLoading: false,
187+
isPaused: false,
181188
isPending: false,
189+
isPlaceholderData: false,
182190
error: null,
183191
refetch: expect.any(Function),
184192
});

packages/ra-ui-materialui/src/field/ReferenceField.spec.tsx

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
SXLink,
2626
SXNoLink,
2727
SlowAccessControl,
28+
ErrorWhileFetching,
2829
} from './ReferenceField.stories';
2930
import { TextField } from './TextField';
3031

@@ -459,29 +460,8 @@ describe('<ReferenceField />', () => {
459460

460461
it('should display an error icon if the dataProvider call fails', async () => {
461462
jest.spyOn(console, 'error').mockImplementation(() => {});
462-
const dataProvider = testDataProvider({
463-
getMany: jest.fn().mockRejectedValue(new Error('boo')),
464-
});
465-
render(
466-
<ThemeProvider theme={theme}>
467-
<CoreAdminContext dataProvider={dataProvider}>
468-
<ReferenceField
469-
record={record}
470-
resource="comments"
471-
source="postId"
472-
reference="posts"
473-
>
474-
<TextField source="title" />
475-
</ReferenceField>
476-
</CoreAdminContext>
477-
</ThemeProvider>
478-
);
479-
await new Promise(resolve => setTimeout(resolve, 10));
480-
const ErrorIcon = screen.queryByRole('presentation', {
481-
hidden: true,
482-
});
483-
expect(ErrorIcon).not.toBeNull();
484-
await screen.findByText('boo');
463+
render(<ErrorWhileFetching />);
464+
await screen.findByText('ra.notification.http_error');
485465
});
486466

487467
describe('link', () => {

packages/ra-ui-materialui/src/field/ReferenceField.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import ErrorIcon from '@mui/icons-material/Error';
1212
import {
1313
type LinkToType,
1414
useGetRecordRepresentation,
15-
useTranslate,
1615
type RaRecord,
1716
ReferenceFieldBase,
1817
useReferenceFieldContext,
1918
useFieldValue,
19+
Translate,
2020
} from 'ra-core';
2121
import type { UseQueryOptions } from '@tanstack/react-query';
2222
import clsx from 'clsx';
@@ -68,13 +68,12 @@ export const ReferenceField = <
6868
name: PREFIX,
6969
});
7070
const { emptyText } = props;
71-
const translate = useTranslate();
7271
const id = useFieldValue(props);
7372

7473
if (id == null) {
7574
return emptyText ? (
7675
<Typography component="span" variant="body2">
77-
{emptyText && translate(emptyText, { _: emptyText })}
76+
<Translate i18nKey={emptyText}>{emptyText}</Translate>
7877
</Typography>
7978
) : null;
8079
}
@@ -124,7 +123,6 @@ export const ReferenceFieldView = <
124123
useReferenceFieldContext();
125124

126125
const getRecordRepresentation = useGetRecordRepresentation(reference);
127-
const translate = useTranslate();
128126

129127
if (error) {
130128
return (
@@ -135,9 +133,9 @@ export const ReferenceFieldView = <
135133
variant="body2"
136134
sx={{ color: 'error.main' }}
137135
>
138-
{translate('ra.notification.http_error', {
139-
_: 'Server communication error',
140-
})}
136+
<Translate i18nKey="ra.notification.http_error">
137+
Server communication error
138+
</Translate>
141139
</Typography>
142140
</Stack>
143141
);
@@ -163,7 +161,13 @@ export const ReferenceFieldView = <
163161
variant="body2"
164162
sx={{ color: 'error.main' }}
165163
>
166-
{offline && translate(offline, { _: offline })}
164+
{typeof offline === 'string' ? (
165+
<Translate i18nKey={offline}>
166+
{offline}
167+
</Translate>
168+
) : (
169+
offline
170+
)}
167171
</Typography>
168172
</Stack>
169173
);
@@ -173,7 +177,7 @@ export const ReferenceFieldView = <
173177
}
174178
return emptyText ? (
175179
<Typography component="span" variant="body2">
176-
{emptyText && translate(emptyText, { _: emptyText })}
180+
<Translate i18nKey={emptyText}>{emptyText}</Translate>
177181
</Typography>
178182
) : null;
179183
}

packages/ra-ui-materialui/src/field/ReferenceManyCount.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('<ReferenceManyCount />', () => {
1717
it('should render an error icon when the request fails', async () => {
1818
jest.spyOn(console, 'error').mockImplementation(() => {});
1919
render(<ErrorState />);
20-
await screen.findByTitle('error');
20+
await screen.findByText('Server communication error');
2121
});
2222
it('should accept a filter prop', async () => {
2323
render(<WithFilter />);

packages/ra-ui-materialui/src/field/ReferenceManyCount.tsx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ import {
66
useCreatePath,
77
SortPayload,
88
RaRecord,
9+
Translate,
910
} from 'ra-core';
10-
import { Typography, TypographyProps, CircularProgress } from '@mui/material';
11+
import {
12+
Typography,
13+
TypographyProps,
14+
CircularProgress,
15+
Stack,
16+
} from '@mui/material';
1117
import ErrorIcon from '@mui/icons-material/Error';
1218

1319
import { FieldProps } from './types';
@@ -78,11 +84,18 @@ export const ReferenceManyCount = <RecordType extends RaRecord = RaRecord>(
7884

7985
if (error) {
8086
body = (
81-
<ErrorIcon
82-
color="error"
83-
fontSize="small"
84-
titleAccess={error.message}
85-
/>
87+
<Stack direction="row" alignItems="center" gap={1}>
88+
<ErrorIcon role="presentation" color="error" fontSize="small" />
89+
<Typography
90+
component="span"
91+
variant="body2"
92+
sx={{ color: 'error.main' }}
93+
>
94+
<Translate i18nKey="ra.notification.http_error">
95+
Server communication error
96+
</Translate>
97+
</Typography>
98+
</Stack>
8699
);
87100
}
88101

packages/ra-ui-materialui/src/input/InPlaceEditor/InPlaceEditor.spec.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { render, screen, fireEvent } from '@testing-library/react';
44
import { Basic } from './InPlaceEditor.stories';
55

66
describe('InPlaceEditor', () => {
7+
beforeEach(() => {
8+
jest.clearAllMocks();
9+
});
710
it('should render the field value on mount', async () => {
811
render(<Basic delay={0} />);
912
await screen.findByText('John Doe');
@@ -24,6 +27,7 @@ describe('InPlaceEditor', () => {
2427
await screen.findByText('Jane Doe');
2528
});
2629
it('should revert to the previous version on error', async () => {
30+
jest.spyOn(console, 'error').mockImplementation(() => {});
2731
render(<Basic delay={0} updateFails />);
2832
const value = await screen.findByText('John Doe');
2933
value.click();

0 commit comments

Comments
 (0)