File tree 2 files changed +23
-2
lines changed
2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -47,12 +47,24 @@ const App = () => {
47
47
setInput ( e . target . value ) ;
48
48
} ;
49
49
50
- const onImageSubmit = ( e ) => {
50
+ const onImageSubmit = async ( e ) => {
51
51
e . preventDefault ( ) ;
52
52
const sanitizedUrl = DOMPurify . sanitize ( input ) . trim ( ) ;
53
- setImageUrl ( sanitizedUrl ) ;
53
+ ( await isImageUrl ( sanitizedUrl ) ) && setImageUrl ( sanitizedUrl ) ;
54
54
} ;
55
55
56
+ function isImageUrl ( url ) {
57
+ return new Promise ( ( resolve ) => {
58
+ const img = new Image ( ) ;
59
+ img . onload = ( ) => resolve ( true ) ;
60
+ img . onerror = ( ) => {
61
+ alert ( 'Url is not an image' ) ;
62
+ resolve ( false ) ;
63
+ } ;
64
+ img . src = url ;
65
+ } ) ;
66
+ }
67
+
56
68
const onRouteChange = useCallback ( ( newRoute ) => {
57
69
if ( newRoute === 'signout' ) {
58
70
setIsSignedIn ( false ) ;
Original file line number Diff line number Diff line change @@ -79,6 +79,15 @@ describe('App component', () => {
79
79
} ) ;
80
80
81
81
test ( 'shows FindFace component after submitting an image URL' , async ( ) => {
82
+ global . Image = class {
83
+ constructor ( ) {
84
+ setTimeout ( ( ) => {
85
+ if ( this . onload ) this . onload ( ) ;
86
+ } , 0 ) ;
87
+ }
88
+ set src ( _ ) { }
89
+ } ;
90
+
82
91
renderApp ( ) ;
83
92
84
93
fireEvent . click ( screen . getByText ( / h o m e / i) ) ;
You can’t perform that action at this time.
0 commit comments