Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Jan 19, 2021
1 parent d1a7c75 commit a9c2833
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -665,4 +665,65 @@ describe('<AutocompleteArrayInput />', () => {
fireEvent.focus(input);
expect(queryAllByRole('option')).toHaveLength(1);
});

// TODO: restore once master has been merged back to next
it.skip('should not render a LinearProgress if loading is true and a second has not passed yet', () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<AutocompleteArrayInput
{...{
...defaultProps,
loaded: true,
loading: true,
}}
/>
)}
/>
);

expect(queryByRole('progressbar')).toBeNull();
});

it('should render a LinearProgress if loading is true and a second has passed', async () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<AutocompleteArrayInput
{...{
...defaultProps,
loaded: true,
loading: true,
}}
/>
)}
/>
);

await new Promise(resolve => setTimeout(resolve, 1001));

expect(queryByRole('progressbar')).not.toBeNull();
});

it('should not render a LinearProgress if loading is false', () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<AutocompleteArrayInput
{...{
...defaultProps,
}}
/>
)}
/>
);

expect(queryByRole('progressbar')).toBeNull();
});
});
61 changes: 61 additions & 0 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -639,4 +639,65 @@ describe('<AutocompleteInput />', () => {
expect(queryByDisplayValue('foo')).not.toBeNull();
});
});

// TODO: restore once master has been merged back to next
it.skip('should not render a LinearProgress if loading is true and a second has not passed yet', () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<AutocompleteInput
{...{
...defaultProps,
loaded: true,
loading: true,
}}
/>
)}
/>
);

expect(queryByRole('progressbar')).toBeNull();
});

it('should render a LinearProgress if loading is true and a second has passed', async () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<AutocompleteInput
{...{
...defaultProps,
loaded: true,
loading: true,
}}
/>
)}
/>
);

await new Promise(resolve => setTimeout(resolve, 1001));

expect(queryByRole('progressbar')).not.toBeNull();
});

it('should not render a LinearProgress if loading is false', () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<AutocompleteInput
{...{
...defaultProps,
}}
/>
)}
/>
);

expect(queryByRole('progressbar')).toBeNull();
});
});
61 changes: 61 additions & 0 deletions packages/ra-ui-materialui/src/input/CheckboxGroupInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,65 @@ describe('<CheckboxGroupInput />', () => {
expect(error.classList.contains('Mui-error')).toEqual(true);
});
});

// TODO: restore once master has been merged back to next
it.skip('should not render a LinearProgress if loading is true and a second has not passed yet', () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<CheckboxGroupInput
{...{
...defaultProps,
loaded: true,
loading: true,
}}
/>
)}
/>
);

expect(queryByRole('progressbar')).toBeNull();
});

it('should render a LinearProgress if loading is true and a second has passed', async () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<CheckboxGroupInput
{...{
...defaultProps,
loaded: true,
loading: true,
}}
/>
)}
/>
);

await new Promise(resolve => setTimeout(resolve, 1001));

expect(queryByRole('progressbar')).not.toBeNull();
});

it('should not render a LinearProgress if loading is false', () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<CheckboxGroupInput
{...{
...defaultProps,
}}
/>
)}
/>
);

expect(queryByRole('progressbar')).toBeNull();
});
});
61 changes: 61 additions & 0 deletions packages/ra-ui-materialui/src/input/RadioButtonGroupInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,65 @@ describe('<RadioButtonGroupInput />', () => {
expect(queryByText('Can I help you?')).toBeNull();
});
});

// TODO: restore once master has been merged back to next
it.skip('should not render a LinearProgress if loading is true and a second has not passed yet', () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<RadioButtonGroupInput
{...{
...defaultProps,
loaded: true,
loading: true,
}}
/>
)}
/>
);

expect(queryByRole('progressbar')).toBeNull();
});

it('should render a LinearProgress if loading is true and a second has passed', async () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<RadioButtonGroupInput
{...{
...defaultProps,
loaded: true,
loading: true,
}}
/>
)}
/>
);

await new Promise(resolve => setTimeout(resolve, 1001));

expect(queryByRole('progressbar')).not.toBeNull();
});

it('should not render a LinearProgress if loading is false', () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<RadioButtonGroupInput
{...{
...defaultProps,
}}
/>
)}
/>
);

expect(queryByRole('progressbar')).toBeNull();
});
});
61 changes: 61 additions & 0 deletions packages/ra-ui-materialui/src/input/SelectArrayInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,66 @@ describe('<SelectArrayInput />', () => {
);
expect(queryByText('Required field.')).toBeDefined();
});

// TODO: restore once master has been merged back to next
it.skip('should not render a LinearProgress if loading is true and a second has not passed yet', () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<SelectArrayInput
{...{
...defaultProps,
loaded: true,
loading: true,
}}
/>
)}
/>
);

expect(queryByRole('progressbar')).toBeNull();
});

it('should render a LinearProgress if loading is true and a second has passed', async () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<SelectArrayInput
{...{
...defaultProps,
loaded: true,
loading: true,
}}
/>
)}
/>
);

await new Promise(resolve => setTimeout(resolve, 1001));

expect(queryByRole('progressbar')).not.toBeNull();
});

it('should not render a LinearProgress if loading is false', () => {
const { queryByRole } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<SelectArrayInput
{...{
...defaultProps,
}}
/>
)}
/>
);

expect(queryByRole('progressbar')).toBeNull();
});
});
});
5 changes: 4 additions & 1 deletion packages/ra-ui-materialui/src/input/SelectArrayInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ const SelectArrayInput: FunctionComponent<SelectArrayInputProps> = props => {
const inputLabel = useRef(null);
const [labelWidth, setLabelWidth] = useState(0);
useEffect(() => {
setLabelWidth(inputLabel.current.offsetWidth);
// Will be null while loading and we don't need this fix in that case
if (inputLabel.current) {
setLabelWidth(inputLabel.current.offsetWidth);
}
}, []);

const { getChoiceText, getChoiceValue } = useChoices({
Expand Down

0 comments on commit a9c2833

Please sign in to comment.