Skip to content

Commit 7292f76

Browse files
committed
Add tests
1 parent 490fd39 commit 7292f76

File tree

3 files changed

+71
-34
lines changed

3 files changed

+71
-34
lines changed

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

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import {
2020
LinkDefaultShowView,
2121
LinkMissingView,
2222
LinkFalse,
23-
MissingReferenceIdEmptyTextTranslation,
2423
MissingReferenceEmptyText,
24+
MissingReferenceIdEmptyTextTranslation,
25+
MissingReferenceIdEmpty,
26+
MissingReferenceIdEmptyTranslation,
2527
SXLink,
2628
SXNoLink,
2729
SlowAccessControl,
@@ -293,29 +295,48 @@ describe('<ReferenceField />', () => {
293295
});
294296
});
295297

296-
it('should display the emptyText if the field is empty', () => {
297-
render(
298-
<ThemeProvider theme={theme}>
299-
<CoreAdminContext dataProvider={testDataProvider()}>
300-
<ReferenceField
301-
record={{ id: 123 }}
302-
resource="comments"
303-
// @ts-expect-error source prop does not have a valid value
304-
source="postId"
305-
reference="posts"
306-
emptyText="EMPTY"
307-
>
308-
<TextField source="title" />
309-
</ReferenceField>
310-
</CoreAdminContext>
311-
</ThemeProvider>
312-
);
313-
expect(screen.getByText('EMPTY')).not.toBeNull();
298+
describe('emptyText', () => {
299+
it('should display the emptyText if the field is empty', () => {
300+
render(
301+
<ThemeProvider theme={theme}>
302+
<CoreAdminContext dataProvider={testDataProvider()}>
303+
<ReferenceField
304+
record={{ id: 123 }}
305+
resource="comments"
306+
// @ts-expect-error source prop does not have a valid value
307+
source="postId"
308+
reference="posts"
309+
emptyText="EMPTY"
310+
>
311+
<TextField source="title" />
312+
</ReferenceField>
313+
</CoreAdminContext>
314+
</ThemeProvider>
315+
);
316+
expect(screen.getByText('EMPTY')).not.toBeNull();
317+
});
318+
319+
it('should display the emptyText if there is no reference', async () => {
320+
render(<MissingReferenceEmptyText />);
321+
await screen.findByText('no detail');
322+
});
323+
324+
it('should translate emptyText', async () => {
325+
render(<MissingReferenceIdEmptyTextTranslation />);
326+
327+
expect(await screen.findByText('Not found')).not.toBeNull();
328+
});
314329
});
315330

316-
it('should display the emptyText if there is no reference', async () => {
317-
render(<MissingReferenceEmptyText />);
318-
await screen.findByText('no detail');
331+
describe('empty', () => {
332+
it('should render the empty prop when the record is not found', async () => {
333+
render(<MissingReferenceIdEmpty />);
334+
await screen.findByText('no detail');
335+
});
336+
it('should translate empty if it is a string', async () => {
337+
render(<MissingReferenceIdEmptyTranslation />);
338+
await screen.findByText('Not found');
339+
});
319340
});
320341

321342
it('should use record from RecordContext', async () => {
@@ -582,12 +603,6 @@ describe('<ReferenceField />', () => {
582603
expect(await screen.findByText('novel')).not.toBeNull();
583604
});
584605

585-
it('should translate emptyText', async () => {
586-
render(<MissingReferenceIdEmptyTextTranslation />);
587-
588-
expect(await screen.findByText('Not found')).not.toBeNull();
589-
});
590-
591606
it('should accept a queryOptions prop', async () => {
592607
const dataProvider = testDataProvider({
593608
getMany: jest.fn().mockResolvedValue({

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { SingleFieldList } from '../list/SingleFieldList';
1111
import { Pagination } from '../list/pagination/Pagination';
1212
import {
1313
Basic,
14+
Empty,
1415
WithPagination,
1516
WithPaginationAndSelectAllLimit,
1617
} from './ReferenceManyField.stories';
@@ -274,6 +275,13 @@ describe('<ReferenceManyField />', () => {
274275
});
275276
});
276277

278+
describe('empty', () => {
279+
it('should render the empty prop when the record is not found', async () => {
280+
render(<Empty />);
281+
await screen.findByText('no books');
282+
});
283+
});
284+
277285
describe('"Select all" button', () => {
278286
it('should be displayed if all the items of the page are selected', async () => {
279287
render(<WithPagination />);

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
EmptyTextWithTranslate,
88
QueryOptions,
99
EmptyText,
10+
Empty,
1011
Themed,
1112
} from './ReferenceOneField.stories';
1213

@@ -53,13 +54,26 @@ describe('ReferenceOneField', () => {
5354
});
5455
});
5556

56-
it('should render the "emptyContent" prop when the record is not found', async () => {
57-
render(<EmptyText />);
58-
await waitFor(() => {
59-
expect(screen.queryAllByText('no detail')).toHaveLength(3);
57+
describe('emptyText', () => {
58+
it('should render the emptyText prop when the record is not found', async () => {
59+
render(<EmptyText />);
60+
await waitFor(() => {
61+
expect(screen.queryAllByText('no detail')).toHaveLength(3);
62+
});
63+
fireEvent.click(screen.getByText('War and Peace'));
64+
await screen.findByText('Create');
65+
});
66+
});
67+
68+
describe('empty', () => {
69+
it('should render the empty prop when the record is not found', async () => {
70+
render(<Empty />);
71+
await waitFor(() => {
72+
expect(screen.queryAllByText('no detail')).toHaveLength(3);
73+
});
74+
fireEvent.click(screen.getByText('War and Peace'));
75+
await screen.findByText('Create');
6076
});
61-
fireEvent.click(screen.getByText('War and Peace'));
62-
await screen.findByText('Create');
6377
});
6478

6579
it('should be customized by a theme', async () => {

0 commit comments

Comments
 (0)