-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathTextField.spec.tsx
115 lines (107 loc) · 3.73 KB
/
TextField.spec.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import * as React from 'react';
import expect from 'expect';
import { screen, render, getNodeText } from '@testing-library/react';
import { RecordContextProvider, I18nContextProvider } from 'ra-core';
import polyglotI18nProvider from 'ra-i18n-polyglot';
import { TextField } from './TextField';
describe('<TextField />', () => {
it('should display record specific value as plain text', () => {
const record = {
id: 123,
title: "I'm sorry, Dave. I'm afraid I can't do that.",
};
const { queryByText } = render(
<TextField record={record} source="title" />
);
expect(
queryByText("I'm sorry, Dave. I'm afraid I can't do that.")
).not.toBeNull();
});
it('should use record from RecordContext', () => {
const record = {
id: 123,
title: "I'm sorry, Dave. I'm afraid I can't do that.",
};
const { queryByText } = render(
<RecordContextProvider value={record}>
<TextField source="title" />
</RecordContextProvider>
);
expect(
queryByText("I'm sorry, Dave. I'm afraid I can't do that.")
).not.toBeNull();
});
it.each([null, undefined])(
'should display emptyText prop if provided for %s value',
value => {
const record = { id: 123, title: value };
render(
<TextField
emptyText="Sorry, there's nothing here"
record={record}
source="title"
/>
);
expect("Sorry, there's nothing here").not.toBeNull();
}
);
it.each([null, undefined])(
'should display nothing for %s value without emptyText prop',
value => {
const record = { id: 123, title: value };
const { container } = render(
<TextField record={record} source="title" />
);
expect(getNodeText(container)).toStrictEqual('');
}
);
it('should handle deep fields', () => {
const record = {
id: 123,
foo: { title: "I'm sorry, Dave. I'm afraid I can't do that." },
};
const { queryByText } = render(
<TextField record={record} source="foo.title" />
);
expect(
queryByText("I'm sorry, Dave. I'm afraid I can't do that.")
).not.toBeNull();
});
it('should render the emptyText when value is null', () => {
const record = { id: 123, title: null };
const { queryByText } = render(
<TextField record={record} source="title" emptyText="NA" />
);
expect(queryByText('NA')).not.toBeNull();
});
it('should translate emptyText', () => {
const i18nProvider = polyglotI18nProvider(
_locale =>
({
resources: {
books: {
name: 'Books',
fields: {
id: 'Id',
title: 'Title',
author: 'Author',
year: 'Year',
},
not_found: 'Not found',
},
},
} as any),
'en'
);
render(
<I18nContextProvider value={i18nProvider}>
<TextField
record={{ id: 123 }}
source="foo.bar"
emptyText="resources.books.not_found"
/>
</I18nContextProvider>
);
expect(screen.getByText('Not found')).not.toBeNull();
});
});