Skip to content

Commit c81c22a

Browse files
committedSep 7, 2022
test: add translated empty field tests
1 parent f525eaa commit c81c22a

11 files changed

+330
-9
lines changed
 

‎packages/ra-ui-materialui/src/field/BooleanField.spec.tsx

+34-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as React from 'react';
22
import expect from 'expect';
33
import { BooleanField } from './BooleanField';
44
import { screen, render } from '@testing-library/react';
5-
import { RecordContextProvider } from 'ra-core';
5+
import { RecordContextProvider, I18nContextProvider } from 'ra-core';
6+
import polyglotI18nProvider from 'ra-i18n-polyglot';
67

78
const defaultProps = {
89
record: { id: 123, published: true },
@@ -11,6 +12,24 @@ const defaultProps = {
1112
classes: {},
1213
};
1314

15+
const i18nProvider = polyglotI18nProvider(
16+
_locale => ({
17+
resources: {
18+
books: {
19+
name: 'Books',
20+
fields: {
21+
id: 'Id',
22+
title: 'Title',
23+
author: 'Author',
24+
year: 'Year',
25+
},
26+
not_found: 'Not found',
27+
},
28+
},
29+
}),
30+
'en'
31+
);
32+
1433
describe('<BooleanField />', () => {
1534
it('should display tick and truthy text if value is true', () => {
1635
render(<BooleanField {...defaultProps} />);
@@ -152,4 +171,18 @@ describe('<BooleanField />', () => {
152171
);
153172
expect(screen.queryByLabelText('ra.boolean.true')).not.toBeNull();
154173
});
174+
175+
it('should translate emptyText', () => {
176+
const { getByText } = render(
177+
<I18nContextProvider value={i18nProvider}>
178+
<BooleanField
179+
record={{ id: 123 }}
180+
source="foo.bar"
181+
emptyText="resources.books.not_found"
182+
/>
183+
</I18nContextProvider>
184+
);
185+
186+
expect(getByText('Not found')).not.toBeNull();
187+
});
155188
});

‎packages/ra-ui-materialui/src/field/ChipField.spec.tsx

+34-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,26 @@ import * as React from 'react';
22
import expect from 'expect';
33
import { ChipField } from './ChipField';
44
import { render } from '@testing-library/react';
5-
import { RecordContextProvider } from 'ra-core';
5+
import { RecordContextProvider, I18nContextProvider } from 'ra-core';
6+
import polyglotI18nProvider from 'ra-i18n-polyglot';
7+
8+
const i18nProvider = polyglotI18nProvider(
9+
_locale => ({
10+
resources: {
11+
books: {
12+
name: 'Books',
13+
fields: {
14+
id: 'Id',
15+
title: 'Title',
16+
author: 'Author',
17+
year: 'Year',
18+
},
19+
not_found: 'Not found',
20+
},
21+
},
22+
}),
23+
'en'
24+
);
625

726
describe('<ChipField />', () => {
827
it('should display the record value added as source', () => {
@@ -54,4 +73,18 @@ describe('<ChipField />', () => {
5473
expect(getByText('NA')).not.toBeNull();
5574
}
5675
);
76+
77+
it('should translate emptyText', () => {
78+
const { getByText } = render(
79+
<I18nContextProvider value={i18nProvider}>
80+
<ChipField
81+
record={{ id: 123 }}
82+
source="foo.bar"
83+
emptyText="resources.books.not_found"
84+
/>
85+
</I18nContextProvider>
86+
);
87+
88+
expect(getByText('Not found')).not.toBeNull();
89+
});
5790
});

‎packages/ra-ui-materialui/src/field/DateField.spec.tsx

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
import * as React from 'react';
22
import expect from 'expect';
33
import { render } from '@testing-library/react';
4-
import { RecordContextProvider } from 'ra-core';
4+
import { RecordContextProvider, I18nContextProvider } from 'ra-core';
5+
import polyglotI18nProvider from 'ra-i18n-polyglot';
56

67
import { DateField } from './DateField';
78

9+
const i18nProvider = polyglotI18nProvider(
10+
_locale => ({
11+
resources: {
12+
books: {
13+
name: 'Books',
14+
fields: {
15+
id: 'Id',
16+
title: 'Title',
17+
author: 'Author',
18+
year: 'Year',
19+
},
20+
not_found: 'Not found',
21+
},
22+
},
23+
}),
24+
'en'
25+
);
26+
827
describe('<DateField />', () => {
928
it('should return null when the record is not set', () => {
1029
const { container } = render(<DateField source="foo" />);
@@ -161,4 +180,18 @@ describe('<DateField />', () => {
161180
expect(queryByText('NA')).not.toBeNull();
162181
}
163182
);
183+
184+
it('should translate emptyText', () => {
185+
const { getByText } = render(
186+
<I18nContextProvider value={i18nProvider}>
187+
<DateField
188+
record={{ id: 123 }}
189+
source="foo.bar"
190+
emptyText="resources.books.not_found"
191+
/>
192+
</I18nContextProvider>
193+
);
194+
195+
expect(getByText('Not found')).not.toBeNull();
196+
});
164197
});

‎packages/ra-ui-materialui/src/field/EmailField.spec.tsx

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
import * as React from 'react';
22
import expect from 'expect';
33
import { render } from '@testing-library/react';
4-
import { RecordContextProvider } from 'ra-core';
4+
import { RecordContextProvider, I18nContextProvider } from 'ra-core';
5+
import polyglotI18nProvider from 'ra-i18n-polyglot';
56

67
import { EmailField } from './EmailField';
78

9+
const i18nProvider = polyglotI18nProvider(
10+
_locale => ({
11+
resources: {
12+
books: {
13+
name: 'Books',
14+
fields: {
15+
id: 'Id',
16+
title: 'Title',
17+
author: 'Author',
18+
year: 'Year',
19+
},
20+
not_found: 'Not found',
21+
},
22+
},
23+
}),
24+
'en'
25+
);
26+
827
const url = 'foo@bar.com';
928

1029
describe('<EmailField />', () => {
@@ -87,4 +106,18 @@ describe('<EmailField />', () => {
87106
);
88107
expect(container.firstChild).toBeNull();
89108
});
109+
110+
it('should translate emptyText', () => {
111+
const { getByText } = render(
112+
<I18nContextProvider value={i18nProvider}>
113+
<EmailField
114+
record={{ id: 123 }}
115+
source="foo.bar"
116+
emptyText="resources.books.not_found"
117+
/>
118+
</I18nContextProvider>
119+
);
120+
121+
expect(getByText('Not found')).not.toBeNull();
122+
});
90123
});

‎packages/ra-ui-materialui/src/field/FileField.spec.tsx

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as React from 'react';
22
import expect from 'expect';
33
import { render } from '@testing-library/react';
4-
import { RecordContextProvider } from 'ra-core';
4+
import { RecordContextProvider, I18nContextProvider } from 'ra-core';
5+
import polyglotI18nProvider from 'ra-i18n-polyglot';
56

67
import { FileField } from './FileField';
78

@@ -10,6 +11,24 @@ const defaultProps = {
1011
source: 'url',
1112
};
1213

14+
const i18nProvider = polyglotI18nProvider(
15+
_locale => ({
16+
resources: {
17+
books: {
18+
name: 'Books',
19+
fields: {
20+
id: 'Id',
21+
title: 'Title',
22+
author: 'Author',
23+
year: 'Year',
24+
},
25+
not_found: 'Not found',
26+
},
27+
},
28+
}),
29+
'en'
30+
);
31+
1332
describe('<FileField />', () => {
1433
it('should return an empty div when record is not set', () => {
1534
const { container } = render(<FileField {...defaultProps} />);
@@ -163,4 +182,18 @@ describe('<FileField />', () => {
163182
);
164183
expect(container.children[0].classList.contains('foo')).toBe(true);
165184
});
185+
186+
it('should translate emptyText', () => {
187+
const { getByText } = render(
188+
<I18nContextProvider value={i18nProvider}>
189+
<FileField
190+
record={{ id: 123 }}
191+
source="foo.bar"
192+
emptyText="resources.books.not_found"
193+
/>
194+
</I18nContextProvider>
195+
);
196+
197+
expect(getByText('Not found')).not.toBeNull();
198+
});
166199
});

‎packages/ra-ui-materialui/src/field/ImageField.spec.tsx

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as React from 'react';
22
import expect from 'expect';
33
import { render } from '@testing-library/react';
4-
import { RecordContextProvider } from 'ra-core';
4+
import { RecordContextProvider, I18nContextProvider } from 'ra-core';
5+
import polyglotI18nProvider from 'ra-i18n-polyglot';
56

67
import { ImageField } from './ImageField';
78

@@ -10,6 +11,24 @@ const defaultProps = {
1011
source: 'url',
1112
};
1213

14+
const i18nProvider = polyglotI18nProvider(
15+
_locale => ({
16+
resources: {
17+
books: {
18+
name: 'Books',
19+
fields: {
20+
id: 'Id',
21+
title: 'Title',
22+
author: 'Author',
23+
year: 'Year',
24+
},
25+
not_found: 'Not found',
26+
},
27+
},
28+
}),
29+
'en'
30+
);
31+
1332
describe('<ImageField />', () => {
1433
it('should return an empty div when record is not set', () => {
1534
const { container } = render(<ImageField {...defaultProps} />);
@@ -151,4 +170,18 @@ describe('<ImageField />', () => {
151170

152171
expect(container.children[0].classList.contains('foo')).toBe(true);
153172
});
173+
174+
it('should translate emptyText', () => {
175+
const { getByText } = render(
176+
<I18nContextProvider value={i18nProvider}>
177+
<ImageField
178+
record={{ id: 123 }}
179+
source="foo.bar"
180+
emptyText="resources.books.not_found"
181+
/>
182+
</I18nContextProvider>
183+
);
184+
185+
expect(getByText('Not found')).not.toBeNull();
186+
});
154187
});

‎packages/ra-ui-materialui/src/field/NumberField.spec.tsx

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
import * as React from 'react';
22
import expect from 'expect';
33
import { render } from '@testing-library/react';
4-
import { RecordContextProvider } from 'ra-core';
4+
import { RecordContextProvider, I18nContextProvider } from 'ra-core';
5+
import polyglotI18nProvider from 'ra-i18n-polyglot';
56

67
import { NumberField } from './NumberField';
78

9+
const i18nProvider = polyglotI18nProvider(
10+
_locale => ({
11+
resources: {
12+
books: {
13+
name: 'Books',
14+
fields: {
15+
id: 'Id',
16+
title: 'Title',
17+
author: 'Author',
18+
year: 'Year',
19+
},
20+
not_found: 'Not found',
21+
},
22+
},
23+
}),
24+
'en'
25+
);
26+
827
describe('<NumberField />', () => {
928
it('should return null when the record is not set', () => {
1029
const { container } = render(<NumberField source="foo" />);
@@ -93,4 +112,18 @@ describe('<NumberField />', () => {
93112

94113
expect(queryByText('2')).not.toBeNull();
95114
});
115+
116+
it('should translate emptyText', () => {
117+
const { getByText } = render(
118+
<I18nContextProvider value={i18nProvider}>
119+
<NumberField
120+
record={{ id: 123 }}
121+
source="foo.bar"
122+
emptyText="resources.books.not_found"
123+
/>
124+
</I18nContextProvider>
125+
);
126+
127+
expect(getByText('Not found')).not.toBeNull();
128+
});
96129
});

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import { QueryClient } from 'react-query';
1212
import { createTheme, ThemeProvider } from '@mui/material/styles';
1313

1414
import { ReferenceField } from './ReferenceField';
15-
import { Children, MissingReference } from './ReferenceField.stories';
15+
import {
16+
Children,
17+
EmptyWithTranslate,
18+
MissingReference,
19+
} from './ReferenceField.stories';
1620
import { TextField } from './TextField';
1721

1822
const theme = createTheme({});
@@ -457,4 +461,10 @@ describe('<ReferenceField />', () => {
457461
expect(screen.findByText('9780393966473')).not.toBeNull();
458462
expect(screen.findByText('novel')).not.toBeNull();
459463
});
464+
465+
it('should translate emptyText', () => {
466+
render(<EmptyWithTranslate />);
467+
468+
expect(screen.findByText('Not found')).not.toBeNull();
469+
});
460470
});

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import * as React from 'react';
22
import { render, screen } from '@testing-library/react';
33

4-
import { RecordRepresentation, Basic } from './ReferenceOneField.stories';
4+
import {
5+
RecordRepresentation,
6+
Basic,
7+
EmptyWithTranslate,
8+
} from './ReferenceOneField.stories';
59

610
describe('ReferenceOneField', () => {
711
it('should render the recordRepresentation of the related record', async () => {
@@ -12,4 +16,10 @@ describe('ReferenceOneField', () => {
1216
render(<Basic />);
1317
await screen.findByText('9780393966473');
1418
});
19+
20+
it('should translate emptyText', () => {
21+
render(<EmptyWithTranslate />);
22+
23+
expect(screen.findByText('Not found')).not.toBeNull();
24+
});
1525
});

‎packages/ra-ui-materialui/src/field/SelectField.spec.tsx

+35
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,30 @@ import {
55
RecordContextProvider,
66
TestTranslationProvider,
77
useRecordContext,
8+
I18nContextProvider,
89
} from 'ra-core';
10+
import polyglotI18nProvider from 'ra-i18n-polyglot';
911

1012
import { SelectField } from './SelectField';
1113

14+
const i18nProvider = polyglotI18nProvider(
15+
_locale => ({
16+
resources: {
17+
books: {
18+
name: 'Books',
19+
fields: {
20+
id: 'Id',
21+
title: 'Title',
22+
author: 'Author',
23+
year: 'Year',
24+
},
25+
not_found: 'Not found',
26+
},
27+
},
28+
}),
29+
'en'
30+
);
31+
1232
describe('<SelectField />', () => {
1333
const defaultProps = {
1434
source: 'foo',
@@ -160,4 +180,19 @@ describe('<SelectField />', () => {
160180
expect(screen.queryAllByText('hello')).toHaveLength(1);
161181
expect(screen.queryAllByText('bonjour')).toHaveLength(0);
162182
});
183+
184+
it('should translate emptyText', () => {
185+
const { getByText } = render(
186+
<I18nContextProvider value={i18nProvider}>
187+
<SelectField
188+
{...defaultProps}
189+
record={{ id: 123 }}
190+
source="foo.bar"
191+
emptyText="resources.books.not_found"
192+
/>
193+
</I18nContextProvider>
194+
);
195+
196+
expect(getByText('Not found')).not.toBeNull();
197+
});
163198
});

‎packages/ra-ui-materialui/src/field/UrlField.spec.tsx

+35
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
import * as React from 'react';
22
import expect from 'expect';
33
import { render } from '@testing-library/react';
4+
import { I18nContextProvider } from 'ra-core';
5+
import polyglotI18nProvider from 'ra-i18n-polyglot';
6+
47
import { UrlField } from './UrlField';
58

69
const url = 'https://en.wikipedia.org/wiki/HAL_9000';
710

11+
const i18nProvider = polyglotI18nProvider(
12+
_locale => ({
13+
resources: {
14+
books: {
15+
name: 'Books',
16+
fields: {
17+
id: 'Id',
18+
title: 'Title',
19+
author: 'Author',
20+
year: 'Year',
21+
},
22+
not_found: 'Not found',
23+
},
24+
},
25+
}),
26+
'en'
27+
);
28+
829
describe('<UrlField />', () => {
930
it('should render a link', () => {
1031
const record = { id: 123, website: url };
@@ -42,4 +63,18 @@ describe('<UrlField />', () => {
4263
expect(getByText('NA')).not.toEqual(null);
4364
}
4465
);
66+
67+
it('should translate emptyText', () => {
68+
const { getByText } = render(
69+
<I18nContextProvider value={i18nProvider}>
70+
<UrlField
71+
record={{ id: 123 }}
72+
source="foo.bar"
73+
emptyText="resources.books.not_found"
74+
/>
75+
</I18nContextProvider>
76+
);
77+
78+
expect(getByText('Not found')).not.toBeNull();
79+
});
4580
});

0 commit comments

Comments
 (0)
Please sign in to comment.