Browse Source

#8 add test cases for stat and lstat

Ben Hsieh 8 years ago
parent
commit
981018e20d
3 changed files with 60 additions and 6 deletions
  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 View File

163
  * @return {RNFetchBlobFile}
163
  * @return {RNFetchBlobFile}
164
  */
164
  */
165
 function stat(path:string):Promise<RNFetchBlobFile> {
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
  * @return {Promise}
182
  * @return {Promise}
176
  */
183
  */
177
 function scanFile(paths:Array<string>, mimes: Array<string>):Promise {
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
 function cp(path:string, dest:string):Promise<boolean> {
195
 function cp(path:string, dest:string):Promise<boolean> {
201
 }
215
 }
202
 
216
 
203
 function lstat(path:string):Promise<Array<RNFetchBlobFile>> {
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
 function ls(path:string):Promise<Array<String>> {
228
 function ls(path:string):Promise<Array<String>> {

+ 3
- 1
test/react-native-testkit/lib/comparer.js View File

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

+ 33
- 2
test/test-fs.js View File

24
 describe('Get storage folders', (report, done) => {
24
 describe('Get storage folders', (report, done) => {
25
   fs.getSystemDirs().then((resp) => {
25
   fs.getSystemDirs().then((resp) => {
26
     dirs = resp
26
     dirs = resp
27
+    console.log(dirs)
27
     report(
28
     report(
28
       <Assert key="system folders should exists" expect={resp} comparer={Comparer.exists} />,
29
       <Assert key="system folders should exists" expect={resp} comparer={Comparer.exists} />,
29
       <Assert key="check properties"
30
       <Assert key="check properties"
30
-        expect={resp}
31
+        expect={['DocumentDir', 'CacheDir']}
31
         comparer={Comparer.hasProperties}
32
         comparer={Comparer.hasProperties}
32
-        actual={['DocumentDir', 'CacheDir', 'DCIMDir', 'DownloadDir']}
33
+        actual={dirs}
33
       />,
34
       />,
34
       <Info key="System Folders">
35
       <Info key="System Folders">
35
         <Text>{`${JSON.stringify(dirs)}`}</Text>
36
         <Text>{`${JSON.stringify(dirs)}`}</Text>
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
 function getASCIIArray(str) {
410
 function getASCIIArray(str) {
380
   let r = []
411
   let r = []
381
   for(let i=0;i<str.length;i++) {
412
   for(let i=0;i<str.length;i++) {