@@ -92,6 +92,61 @@ describe('CacheableImage', function () {
92
92
} )
93
93
} )
94
94
95
+ describe ( 'cacheLocalFile' , ( ) => {
96
+ it ( 'When local file exists, it should be copied to the cache' , async ( ) => {
97
+ const CacheableImage = imageCacheHoc ( Image )
98
+
99
+ const local = '/exists.png'
100
+ const url = 'https://example.com/exists.png'
101
+
102
+ const result = await CacheableImage . cacheLocalFile ( local , url )
103
+
104
+ expect ( result ) . toStrictEqual ( {
105
+ url : 'https://example.com/exists.png' ,
106
+ path :
107
+ '/base/file/path/react-native-image-cache-hoc/90c1be491d18ff2a7280039e9b65749461a65403.png' ,
108
+ } )
109
+
110
+ expect ( MockedRNFS . copyFile ) . toHaveBeenCalled ( )
111
+ } )
112
+
113
+ it ( 'When local file exists, it should be moved to the cache' , async ( ) => {
114
+ const CacheableImage = imageCacheHoc ( Image )
115
+
116
+ const local = '/exists.png'
117
+ const url = 'https://example.com/exists.png'
118
+
119
+ const result = await CacheableImage . cacheLocalFile ( local , url , true )
120
+
121
+ expect ( result ) . toStrictEqual ( {
122
+ url : 'https://example.com/exists.png' ,
123
+ path :
124
+ '/base/file/path/react-native-image-cache-hoc/90c1be491d18ff2a7280039e9b65749461a65403.png' ,
125
+ } )
126
+
127
+ expect ( MockedRNFS . moveFile ) . toHaveBeenCalled ( )
128
+ } )
129
+
130
+ it ( 'When local file does not exist, no fs operation should be performed' , async ( ) => {
131
+ MockedRNFS . exists . mockResolvedValueOnce ( false )
132
+
133
+ const CacheableImage = imageCacheHoc ( Image )
134
+
135
+ const local = '/missing.png'
136
+ const url = 'https://example.com/missing.png'
137
+
138
+ const result = await CacheableImage . cacheLocalFile ( local , url )
139
+
140
+ expect ( result ) . toStrictEqual ( {
141
+ url : 'https://example.com/missing.png' ,
142
+ path : null ,
143
+ } )
144
+
145
+ expect ( MockedRNFS . copyFile ) . not . toHaveBeenCalled ( )
146
+ expect ( MockedRNFS . moveFile ) . not . toHaveBeenCalled ( )
147
+ } )
148
+ } )
149
+
95
150
it ( '#flush static method should work as expected.' , ( ) => {
96
151
// Mock unlink to always be true.
97
152
MockedRNFS . unlink . mockResolvedValueOnce ( )
0 commit comments