File tree Expand file tree Collapse file tree 2 files changed +20
-5
lines changed
src/components/forms/FileInput Expand file tree Collapse file tree 2 files changed +20
-5
lines changed Original file line number Diff line number Diff line change 1
- import React from 'react'
1
+ import React , { StrictMode } from 'react'
2
2
import { fireEvent , render , waitFor } from '@testing-library/react'
3
3
4
4
import { FilePreview } from './FilePreview'
@@ -27,6 +27,16 @@ describe('FilePreview component', () => {
27
27
)
28
28
} )
29
29
30
+ it ( 'renders without errors when loaded multiple times (simulating react dev mode)' , ( ) => {
31
+ const { getByTestId } = render (
32
+ < StrictMode >
33
+ < FilePreview imageId = "" file = { TEST_TEXT_FILE } />
34
+ </ StrictMode >
35
+ )
36
+
37
+ expect ( getByTestId ( 'file-input-preview' ) ) . toBeInTheDocument ( )
38
+ } )
39
+
30
40
it ( 'renders a preview image' , async ( ) => {
31
41
const { getByTestId } = await waitFor ( ( ) =>
32
42
render ( < FilePreview { ...testProps } /> )
Original file line number Diff line number Diff line change @@ -19,18 +19,23 @@ export const FilePreview = ({
19
19
const [ isLoading , setIsLoading ] = useState ( true )
20
20
const [ previewSrc , setPreviewSrc ] = useState ( SPACER_GIF )
21
21
const [ showGenericPreview , setShowGenericPreview ] = useState ( false )
22
+ const firstRenderRef = useRef ( false )
22
23
23
24
useEffect ( ( ) => {
25
+ if ( firstRenderRef . current ) {
26
+ // already run, do nothing
27
+ return
28
+ }
29
+ // only run once
30
+ firstRenderRef . current = true
31
+
24
32
fileReaderRef . current . onloadend = ( ) : void => {
25
33
setIsLoading ( false )
26
34
setPreviewSrc ( fileReaderRef . current . result as string )
35
+ fileReaderRef . current . onloadend = null // is only run once
27
36
}
28
37
29
38
fileReaderRef . current . readAsDataURL ( file )
30
-
31
- return ( ) : void => {
32
- fileReaderRef . current . onloadend = null
33
- }
34
39
} , [ ] )
35
40
36
41
const { name } = file
You can’t perform that action at this time.
0 commit comments