Selaa lähdekoodia

#8 add test cases for stat and lstat

Ben Hsieh 8 vuotta sitten
vanhempi
commit
981018e20d
3 muutettua tiedostoa jossa 60 lisäystä ja 6 poistoa
  1. 24
    3
      src/fs.js
  2. 3
    1
      test/react-native-testkit/lib/comparer.js
  3. 33
    2
      test/test-fs.js

+ 24
- 3
src/fs.js Näytä tiedosto

@@ -163,7 +163,14 @@ function mkdir(path:string):Promise {
163 163
  * @return {RNFetchBlobFile}
164 164
  */
165 165
 function stat(path:string):Promise<RNFetchBlobFile> {
166
-
166
+  return new Promise((resolve, reject) => {
167
+    RNFetchBlob.stat(path, (err, stat) => {
168
+      if(err)
169
+        reject(err)
170
+      else
171
+        resolve(stat)
172
+    })
173
+  })
167 174
 }
168 175
 
169 176
 /**
@@ -175,7 +182,14 @@ function stat(path:string):Promise<RNFetchBlobFile> {
175 182
  * @return {Promise}
176 183
  */
177 184
 function scanFile(paths:Array<string>, mimes: Array<string>):Promise {
178
-
185
+  return new Promise((resolve, reject) => {
186
+    RNFetchBlob.scanFile(path, (err) => {
187
+      if(err)
188
+        reject(err)
189
+      else
190
+        resolve()
191
+    })
192
+  })
179 193
 }
180 194
 
181 195
 function cp(path:string, dest:string):Promise<boolean> {
@@ -201,7 +215,14 @@ function mv(path:string, dest:string):Promise<boolean> {
201 215
 }
202 216
 
203 217
 function lstat(path:string):Promise<Array<RNFetchBlobFile>> {
204
-
218
+  return new Promise((resolve, reject) => {
219
+    RNFetchBlob.lstat(path, (err, stat) => {
220
+      if(err)
221
+        reject(err)
222
+      else
223
+        resolve(stat)
224
+    })
225
+  })
205 226
 }
206 227
 
207 228
 function ls(path:string):Promise<Array<String>> {

+ 3
- 1
test/react-native-testkit/lib/comparer.js Näytä tiedosto

@@ -18,10 +18,12 @@ export default {
18 18
   isArray : (a, b) => Array.isArray(a),
19 19
   hasProperties : (a, b) => {
20 20
     let res = true
21
+    let c = 0
21 22
     for(let i in a) {
22 23
       let found = false
23 24
       for(let j in b) {
24
-        if(b[j] === i) {
25
+        c++
26
+        if(j === a[i]) {
25 27
           found = true
26 28
           break;
27 29
         }

+ 33
- 2
test/test-fs.js Näytä tiedosto

@@ -24,12 +24,13 @@ let dirs = null
24 24
 describe('Get storage folders', (report, done) => {
25 25
   fs.getSystemDirs().then((resp) => {
26 26
     dirs = resp
27
+    console.log(dirs)
27 28
     report(
28 29
       <Assert key="system folders should exists" expect={resp} comparer={Comparer.exists} />,
29 30
       <Assert key="check properties"
30
-        expect={resp}
31
+        expect={['DocumentDir', 'CacheDir']}
31 32
         comparer={Comparer.hasProperties}
32
-        actual={['DocumentDir', 'CacheDir', 'DCIMDir', 'DownloadDir']}
33
+        actual={dirs}
33 34
       />,
34 35
       <Info key="System Folders">
35 36
         <Text>{`${JSON.stringify(dirs)}`}</Text>
@@ -376,6 +377,36 @@ describe('format conversion', (report, done) => {
376 377
   })
377 378
 })
378 379
 
380
+describe('stat and lstat test', (report, done) => {
381
+  let p = ''
382
+  let dirs = null
383
+  let file = null
384
+  fs.getSystemDirs().then((resp) => {
385
+    dirs = resp
386
+    p = dirs.DocumentDir + '/' + 'ls-stat-test' + Date.now()
387
+    return fs.lstat(dirs.DocumentDir)
388
+  })
389
+  // stat a folder
390
+  .then((stat) => {
391
+    report(
392
+      <Assert key="result should be an array"
393
+        expect={true}
394
+        actual={Array.isArray(stat)}/>)
395
+    file = stat[0].path
396
+    return fs.stat(file)
397
+  })
398
+  .then((stat) => {
399
+    console.log(stat)
400
+    report(
401
+      <Assert key="should have properties"
402
+        expect={['size', 'type', 'lastModified', 'filename', 'path']}
403
+        comparer={Comparer.hasProperties}
404
+        actual={stat}/>)
405
+    done()
406
+  })
407
+
408
+})
409
+
379 410
 function getASCIIArray(str) {
380 411
   let r = []
381 412
   for(let i=0;i<str.length;i++) {