Skip to content

Commit

Permalink
added unit test for isImageUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
MaggieCabrera committed Feb 1, 2024
1 parent 9f1a65f commit 363109f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @return {boolean} true if the url is an image url.
*/
export default function isImageUrl( url ) {
const pattern = /(http(s?):)([/|.|\w|\s|-])*\.(?:jpg|gif|png)/g;
const pattern =
/^(https?:\/\/)([\w-]+(?:\.[\w-]+)*\/)*[\w-]+\.(?:jpg|JPG|jpeg|gif|png|webp)$/i;
return pattern.test( url );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Internal dependencies
*/
import isImageUrl from '../is-url-image';

describe( 'isImageUrl', () => {
// use .each to test multiple cases
it.each( [
[ true, 'https://example.com/image.jpg' ],
[ true, 'https://example.com/image.gif' ],
[ true, 'https://example.com/image.png' ],
[ true, 'https://example.com/image.webp' ],
[ true, 'https://example.com/image.jpeg' ],
[ true, 'https://example.com/image.JPG' ],
[ false, 'https://example.com/image.txt' ],
[ false, 'https://example.com/image' ],
[ false, 'https://example.com/image.jpg?query=123' ],
[ false, '' ],
[ false, null ],
[ false, undefined ],
[ false, 123 ],
] )(
'returns %s when testing against URL "%s" for a valid image',
( expected, testString ) => {
expect( isImageUrl( testString ) ).toBe( expected );
}
);
} );

0 comments on commit 363109f

Please sign in to comment.