Skip to content

Commit c9bb681

Browse files
authored
Merge pull request #10831 from WiXSL/selectinput-test
Added `SelectInput` and `SelectArrayInput` disable choices test and story
2 parents 8eb5d3a + 3b70e49 commit c9bb681

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ describe('<SelectArrayInput />', () => {
228228
expect(screen.queryByText('Programming')).not.toBeNull();
229229
});
230230

231-
it('should render disable choices marked so', () => {
231+
it('should render disable choices marked as so', () => {
232232
render(
233233
<AdminContext dataProvider={testDataProvider()}>
234234
<ResourceContextProvider value="posts">
@@ -254,6 +254,37 @@ describe('<SelectArrayInput />', () => {
254254
expect(option2.getAttribute('aria-disabled')).toEqual('true');
255255
});
256256

257+
it('should render disabled choices marked as so by disableValue prop', () => {
258+
render(
259+
<AdminContext dataProvider={testDataProvider()}>
260+
<ResourceContextProvider value="posts">
261+
<SimpleForm onSubmit={jest.fn()}>
262+
<SelectArrayInput
263+
{...defaultProps}
264+
choices={[
265+
{ id: 'ang', name: 'Angular' },
266+
{
267+
id: 'rea',
268+
name: 'React',
269+
not_available: true,
270+
},
271+
]}
272+
disableValue="not_available"
273+
/>
274+
</SimpleForm>
275+
</ResourceContextProvider>
276+
</AdminContext>
277+
);
278+
fireEvent.mouseDown(
279+
screen.getByLabelText('resources.posts.fields.categories')
280+
);
281+
const option1 = screen.getByText('Angular');
282+
expect(option1.getAttribute('aria-disabled')).toBeNull();
283+
284+
const option2 = screen.getByText('React');
285+
expect(option2.getAttribute('aria-disabled')).toEqual('true');
286+
});
287+
257288
describe('translateChoice', () => {
258289
it('should translate the choices by default', async () => {
259290
render(<TranslateChoice />);

packages/ra-ui-materialui/src/input/SelectArrayInput.stories.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,20 @@ export const Disabled = () => (
167167
</AdminContext>
168168
);
169169

170+
export const DisabledChoice = () => (
171+
<Wrapper>
172+
<SelectArrayInput
173+
source="roles"
174+
choices={[
175+
{ id: 'admin', name: 'Admin' },
176+
{ id: 'u001', name: 'Editor' },
177+
{ id: 'u002', name: 'Moderator', disabled: true },
178+
{ id: 'u003', name: 'Reviewer' },
179+
]}
180+
/>
181+
</Wrapper>
182+
);
183+
170184
export const ReadOnly = () => (
171185
<AdminContext i18nProvider={i18nProvider}>
172186
<Create

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ describe('<SelectInput />', () => {
7979
).toEqual('rea');
8080
});
8181

82-
it('should render disabled choices marked so', () => {
82+
it('should render disabled choices marked as so', () => {
8383
render(
8484
<AdminContext dataProvider={testDataProvider()}>
8585
<ResourceContextProvider value="posts">
@@ -111,6 +111,39 @@ describe('<SelectInput />', () => {
111111
).toEqual('true');
112112
});
113113

114+
it('should render disabled choices marked as so by disableValue prop', () => {
115+
render(
116+
<AdminContext dataProvider={testDataProvider()}>
117+
<ResourceContextProvider value="posts">
118+
<SimpleForm onSubmit={jest.fn()}>
119+
<SelectInput
120+
{...defaultProps}
121+
choices={[
122+
{ id: 'ang', name: 'Angular' },
123+
{
124+
id: 'rea',
125+
name: 'React',
126+
not_available: true,
127+
},
128+
]}
129+
disableValue="not_available"
130+
/>
131+
</SimpleForm>
132+
</ResourceContextProvider>
133+
</AdminContext>
134+
);
135+
fireEvent.mouseDown(
136+
screen.getByLabelText('resources.posts.fields.language')
137+
);
138+
139+
expect(
140+
screen.getByText('Angular').getAttribute('aria-disabled')
141+
).toBeNull();
142+
expect(
143+
screen.getByText('React').getAttribute('aria-disabled')
144+
).toEqual('true');
145+
});
146+
114147
it('should include an empty option by default', () => {
115148
render(
116149
<AdminContext dataProvider={testDataProvider()}>

packages/ra-ui-materialui/src/input/SelectInput.stories.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ export const Disabled = () => (
112112
</Wrapper>
113113
);
114114

115+
export const DisabledChoice = () => (
116+
<Wrapper>
117+
<SelectInput
118+
source="city"
119+
choices={[
120+
{ id: 'P', name: 'Paris' },
121+
{ id: 'L', name: 'London' },
122+
{ id: 'N', name: 'New York', disabled: true },
123+
]}
124+
/>
125+
</Wrapper>
126+
);
127+
115128
export const Variant = ({ hideLabel }) => (
116129
<Wrapper>
117130
<SelectInput

0 commit comments

Comments
 (0)