1- import { loadImageResource } from "./imageResources.function" ;
1+ import { fetchFromUrl , loadImageResource } from "./imageResources.function" ;
22import { mockPartial } from "sneer" ;
33import { ProviderRegistry } from "./provider/provider-registry.class" ;
44import { ImageReader } from "./provider" ;
55import { join } from "path" ;
6+ import { ColorMode } from "./colormode.enum" ;
67
78const loadMock = jest . fn ( ) ;
89const providerRegistryMock = mockPartial < ProviderRegistry > ( {
@@ -25,4 +26,46 @@ describe('imageResources', () => {
2526 // THEN
2627 expect ( loadMock ) . toBeCalledWith ( join ( resourceDirectoryPath , imageFileName ) ) ;
2728 } ) ;
29+ } ) ;
30+
31+ describe ( 'fetchFromUrl' , ( ) => {
32+ it ( 'should throw on malformed URLs' , async ( ) => {
33+ // GIVEN
34+ const malformedUrl = "foo" ;
35+
36+ // WHEN
37+ const SUT = ( ) => fetchFromUrl ( malformedUrl ) ;
38+
39+ // THEN
40+ await expect ( SUT ) . rejects . toThrowError ( "Invalid URL" ) ;
41+ } ) ;
42+
43+ it ( 'should throw on non-image URLs' , async ( ) => {
44+ // GIVEN
45+ const nonImageUrl = 'https://www.npmjs.com/package/jimp' ;
46+
47+ // WHEN
48+ const SUT = ( ) => fetchFromUrl ( nonImageUrl ) ;
49+
50+ // THEN
51+ await expect ( SUT ) . rejects . toThrowError ( 'Could not find MIME for Buffer' ) ;
52+ } ) ;
53+
54+ it ( 'should return an RGB image from a valid URL' , async ( ) => {
55+ // GIVEN
56+ const validImageUrl = 'https://github.com/nut-tree/nut.js/raw/master/.gfx/nut.png' ;
57+ const expectedDimensions = {
58+ width : 502 ,
59+ height : 411
60+ } ;
61+ const expectedColorMode = ColorMode . RGB ;
62+
63+ // WHEN
64+ const rgbImage = await fetchFromUrl ( validImageUrl ) ;
65+
66+ // THEN
67+ expect ( rgbImage . colorMode ) . toBe ( expectedColorMode ) ;
68+ expect ( rgbImage . width ) . toBe ( expectedDimensions . width ) ;
69+ expect ( rgbImage . height ) . toBe ( expectedDimensions . height ) ;
70+ } ) ;
2871} ) ;
0 commit comments