Skip to content

Commit 7ddda78

Browse files
committed
use CreateLabel story in tests
1 parent e876fda commit 7ddda78

File tree

1 file changed

+15
-77
lines changed

1 file changed

+15
-77
lines changed

packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx

Lines changed: 15 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
WithInputProps,
2828
OnCreate,
2929
OnCreateSlow,
30+
CreateLabel,
3031
} from './AutocompleteInput.stories';
3132
import { ReferenceArrayInput } from './ReferenceArrayInput';
3233
import { AutocompleteArrayInput } from './AutocompleteArrayInput';
@@ -1213,41 +1214,11 @@ describe('<AutocompleteInput />', () => {
12131214
expect(screen.queryByText('ra.action.create_item')).toBeNull();
12141215
});
12151216
it('should include an option with the custom createLabel when the input is empty', async () => {
1216-
const choices = [
1217-
{ id: 'ang', name: 'Angular' },
1218-
{ id: 'rea', name: 'React' },
1219-
];
1220-
const handleCreate = filter => {
1221-
const newChoice = {
1222-
id: 'js_fatigue',
1223-
name: filter,
1224-
};
1225-
choices.push(newChoice);
1226-
return newChoice;
1227-
};
1217+
render(<CreateLabel />);
12281218

1229-
render(
1230-
<AdminContext dataProvider={testDataProvider()}>
1231-
<ResourceContextProvider value="posts">
1232-
<SimpleForm
1233-
mode="onBlur"
1234-
onSubmit={jest.fn()}
1235-
defaultValues={{ language: 'ang' }}
1236-
>
1237-
<AutocompleteInput
1238-
source="language"
1239-
choices={choices}
1240-
onCreate={handleCreate}
1241-
createLabel="Start typing to create a new item"
1242-
/>
1243-
</SimpleForm>
1244-
</ResourceContextProvider>
1245-
</AdminContext>
1246-
);
1247-
1248-
const input = screen.getByLabelText(
1249-
'resources.posts.fields.language'
1250-
) as HTMLInputElement;
1219+
const input = (await screen.findByLabelText(
1220+
'Author'
1221+
)) as HTMLInputElement;
12511222
input.focus();
12521223
fireEvent.change(input, {
12531224
target: { value: '' },
@@ -1256,69 +1227,36 @@ describe('<AutocompleteInput />', () => {
12561227
expect(
12571228
screen.queryByText('Start typing to create a new item')
12581229
).not.toBeNull();
1259-
expect(screen.queryByText('ra.action.create')).toBeNull();
1260-
expect(screen.queryByText('ra.action.create_item')).toBeNull();
1230+
expect(screen.queryByText(/Create/)).toBeNull();
12611231
});
12621232
it('should not show the create option when a choice is selected when using a custom createLabel', async () => {
1263-
const choices = [
1264-
{ id: 'ang', name: 'Angular' },
1265-
{ id: 'rea', name: 'React' },
1266-
];
1267-
const handleCreate = filter => {
1268-
const newChoice = {
1269-
id: 'js_fatigue',
1270-
name: filter,
1271-
};
1272-
choices.push(newChoice);
1273-
return newChoice;
1274-
};
1275-
1276-
render(
1277-
<AdminContext dataProvider={testDataProvider()}>
1278-
<ResourceContextProvider value="posts">
1279-
<SimpleForm
1280-
mode="onBlur"
1281-
onSubmit={jest.fn()}
1282-
defaultValues={{ language: 'ang' }}
1283-
>
1284-
<AutocompleteInput
1285-
source="language"
1286-
choices={choices}
1287-
onCreate={handleCreate}
1288-
createLabel="Start typing to create a new item"
1289-
/>
1290-
</SimpleForm>
1291-
</ResourceContextProvider>
1292-
</AdminContext>
1293-
);
1233+
render(<CreateLabel />);
12941234

1295-
const input = screen.getByLabelText(
1296-
'resources.posts.fields.language'
1297-
) as HTMLInputElement;
1235+
const input = (await screen.findByLabelText(
1236+
'Author'
1237+
)) as HTMLInputElement;
12981238
input.focus();
12991239

13001240
// First, clear the input
13011241
fireEvent.change(input, {
13021242
target: { value: '' },
13031243
});
13041244
// We expect only the 'Start typing to create a new item' option
1305-
await screen.findByText('React');
1245+
await screen.findByText('Victor Hugo');
13061246
expect(
13071247
screen.queryByText('Start typing to create a new item')
13081248
).not.toBeNull();
1309-
expect(screen.queryByText('ra.action.create_item')).toBeNull();
1310-
expect(screen.queryByText('ra.action.create')).toBeNull();
1249+
expect(screen.queryByText(/Create/)).toBeNull();
13111250

13121251
// Then, change the input to an existing value
1313-
fireEvent.click(screen.getByText('Angular'));
1252+
fireEvent.click(screen.getByText('Leo Tolstoy'));
13141253
fireEvent.focus(input);
13151254
// We expect all create labels not to render
1316-
await screen.findByText('React');
1255+
await screen.findByText('Victor Hugo');
13171256
expect(
13181257
screen.queryByText('Start typing to create a new item')
13191258
).toBeNull();
1320-
expect(screen.queryByText('ra.action.create_item')).toBeNull();
1321-
expect(screen.queryByText('ra.action.create')).toBeNull();
1259+
expect(screen.queryByText(/Create/)).toBeNull();
13221260
});
13231261
it('should include an option with the createItemLabel when the input not empty', async () => {
13241262
const choices = [

0 commit comments

Comments
 (0)