1
1
import * as React from 'react' ;
2
2
import expect from 'expect' ;
3
3
import { ChipField } from './ChipField' ;
4
- import { render } from '@testing-library/react' ;
4
+ import { render , screen } from '@testing-library/react' ;
5
5
import { RecordContextProvider , I18nContextProvider } from 'ra-core' ;
6
6
import polyglotI18nProvider from 'ra-i18n-polyglot' ;
7
7
@@ -25,49 +25,59 @@ const i18nProvider = polyglotI18nProvider(
25
25
) ;
26
26
27
27
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' ) ;
33
31
} ) ;
34
32
35
- it ( 'should use record from RecordContext' , ( ) => {
36
- const { getByText } = render (
33
+ it ( 'should use record from RecordContext' , async ( ) => {
34
+ render (
37
35
< RecordContextProvider value = { { id : 123 , name : 'foo' } } >
38
36
< ChipField source = "name" />
39
37
</ RecordContextProvider >
40
38
) ;
41
- expect ( getByText ( 'foo' ) ) . not . toBeNull ( ) ;
39
+ await screen . findByText ( 'foo' ) ;
42
40
} ) ;
43
41
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 (
46
44
< ChipField
47
45
source = "name"
48
46
record = { { id : 123 , name : 'foo' } }
49
47
label = "bar"
50
48
/>
51
49
) ;
52
- expect ( getByText ( 'foo' ) ) . not . toBeNull ( ) ;
50
+ await screen . findByText ( 'foo' ) ;
53
51
} ) ;
54
52
55
53
it . each ( [ null , undefined ] ) (
56
54
'should render the emptyText when value is %s' ,
57
- name => {
58
- const { getByText } = render (
55
+ async name => {
56
+ render (
59
57
< ChipField
60
58
source = "name"
61
59
record = { { id : 123 , name } }
62
60
emptyText = "NA"
63
61
/>
64
62
) ;
65
- expect ( getByText ( 'NA' ) ) . not . toBeNull ( ) ;
63
+ await screen . findByText ( 'NA' ) ;
66
64
}
67
65
) ;
68
66
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 (
71
81
< I18nContextProvider value = { i18nProvider } >
72
82
< ChipField
73
83
record = { { id : 123 } }
@@ -78,7 +88,7 @@ describe('<ChipField />', () => {
78
88
</ I18nContextProvider >
79
89
) ;
80
90
81
- expect ( getByText ( 'Not found' ) ) . not . toBeNull ( ) ;
91
+ await screen . findByText ( 'Not found' ) ;
82
92
} ) ;
83
93
84
94
it ( 'should return null when value and emptyText are an empty string' , ( ) => {
@@ -92,15 +102,15 @@ describe('<ChipField />', () => {
92
102
expect ( container . firstChild ) . toBeNull ( ) ;
93
103
} ) ;
94
104
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 (
97
107
< ChipField
98
108
source = "name"
99
109
record = { { id : 123 , name : '' } }
100
110
emptyText = "NA"
101
111
/>
102
112
) ;
103
- expect ( getByText ( 'NA' ) ) . not . toBeNull ( ) ;
113
+ await screen . findByText ( 'NA' ) ;
104
114
} ) ;
105
115
106
116
it ( 'should return null when value is an empty string and emptyText is null' , ( ) => {
0 commit comments