1
- import { loadImageResource } from "./imageResources.function" ;
1
+ import { fetchFromUrl , loadImageResource } from "./imageResources.function" ;
2
2
import { mockPartial } from "sneer" ;
3
3
import { ProviderRegistry } from "./provider/provider-registry.class" ;
4
4
import { ImageReader } from "./provider" ;
5
5
import { join } from "path" ;
6
+ import { ColorMode } from "./colormode.enum" ;
6
7
7
8
const loadMock = jest . fn ( ) ;
8
9
const providerRegistryMock = mockPartial < ProviderRegistry > ( {
@@ -25,4 +26,46 @@ describe('imageResources', () => {
25
26
// THEN
26
27
expect ( loadMock ) . toBeCalledWith ( join ( resourceDirectoryPath , imageFileName ) ) ;
27
28
} ) ;
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
+ } ) ;
28
71
} ) ;
0 commit comments