Skip to content

Commit 0a10d01

Browse files
committed
Fix tests to use AdminContext instead of CoreAdminContext
1 parent 6eaca36 commit 0a10d01

File tree

5 files changed

+483
-599
lines changed

5 files changed

+483
-599
lines changed

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

Lines changed: 61 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as React from 'react';
22
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
3-
import { CoreAdminContext, testDataProvider } from 'ra-core';
3+
import { testDataProvider } from 'ra-core';
44
import { createTheme, ThemeProvider } from '@mui/material/styles';
55

6-
import { defaultTheme } from '../defaultTheme';
6+
import { AdminContext } from '../AdminContext';
77
import { SimpleForm } from '../form';
88
import { BooleanInput } from './BooleanInput';
99

@@ -15,16 +15,14 @@ describe('<BooleanInput />', () => {
1515

1616
it('should render as a checkbox', () => {
1717
render(
18-
<ThemeProvider theme={createTheme(defaultTheme)}>
19-
<CoreAdminContext dataProvider={testDataProvider()}>
20-
<SimpleForm
21-
defaultValues={{ isPublished: true }}
22-
onSubmit={jest.fn}
23-
>
24-
<BooleanInput {...defaultProps} />
25-
</SimpleForm>
26-
</CoreAdminContext>
27-
</ThemeProvider>
18+
<AdminContext dataProvider={testDataProvider()}>
19+
<SimpleForm
20+
defaultValues={{ isPublished: true }}
21+
onSubmit={jest.fn}
22+
>
23+
<BooleanInput {...defaultProps} />
24+
</SimpleForm>
25+
</AdminContext>
2826
);
2927

3028
const input = screen.getByLabelText(
@@ -36,16 +34,14 @@ describe('<BooleanInput />', () => {
3634

3735
it('should be checked if the value is true', () => {
3836
render(
39-
<ThemeProvider theme={createTheme(defaultTheme)}>
40-
<CoreAdminContext dataProvider={testDataProvider()}>
41-
<SimpleForm
42-
onSubmit={jest.fn}
43-
defaultValues={{ isPublished: true }}
44-
>
45-
<BooleanInput {...defaultProps} />
46-
</SimpleForm>
47-
</CoreAdminContext>
48-
</ThemeProvider>
37+
<AdminContext dataProvider={testDataProvider()}>
38+
<SimpleForm
39+
onSubmit={jest.fn}
40+
defaultValues={{ isPublished: true }}
41+
>
42+
<BooleanInput {...defaultProps} />
43+
</SimpleForm>
44+
</AdminContext>
4945
);
5046

5147
const input = screen.getByLabelText(
@@ -57,16 +53,14 @@ describe('<BooleanInput />', () => {
5753

5854
it('should not be checked if the value is false', () => {
5955
render(
60-
<ThemeProvider theme={createTheme(defaultTheme)}>
61-
<CoreAdminContext dataProvider={testDataProvider()}>
62-
<SimpleForm
63-
onSubmit={jest.fn}
64-
defaultValues={{ isPublished: false }}
65-
>
66-
<BooleanInput {...defaultProps} />
67-
</SimpleForm>
68-
</CoreAdminContext>
69-
</ThemeProvider>
56+
<AdminContext dataProvider={testDataProvider()}>
57+
<SimpleForm
58+
onSubmit={jest.fn}
59+
defaultValues={{ isPublished: false }}
60+
>
61+
<BooleanInput {...defaultProps} />
62+
</SimpleForm>
63+
</AdminContext>
7064
);
7165

7266
const input = screen.getByLabelText(
@@ -78,13 +72,11 @@ describe('<BooleanInput />', () => {
7872

7973
it('should not be checked if the value is undefined', () => {
8074
render(
81-
<ThemeProvider theme={createTheme(defaultTheme)}>
82-
<CoreAdminContext dataProvider={testDataProvider()}>
83-
<SimpleForm onSubmit={jest.fn}>
84-
<BooleanInput {...defaultProps} />
85-
</SimpleForm>
86-
</CoreAdminContext>
87-
</ThemeProvider>
75+
<AdminContext dataProvider={testDataProvider()}>
76+
<SimpleForm onSubmit={jest.fn}>
77+
<BooleanInput {...defaultProps} />
78+
</SimpleForm>
79+
</AdminContext>
8880
);
8981

9082
const input = screen.getByLabelText(
@@ -96,13 +88,11 @@ describe('<BooleanInput />', () => {
9688

9789
it('should be checked if the value is undefined and defaultValue is true', () => {
9890
render(
99-
<ThemeProvider theme={createTheme(defaultTheme)}>
100-
<CoreAdminContext dataProvider={testDataProvider()}>
101-
<SimpleForm onSubmit={jest.fn}>
102-
<BooleanInput {...defaultProps} defaultValue={true} />
103-
</SimpleForm>
104-
</CoreAdminContext>
105-
</ThemeProvider>
91+
<AdminContext dataProvider={testDataProvider()}>
92+
<SimpleForm onSubmit={jest.fn}>
93+
<BooleanInput {...defaultProps} defaultValue={true} />
94+
</SimpleForm>
95+
</AdminContext>
10696
);
10797

10898
const input = screen.getByLabelText(
@@ -114,16 +104,11 @@ describe('<BooleanInput />', () => {
114104

115105
it('should be checked if the value is true and defaultValue is false', () => {
116106
render(
117-
<ThemeProvider theme={createTheme(defaultTheme)}>
118-
<CoreAdminContext dataProvider={testDataProvider()}>
119-
<SimpleForm
120-
onSubmit={jest.fn}
121-
record={{ isPublished: true }}
122-
>
123-
<BooleanInput {...defaultProps} defaultValue={false} />
124-
</SimpleForm>
125-
</CoreAdminContext>
126-
</ThemeProvider>
107+
<AdminContext dataProvider={testDataProvider()}>
108+
<SimpleForm onSubmit={jest.fn} record={{ isPublished: true }}>
109+
<BooleanInput {...defaultProps} defaultValue={false} />
110+
</SimpleForm>
111+
</AdminContext>
127112
);
128113

129114
const input = screen.getByLabelText(
@@ -135,16 +120,14 @@ describe('<BooleanInput />', () => {
135120

136121
it('should update on click', async () => {
137122
render(
138-
<ThemeProvider theme={createTheme(defaultTheme)}>
139-
<CoreAdminContext dataProvider={testDataProvider()}>
140-
<SimpleForm
141-
defaultValues={{ isPublished: false }}
142-
onSubmit={jest.fn}
143-
>
144-
<BooleanInput {...defaultProps} />
145-
</SimpleForm>
146-
</CoreAdminContext>
147-
</ThemeProvider>
123+
<AdminContext dataProvider={testDataProvider()}>
124+
<SimpleForm
125+
defaultValues={{ isPublished: false }}
126+
onSubmit={jest.fn}
127+
>
128+
<BooleanInput {...defaultProps} />
129+
</SimpleForm>
130+
</AdminContext>
148131
);
149132

150133
const input = screen.getByLabelText(
@@ -162,24 +145,24 @@ describe('<BooleanInput />', () => {
162145
const validate = () => 'ra.validation.error';
163146

164147
render(
165-
<ThemeProvider theme={createTheme(defaultTheme)}>
166-
<CoreAdminContext dataProvider={testDataProvider()}>
167-
<SimpleForm
168-
onSubmit={jest.fn}
169-
defaultValues={{ isPublished: true }}
170-
mode="onChange"
171-
>
172-
<BooleanInput {...defaultProps} validate={validate} />
173-
</SimpleForm>
174-
</CoreAdminContext>
175-
</ThemeProvider>
148+
<AdminContext dataProvider={testDataProvider()}>
149+
<SimpleForm
150+
onSubmit={jest.fn}
151+
defaultValues={{ isPublished: true }}
152+
mode="onChange"
153+
>
154+
<BooleanInput {...defaultProps} validate={validate} />
155+
</SimpleForm>
156+
</AdminContext>
176157
);
177158
const input = screen.getByLabelText(
178159
'resources.posts.fields.isPublished'
179160
) as HTMLInputElement;
180161

181162
fireEvent.click(input);
182-
expect(input.checked).toBe(false);
163+
await waitFor(() => {
164+
expect(input.checked).toBe(false);
165+
});
183166

184167
await waitFor(() => {
185168
expect(screen.queryByText('ra.validation.error')).not.toBeNull();

packages/ra-ui-materialui/src/input/BooleanInput.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const BooleanInput = (props: BooleanInputProps) => {
4646

4747
const handleChange = useCallback(
4848
(event, value) => {
49-
field.onChange(event);
49+
field.onChange(value);
5050
// Ensure field is considered as touched
5151
field.onBlur();
5252
},

0 commit comments

Comments
 (0)