Skip to content

Commit a875a4e

Browse files
committed
[WIP] Migrate FileInput and related components to TypeScript
- [x] FileInputPreview - [x] ImageInputPreview - [ ] FileInput - [ ] ImageInput
1 parent 2489ac1 commit a875a4e

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

packages/ra-ui-materialui/src/input/FileInputPreview.spec.js packages/ra-ui-materialui/src/input/FileInputPreview.spec.tsx

+21-16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,21 @@ import { render, cleanup, fireEvent } from '@testing-library/react';
44
import FileInputPreview from './FileInputPreview';
55

66
describe('<FileInputPreview />', () => {
7-
afterEach(cleanup);
7+
beforeAll(() => {
8+
// @ts-ignore
9+
global.URL.revokeObjectURL = jest.fn();
10+
});
11+
12+
afterAll(() => {
13+
// @ts-ignore
14+
delete global.URL.revokeObjectURL;
15+
});
16+
17+
afterEach(() => {
18+
// @ts-ignore
19+
global.URL.revokeObjectURL.mockClear();
20+
cleanup();
21+
});
822

923
const file = {
1024
preview: 'previewUrl',
@@ -13,7 +27,6 @@ describe('<FileInputPreview />', () => {
1327
const defaultProps = {
1428
file,
1529
onRemove: jest.fn(),
16-
revokeObjectURL: jest.fn(),
1730
};
1831

1932
it('should call `onRemove` prop when clicking on remove button', () => {
@@ -41,36 +54,28 @@ describe('<FileInputPreview />', () => {
4154
});
4255

4356
it('should clean up generated URLs for preview', () => {
44-
const revokeObjectURL = jest.fn();
45-
4657
const { unmount } = render(
47-
<FileInputPreview
48-
{...defaultProps}
49-
revokeObjectURL={revokeObjectURL}
50-
>
58+
<FileInputPreview {...defaultProps}>
5159
<div id="child">Child</div>
5260
</FileInputPreview>
5361
);
5462

5563
unmount();
56-
expect(revokeObjectURL).toHaveBeenCalledWith('previewUrl');
64+
// @ts-ignore
65+
expect(global.URL.revokeObjectURL).toHaveBeenCalledWith('previewUrl');
5766
});
5867

5968
it('should not try to clean up preview urls if not passed a File object with a preview', () => {
6069
const file = {};
61-
const revokeObjectURL = jest.fn();
6270

6371
const { unmount } = render(
64-
<FileInputPreview
65-
{...defaultProps}
66-
file={file}
67-
revokeObjectURL={revokeObjectURL}
68-
>
72+
<FileInputPreview {...defaultProps} file={file}>
6973
<div id="child">Child</div>
7074
</FileInputPreview>
7175
);
7276

7377
unmount();
74-
expect(revokeObjectURL).not.toHaveBeenCalled();
78+
// @ts-ignore
79+
expect(global.URL.revokeObjectURL).not.toHaveBeenCalled();
7580
});
7681
});

packages/ra-ui-materialui/src/input/FileInputPreview.js packages/ra-ui-materialui/src/input/FileInputPreview.tsx

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect } from 'react';
1+
import React, { useEffect, ReactNode, FunctionComponent } from 'react';
22
import PropTypes from 'prop-types';
33
import { makeStyles } from '@material-ui/core';
44
import RemoveCircle from '@material-ui/icons/RemoveCircle';
@@ -12,11 +12,17 @@ const useStyles = makeStyles(theme => ({
1212
},
1313
}));
1414

15-
const FileInputPreview = ({
15+
interface Props {
16+
children: ReactNode;
17+
className?: string;
18+
onRemove: () => void;
19+
file: any;
20+
}
21+
22+
const FileInputPreview: FunctionComponent<Props> = ({
1623
children,
1724
className,
1825
onRemove,
19-
revokeObjectURL,
2026
file,
2127
...rest
2228
}) => {
@@ -28,12 +34,10 @@ const FileInputPreview = ({
2834
const preview = file.rawFile ? file.rawFile.preview : file.preview;
2935

3036
if (preview) {
31-
revokeObjectURL
32-
? revokeObjectURL(preview)
33-
: window.URL.revokeObjectURL(preview);
37+
window.URL.revokeObjectURL(preview);
3438
}
3539
};
36-
}, [file, revokeObjectURL]);
40+
}, [file]);
3741

3842
return (
3943
<div className={className} {...rest}>
@@ -55,7 +59,6 @@ FileInputPreview.propTypes = {
5559
className: PropTypes.string,
5660
file: PropTypes.object,
5761
onRemove: PropTypes.func.isRequired,
58-
revokeObjectURL: PropTypes.func,
5962
};
6063

6164
FileInputPreview.defaultProps = {

0 commit comments

Comments
 (0)