Skip to content

Commit 4f116c2

Browse files
committed
Fix ChipField consider zero to be empty
1 parent 40c037a commit 4f116c2

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

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

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import expect from 'expect';
33
import { ChipField } from './ChipField';
4-
import { render } from '@testing-library/react';
4+
import { render, screen } from '@testing-library/react';
55
import { RecordContextProvider, I18nContextProvider } from 'ra-core';
66
import polyglotI18nProvider from 'ra-i18n-polyglot';
77

@@ -25,49 +25,59 @@ const i18nProvider = polyglotI18nProvider(
2525
);
2626

2727
describe('<ChipField />', () => {
28-
it('should display the record value added as source', () => {
29-
const { getByText } = render(
30-
<ChipField source="name" record={{ id: 123, name: 'foo' }} />
31-
);
32-
expect(getByText('foo')).not.toBeNull();
28+
it('should display the record value added as source', async () => {
29+
render(<ChipField source="name" record={{ id: 123, name: 'foo' }} />);
30+
await screen.findByText('foo');
3331
});
3432

35-
it('should use record from RecordContext', () => {
36-
const { getByText } = render(
33+
it('should use record from RecordContext', async () => {
34+
render(
3735
<RecordContextProvider value={{ id: 123, name: 'foo' }}>
3836
<ChipField source="name" />
3937
</RecordContextProvider>
4038
);
41-
expect(getByText('foo')).not.toBeNull();
39+
await screen.findByText('foo');
4240
});
4341

44-
it('should not display any label added as props', () => {
45-
const { getByText } = render(
42+
it('should not display any label added as props', async () => {
43+
render(
4644
<ChipField
4745
source="name"
4846
record={{ id: 123, name: 'foo' }}
4947
label="bar"
5048
/>
5149
);
52-
expect(getByText('foo')).not.toBeNull();
50+
await screen.findByText('foo');
5351
});
5452

5553
it.each([null, undefined])(
5654
'should render the emptyText when value is %s',
57-
name => {
58-
const { getByText } = render(
55+
async name => {
56+
render(
5957
<ChipField
6058
source="name"
6159
record={{ id: 123, name }}
6260
emptyText="NA"
6361
/>
6462
);
65-
expect(getByText('NA')).not.toBeNull();
63+
await screen.findByText('NA');
6664
}
6765
);
6866

69-
it('should translate emptyText', () => {
70-
const { getByText } = render(
67+
it('should not render the emptyText when value is zero', async () => {
68+
render(
69+
<ChipField
70+
source="name"
71+
record={{ id: 123, name: 0 }}
72+
emptyText="NA"
73+
/>
74+
);
75+
76+
expect(screen.queryByText('NA')).toBeNull();
77+
});
78+
79+
it('should translate emptyText', async () => {
80+
render(
7181
<I18nContextProvider value={i18nProvider}>
7282
<ChipField
7383
record={{ id: 123 }}
@@ -78,7 +88,7 @@ describe('<ChipField />', () => {
7888
</I18nContextProvider>
7989
);
8090

81-
expect(getByText('Not found')).not.toBeNull();
91+
await screen.findByText('Not found');
8292
});
8393

8494
it('should return null when value and emptyText are an empty string', () => {
@@ -92,15 +102,15 @@ describe('<ChipField />', () => {
92102
expect(container.firstChild).toBeNull();
93103
});
94104

95-
it('should display the emptyText when value is an empty string', () => {
96-
const { getByText } = render(
105+
it('should display the emptyText when value is an empty string', async () => {
106+
render(
97107
<ChipField
98108
source="name"
99109
record={{ id: 123, name: '' }}
100110
emptyText="NA"
101111
/>
102112
);
103-
expect(getByText('NA')).not.toBeNull();
113+
await screen.findByText('NA');
104114
});
105115

106116
it('should return null when value is an empty string and emptyText is null', () => {

packages/ra-ui-materialui/src/field/ChipField.stories.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ export const Basic = ({
2727

2828
Basic.argTypes = {
2929
value: {
30-
options: ['filled', 'empty', 'undefined'],
30+
options: ['filled', 'empty', 'zero', 'undefined'],
3131
mapping: {
3232
filled: 'Bazinga',
3333
empty: '',
34+
zero: 0,
3435
undefined: undefined,
3536
},
3637
control: { type: 'select' },

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const ChipFieldImpl = <
2525
const value = useFieldValue(props);
2626
const translate = useTranslate();
2727

28-
if (!value) {
28+
if (value == null || value === '') {
2929
if (!emptyText || emptyText.length === 0) {
3030
return null;
3131
}

0 commit comments

Comments
 (0)