Browse Source

Add test cases for fs API #18

Ben Hsieh 8 years ago
parent
commit
38bc6b4324

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

4
   instanceOf : (a, b) => a instanceof b,
4
   instanceOf : (a, b) => a instanceof b,
5
   typeof : (a, b) => typeof a === b,
5
   typeof : (a, b) => typeof a === b,
6
   IsNull : (a, b) => a === null,
6
   IsNull : (a, b) => a === null,
7
-  exists : (a, b) => a,
7
+  exists : (a, b) => {
8
+    return a !== null && a !== void 0
9
+  },
8
   equalToArray : (a, b) => {
10
   equalToArray : (a, b) => {
9
     if(!Array.isArray(a) && Array.isArray(b))
11
     if(!Array.isArray(a) && Array.isArray(b))
10
       return false
12
       return false

+ 12
- 2
test/react-native-testkit/lib/test-context.js View File

22
    *         should return a promise.
22
    *         should return a promise.
23
    * @return {void}
23
    * @return {void}
24
    */
24
    */
25
-  static describe (desc:string, fn:Promise<any>) {
25
+  static describe (...args) {
26
     let { group, timeout, expand, run } = this || {}
26
     let { group, timeout, expand, run } = this || {}
27
-
27
+    let desc, config, fn
28
+    if([...args].length === 2) {
29
+      [desc, fn] = [...args]
30
+    }
31
+    else if ([...args].length === 3) {
32
+      [desc, config, fn] = [...args]
33
+      group = config.group || group
34
+      timeout = config.timeout || timeout
35
+      expand = config.expand || expand
36
+      run = config.run || run
37
+    }
28
     let ctx = {
38
     let ctx = {
29
       group : group || 'common',
39
       group : group || 'common',
30
       status : 'waiting',
40
       status : 'waiting',

+ 7
- 5
test/test-0.5.x.js View File

12
   Image,
12
   Image,
13
 } from 'react-native';
13
 } from 'react-native';
14
 
14
 
15
+const fs = RNFetchBlob.fs
15
 const { Assert, Comparer, Info, prop } = RNTest
16
 const { Assert, Comparer, Info, prop } = RNTest
16
 const describe = RNTest.config({
17
 const describe = RNTest.config({
17
   group : '0.5.x',
18
   group : '0.5.x',
19
+  run : false,
18
   expand : false,
20
   expand : false,
19
 })
21
 })
20
 const { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles } = prop()
22
 const { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles } = prop()
45
 
47
 
46
 describe('Read cached file via file stream', (report, done) => {
48
 describe('Read cached file via file stream', (report, done) => {
47
   let data = 'data:image/png;base64, '
49
   let data = 'data:image/png;base64, '
48
-  let stream = RNFetchBlob.readStream(tmpFilePath, 'base64')
50
+  let stream = fs.readStream(tmpFilePath, 'base64')
49
   stream.onData((chunk) => {
51
   stream.onData((chunk) => {
50
     data += chunk
52
     data += chunk
51
   })
53
   })
66
 })
68
 })
67
 
69
 
68
 describe('File stream reader error should be able to handled', (report, done) => {
70
 describe('File stream reader error should be able to handled', (report, done) => {
69
-  let stream = RNFetchBlob.readStream('^_^ not exists', 'base64')
71
+  let stream = fs.readStream('^_^ not exists', 'base64')
70
   stream.onError((err) => {
72
   stream.onError((err) => {
71
     report(<Info key="error message">
73
     report(<Info key="error message">
72
       <Text>
74
       <Text>
84
 describe('Upload from file storage', (report, done) => {
86
 describe('Upload from file storage', (report, done) => {
85
   let filename = ''
87
   let filename = ''
86
   let filepath = ''
88
   let filepath = ''
87
-  RNFetchBlob.getSystemDirs().then((dirs) => {
89
+  fs.getSystemDirs().then((dirs) => {
88
     sysDirs = dirs
90
     sysDirs = dirs
89
     filename = Platform.OS + '0.5.0-' + Date.now() + '-from-storage.png'
91
     filename = Platform.OS + '0.5.0-' + Date.now() + '-from-storage.png'
90
     filepath = dirs.DocumentDir + '/' + filename
92
     filepath = dirs.DocumentDir + '/' + filename
176
 
178
 
177
   let sessionName = 'test-session-' + Date.now()
179
   let sessionName = 'test-session-' + Date.now()
178
   let baseDir = sysDirs.DocumentDir + '/' + sessionName
180
   let baseDir = sysDirs.DocumentDir + '/' + sessionName
179
-  RNFetchBlob.mkdir(sysDirs.DocumentDir + '/' + sessionName).then(() => {
181
+  fs.mkdir(sysDirs.DocumentDir + '/' + sessionName).then(() => {
180
     let promises = [0,1,2,3,4,5,6,7,8,9].map((p) => {
182
     let promises = [0,1,2,3,4,5,6,7,8,9].map((p) => {
181
       return RNFetchBlob.config({
183
       return RNFetchBlob.config({
182
           session : sessionName,
184
           session : sessionName,
214
         actual={s.list()}/>)
216
         actual={s.list()}/>)
215
 
217
 
216
     s.dispose().then(() => {
218
     s.dispose().then(() => {
217
-      RNFetchBlob.ls(baseDir).then((lsRes) => {
219
+      fs.ls(baseDir).then((lsRes) => {
218
         report(
220
         report(
219
           <Assert
221
           <Assert
220
             key="dispose() should work correctly"
222
             key="dispose() should work correctly"

+ 253
- 4
test/test-fs.js View File

11
   Image,
11
   Image,
12
 } from 'react-native';
12
 } from 'react-native';
13
 const { Assert, Comparer, Info, prop } = RNTest
13
 const { Assert, Comparer, Info, prop } = RNTest
14
+const fs = RNFetchBlob.fs
14
 const describe = RNTest.config({
15
 const describe = RNTest.config({
15
   group : 'fs',
16
   group : 'fs',
16
   expand : false,
17
   expand : false,
18
 })
19
 })
19
 
20
 
20
 let { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles, image } = prop()
21
 let { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles, image } = prop()
22
+let dirs = null
21
 
23
 
22
 describe('Get storage folders', (report, done) => {
24
 describe('Get storage folders', (report, done) => {
23
-
24
-  RNFetchBlob.getSystemDirs().then((dirs) => {
25
+  fs.getSystemDirs().then((resp) => {
26
+    dirs = resp
25
     report(
27
     report(
26
-      <Assert key="system folders should exists" expect={dirs} comparer={Comparer.exists} />,
28
+      <Assert key="system folders should exists" expect={resp} comparer={Comparer.exists} />,
27
       <Assert key="check properties"
29
       <Assert key="check properties"
28
-        expect={dirs}
30
+        expect={resp}
29
         comparer={Comparer.hasProperties}
31
         comparer={Comparer.hasProperties}
30
         actual={['DocumentDir', 'CacheDir', 'DCIMDir', 'DownloadDir']}
32
         actual={['DocumentDir', 'CacheDir', 'DCIMDir', 'DownloadDir']}
31
       />,
33
       />,
37
   })
39
   })
38
 
40
 
39
 })
41
 })
42
+
43
+describe('ls API test', (report, done) => {
44
+  fs.ls(dirs.DocumentDir).then((list) => {
45
+    report(<Assert key="result must be an Array" expect={true} actual={Array.isArray(list)} />)
46
+    return fs.ls('hh87h8uhi')
47
+  })
48
+  .then(()=>{})
49
+  .catch((err) => {
50
+    report(<Assert key="Wrong path should have error"
51
+      expect={err}
52
+      comparer={Comparer.exists}/>)
53
+    done()
54
+  })
55
+})
56
+
57
+describe('exists API test', (report, done) => {
58
+  let exists = fs.exists
59
+  exists(dirs.DocumentDir).then((exist, isDir) => {
60
+    report(
61
+      <Assert key="document dir should exist" expect={true} actual={exist}/>
62
+    )
63
+    return exists('blabajsdio')
64
+  })
65
+  .then((exist, isDir) => {
66
+    report(
67
+      <Assert key="path should not exist" expect={false} actual={exist}/>
68
+    )
69
+    done()
70
+  })
71
+})
72
+
73
+describe('create file API test', (report, done) => {
74
+  let p = dirs.DocumentDir + '/test-' + Date.now()
75
+  let raw = 'hello ' + Date.now()
76
+  let base64 = RNFetchBlob.base64.encode(raw)
77
+
78
+  fs.createFile(p, raw, 'utf8')
79
+    .then(() => {
80
+      let stream = fs.readStream(p, 'utf8')
81
+      let d = ''
82
+      stream.onData((chunk) => {
83
+        d += chunk
84
+      })
85
+      stream.onEnd(() => {
86
+        report(<Assert key="utf8 content test"  expect={raw} actual={d}/>)
87
+        testBase64()
88
+      })
89
+    })
90
+  function testBase64() {
91
+    fs.createFile(p + '-base64', RNFetchBlob.base64.encode(raw), 'base64')
92
+      .then(() => {
93
+        let stream = fs.readStream(p + '-base64', 'utf8')
94
+        let d = ''
95
+        stream.onData((chunk) => {
96
+          d += chunk
97
+        })
98
+        stream.onEnd(() => {
99
+          report(<Assert
100
+            key="base64 content test"
101
+            expect={raw}
102
+            actual={d}/>)
103
+          testASCII()
104
+        })
105
+      })
106
+      .catch((err) => {
107
+        console.log(err)
108
+      })
109
+  }
110
+  function testASCII() {
111
+    fs.createFile(p + '-ascii', raw, 'ascii')
112
+      .then(() => {
113
+        let stream = fs.readStream(p + '-ascii', 'ascii')
114
+        let d = ''
115
+        stream.onData((chunk) => {
116
+          d += chunk
117
+        })
118
+        stream.onEnd(() => {
119
+          report(<Assert
120
+            key="ASCII content test"
121
+            expect={raw}
122
+            actual={d}/>)
123
+          done()
124
+        })
125
+      })
126
+      .catch((err) => {
127
+        console.log(err)
128
+      })
129
+  }
130
+
131
+})
132
+
133
+describe('mkdir and isDir API test', (report, done) => {
134
+  let p = dirs.DocumentDir + '/mkdir-test-' + Date.now()
135
+  fs.mkdir(p + '/mkdir').then((err) => {
136
+    report(<Assert key="folder should be created without error"
137
+      expect={undefined}
138
+      actual={err} />)
139
+    return fs.isDir(p + '/mkdir')
140
+  })
141
+  .then((isDir) => {
142
+    report(<Assert key="isDir should work correctly" expect={true} actual={isDir} />)
143
+    return fs.mkdir(p + '/mkdir')
144
+  })
145
+  .then()
146
+  .catch((err) => {
147
+    report(<Assert key="isDir should not work when folder exists"
148
+      expect={err}
149
+      comparer={Comparer.hasValue}/>)
150
+    done()
151
+  })
152
+})
153
+
154
+describe('unlink and mkdir API test', (report, done) => {
155
+  let p = dirs.DocumentDir + '/unlink-test-' + Date.now()
156
+  fs.createFile(p, 'write' + Date.now(), 'utf8').then(() => {
157
+    return fs.exists(p)
158
+  })
159
+  .then((exist) => {
160
+    report(<Assert key="file created" expect={true} actual={exist} />)
161
+    return fs.unlink(p).then(() => {
162
+      return fs.exists(p)
163
+    })
164
+  })
165
+  .then((exist) => {
166
+    report(<Assert key="file removed" expect={false} actual={exist} />)
167
+    return fs.mkdir(p + '/dir')
168
+  })
169
+  .then((err) => fs.exists(p + '/dir'))
170
+  .then((exist) => {
171
+    report(<Assert key="mkdir should success" expect={true} actual={exist} />)
172
+    return fs.unlink(p + '/dir')
173
+  })
174
+  .then(() => fs.exists(p + '/dir'))
175
+  .then((exist) => {
176
+    report(<Assert key="folder should be removed" expect={false} actual={exist} />)
177
+    done()
178
+  })
179
+})
180
+
181
+describe('write stream API test', (report, done) => {
182
+
183
+  let p = dirs.DocumentDir + '/write-stream' + Date.now()
184
+  let expect = ''
185
+  fs.createFile(p, '1234567890', 'utf8')
186
+    .then(() => fs.writeStream(p, 'utf8', true))
187
+    .then((ws) => {
188
+      ws.write('11')
189
+      ws.write('12')
190
+      ws.write('13')
191
+      ws.write('14')
192
+      return ws.close()
193
+    })
194
+    .then(() => {
195
+      let rs = fs.readStream(p, 'utf8')
196
+      let d1 = ''
197
+      rs.onData((chunk) => {
198
+        d1 += chunk
199
+      })
200
+      rs.onEnd(() => {
201
+        report(
202
+          <Assert key="write data async test"
203
+            expect={'123456789011121314'}
204
+            actual={d1}/>)
205
+          base64Test()
206
+      })
207
+    })
208
+  function base64Test() {
209
+    fs.writeStream(p, 'base64', false)
210
+    .then((ws) => {
211
+      for(let i = 0; i< 100; i++) {
212
+        expect += RNFetchBlob.base64.encode(i)
213
+        ws.write(RNFetchBlob.base64.encode(i))
214
+      }
215
+      return ws.close()
216
+    })
217
+    .then(() => {
218
+      let rs = fs.readStream(p, 'base64')
219
+      let d2 = ''
220
+      rs.onData((chunk) => {
221
+        d2 += chunk
222
+      })
223
+      rs.onEnd(() => {
224
+        report(
225
+          <Assert key="file should be overwritten by base64 encoded data"
226
+            expect={expect}
227
+            actual={RNFetchBlob.base64.decode(d2)} />)
228
+        done()
229
+      })
230
+    })
231
+  }
232
+})
233
+
234
+describe('mv API test', {timeout : 10000},(report, done) => {
235
+  let p = dirs.DocumentDir + '/mvTest' + Date.now()
236
+  let dest = p + '-dest-' + Date.now()
237
+  let content = Date.now() + '-test'
238
+  fs.createFile(p, content, 'utf8')
239
+  .then(() => fs.mkdir(dest))
240
+  .then(() => fs.mv(p, dest +'/moved'))
241
+  .then(() => fs.exists(p))
242
+  .then((exist) => {
243
+    report(<Assert key="file should not exist in old path" expect={false} actual={exist}/>)
244
+    return fs.exists(dest + '/moved')
245
+  })
246
+  .then((exist) => {
247
+    report(<Assert key="file should be moved to destination" expect={true} actual={exist}/>)
248
+    return fs.ls(dest)
249
+  })
250
+  .then((files) => {
251
+    report(<Assert key="file name should be correct" expect={'moved'} actual={files[0]}/>)
252
+    let rs = fs.readStream(dest + '/moved')
253
+    let actual = ''
254
+    rs.onData((chunk) => {
255
+      actual += chunk
256
+    })
257
+    rs.onEnd(() => {
258
+      report(<Assert key="file content should be correct" expect={content} actual={actual}/>)
259
+      done()
260
+    })
261
+  })
262
+})
263
+
264
+describe('cp API test', {timeout : 10000},(report, done) => {
265
+  let p = dirs.DocumentDir + '/cpTest' + Date.now()
266
+  let dest = p + '-dest-' + Date.now()
267
+  let content = Date.now() + '-test'
268
+  fs.createFile(p, content, 'utf8')
269
+  .then(() => fs.mkdir(dest))
270
+  .then(() => fs.cp(p, dest +'/cp'))
271
+  .then(() => fs.exists(dest +'/cp'))
272
+  .then((exist) => {
273
+    report(<Assert key="file should be copy to destination" expect={true} actual={exist}/>)
274
+    return fs.ls(dest)
275
+  })
276
+  .then((files) => {
277
+    report(<Assert key="file name should be correct" expect={'cp'} actual={files[0]}/>)
278
+    let rs = fs.readStream(dest + '/cp')
279
+    let actual = ''
280
+    rs.onData((chunk) => {
281
+      actual += chunk
282
+    })
283
+    rs.onEnd(() => {
284
+      report(<Assert key="file content should be correct" expect={content} actual={actual}/>)
285
+      done()
286
+    })
287
+  })
288
+})

+ 2
- 1
test/test-init.js View File

18
 // test environment variables
18
 // test environment variables
19
 
19
 
20
 prop('FILENAME', `${Platform.OS}-0.5.0-${Date.now()}.png`)
20
 prop('FILENAME', `${Platform.OS}-0.5.0-${Date.now()}.png`)
21
-prop('TEST_SERVER_URL', 'http://192.168.0.14:8123')
21
+prop('TEST_SERVER_URL', 'http://192.168.16.70:8123')
22
 prop('DROPBOX_TOKEN', 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4')
22
 prop('DROPBOX_TOKEN', 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4')
23
 prop('styles', {
23
 prop('styles', {
24
   image : {
24
   image : {
51
     })
51
     })
52
 })
52
 })
53
 
53
 
54
+require('./test-fs')
54
 require('./test-0.1.x-0.4.x')
55
 require('./test-0.1.x-0.4.x')
55
 require('./test-0.5.x')
56
 require('./test-0.5.x')