1
+
2
+ // Define globals for eslint.
3
+ /* global describe it */
4
+
5
+ // Load dependencies
6
+ import should from 'should' ; // eslint-disable-line no-unused-vars
7
+ import { mockData } from './mockData' ;
8
+ import imageCacheHoc from '../lib/imageCacheHoc' ;
9
+ import { Image } from 'react-native' ;
10
+
11
+ describe ( 'CacheableImage' , function ( ) {
12
+
13
+ it ( 'Component property type validation should exist.' , ( ) => {
14
+
15
+ const CacheableImage = imageCacheHoc ( Image ) ;
16
+
17
+ Object . keys ( CacheableImage . propTypes ) . should . deepEqual ( [
18
+ 'fileHostWhitelist' ,
19
+ 'source' ,
20
+ 'permanent' ,
21
+ 'style'
22
+ ] ) ;
23
+
24
+ } ) ;
25
+
26
+ it ( '#constructor should initialize class object properties correctly.' , ( ) => {
27
+
28
+ const CacheableImage = imageCacheHoc ( Image ) ;
29
+
30
+ const cacheableImage = new CacheableImage ( mockData . mockCacheableImageProps ) ;
31
+
32
+ // Ensure defaults set correctly
33
+ cacheableImage . props . should . have . properties ( mockData . mockCacheableImageProps ) ;
34
+ cacheableImage . state . should . have . properties ( {
35
+ localFilePath : null
36
+ } ) ;
37
+ cacheableImage . options . should . have . properties ( {
38
+ validProtocols : [ 'https' ] ,
39
+ fileHostWhitelist : [ ] ,
40
+ cachePruneTriggerLimit : 15728640 ,
41
+ fileDirName : null
42
+ } ) ;
43
+ cacheableImage . fileSystem . should . have . properties ( {
44
+ os : 'ios' ,
45
+ cachePruneTriggerLimit : 15728640 ,
46
+ baseFilePath : mockData . basePath + '/react-native-image-cache-hoc/'
47
+ } ) ;
48
+
49
+ } ) ;
50
+
51
+ it ( '#_validateImageComponent should validate bad component props correctly.' , ( ) => {
52
+
53
+ // Verify source uri prop only accepts web accessible urls.
54
+
55
+ const CacheableImage = imageCacheHoc ( Image ) ;
56
+
57
+ try {
58
+
59
+ const cacheableImage = new CacheableImage ( { // eslint-disable-line no-unused-vars
60
+ source : {
61
+ uri : './local-file.jpg'
62
+ }
63
+ } ) ;
64
+
65
+ throw new Error ( 'Invalid source uri prop was accepted.' ) ;
66
+ } catch ( error ) {
67
+ error . should . deepEqual ( new Error ( 'Invalid source prop. <CacheableImage> props.source.uri should be a web accessible url.' ) ) ;
68
+ }
69
+
70
+ // Verify source uri prop only accepts web accessible urls from whitelist if whitelist set.
71
+
72
+ const CacheableImageWithOpts = imageCacheHoc ( Image , {
73
+ fileHostWhitelist : [ 'i.redd.it' ]
74
+ } ) ;
75
+
76
+ try {
77
+
78
+ const cacheableImageWithOpts = new CacheableImageWithOpts ( { // eslint-disable-line no-unused-vars
79
+ source : {
80
+ uri : 'https://www.google.com/logos/doodles/2017/day-of-the-dead-2017-6241959625621504-l.png'
81
+ }
82
+ } ) ;
83
+
84
+ throw new Error ( 'Invalid source uri prop was accepted.' ) ;
85
+ } catch ( error ) {
86
+ error . should . deepEqual ( new Error ( 'Invalid source prop. <CacheableImage> props.source.uri should be a web accessible url.' ) ) ;
87
+ }
88
+
89
+ // Verify source uri prop only accepts web accessible urls from correct protocols if protocol list set.
90
+
91
+ const CacheableImageWithProtocolOpts = imageCacheHoc ( Image , {
92
+ validProtocols : [ 'http' ]
93
+ } ) ;
94
+
95
+ try {
96
+
97
+ const cacheableImageWithProtocolOpts = new CacheableImageWithProtocolOpts ( { // eslint-disable-line no-unused-vars
98
+ source : {
99
+ uri : 'https://www.google.com/logos/doodles/2017/day-of-the-dead-2017-6241959625621504-l.png'
100
+ }
101
+ } ) ;
102
+
103
+ throw new Error ( 'Invalid source uri prop was accepted.' ) ;
104
+ } catch ( error ) {
105
+ error . should . deepEqual ( new Error ( 'Invalid source prop. <CacheableImage> props.source.uri should be a web accessible url.' ) ) ;
106
+ }
107
+
108
+ } ) ;
109
+
110
+ } ) ;
0 commit comments