Browse Source

#8 test app refactor

Ben Hsieh 8 years ago
parent
commit
8ed91f0070
5 changed files with 298 additions and 244 deletions
  1. 4
    9
      test/index.test.js
  2. 123
    0
      test/test-0.1.x-0.4.x.js
  3. 116
    0
      test/test-0.5.x.js
  4. 55
    0
      test/test-init.js
  5. 0
    235
      test/tests.js

+ 4
- 9
test/index.test.js View File

@@ -1,6 +1,5 @@
1 1
 /**
2
- * Sample React Native App
3
- * https://github.com/facebook/react-native
2
+ * Sample RNTestkit App
4 3
  * @flow
5 4
  */
6 5
 import React, {Component} from 'react';
@@ -15,8 +14,7 @@ import {
15 14
 } from 'react-native';
16 15
 
17 16
 import RNTest from './react-native-testkit/'
18
-import TestContext from './tests'
19
-
17
+import './test-init'
20 18
 
21 19
 class fetchblob extends Component {
22 20
 
@@ -25,14 +23,11 @@ class fetchblob extends Component {
25 23
   }
26 24
 
27 25
   componentDidMount() {
28
-    TestContext.run(this)
26
+    RNTest.run(this)
29 27
   }
30 28
 
31 29
   render() {
32
-
33
-    return (
34
-      <RNTest.Reporter key="test-container" context={TestContext}/>
35
-    )
30
+    return <RNTest.Reporter />
36 31
   }
37 32
 
38 33
 }

+ 123
- 0
test/test-0.1.x-0.4.x.js View File

@@ -0,0 +1,123 @@
1
+import RNTest from './react-native-testkit/'
2
+import React from 'react'
3
+import RNFetchBlob from 'react-native-fetch-blob'
4
+
5
+import {
6
+  StyleSheet,
7
+  Text,
8
+  View,
9
+  ScrollView,
10
+  Dimensions,
11
+  Image,
12
+} from 'react-native';
13
+const { Assert, Comparer, Info, describe, prop } = RNTest
14
+
15
+let { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles, image } = prop()
16
+
17
+describe('The check if it follows 301/302 redirection', (report, done) => {
18
+
19
+  image = RNTest.prop('image')
20
+
21
+  RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/redirect`)
22
+  .then((resp) => {
23
+    report(
24
+      <Assert key="check image size" expect={image.length} actual={resp.base64().length}/>,
25
+      <Info key="Response image">
26
+        <Image
27
+          style={{width:Dimensions.get('window').width*0.9, height : Dimensions.get('window').width*0.9,margin :16}}
28
+          source={{uri : `data:image/png;base64, ${image}`}}/>
29
+      </Info>)
30
+      done()
31
+  })
32
+
33
+})
34
+
35
+describe('Upload octet-stream image to Dropbox', (report, done) => {
36
+
37
+  RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
38
+    Authorization : `Bearer ${DROPBOX_TOKEN}`,
39
+    'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+FILENAME+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
40
+    'Content-Type' : 'application/octet-stream',
41
+  }, image)
42
+  .then((resp) => {
43
+    resp = resp.json()
44
+    report(
45
+      <Assert key="confirm the file has been uploaded" expect={FILENAME} actual={resp.name}/>
46
+    )
47
+    done()
48
+  })
49
+
50
+})
51
+
52
+describe('Upload multipart/form-data', (report, done) => {
53
+
54
+  RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
55
+      Authorization : "Bearer fsXcpmKPrHgAAAAAAAAAEGxFXwhejXM_E8fznZoXPhHbhbNhA-Lytbe6etp1Jznz",
56
+      'Content-Type' : 'multipart/form-data',
57
+    }, [
58
+      { name : 'test-img', filename : 'test-img.png', data: image},
59
+      { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
60
+      { name : 'field1', data : 'hello !!'},
61
+      { name : 'field2', data : 'hello2 !!'}
62
+    ])
63
+  .then((resp) => {
64
+    resp = resp.json()
65
+    report(
66
+      <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
67
+      <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
68
+    )
69
+    done()
70
+  })
71
+
72
+
73
+})
74
+
75
+describe('Compare uploaded multipart image', (report, done) => {
76
+  let r1 = null
77
+  RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-img.png`)
78
+    .then((resp) => {
79
+      r1 = resp
80
+      return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-text.txt`)
81
+    })
82
+    .then((resp) => {
83
+      report(
84
+        <Assert key="check file length" expect={image.length} actual={r1.base64().length}/>,
85
+        <Assert key="check file content" expect={'hello.txt'} actual={resp.text()}/>
86
+      )
87
+      done()
88
+    })
89
+
90
+})
91
+
92
+// added after 0.4.2
93
+
94
+describe('Progress report test', (report, done) => {
95
+  let received = 0
96
+  RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/1mb-dummy`, {
97
+      Authorization : 'Bearer abde123eqweje'
98
+    })
99
+    .progress((written, total) => {
100
+      report(<Info key={`progress = ${written} bytes / ${total} bytes`}/>)
101
+      if(written === total)
102
+        report(<Assert key="progress goes to 100%" expect={written} actual={total}/>)
103
+    })
104
+    .then((resp) => {
105
+      report(<Assert key="response data should be correct event with progress listener"
106
+        expect={resp.text().substr(0,10)} actual={"1234567890"}/>)
107
+      done()
108
+    })
109
+
110
+})
111
+
112
+// FIXME : not yet supported
113
+describe('Large file download test', (report, done) => {
114
+  let received = 0
115
+  // RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`, {
116
+  //   Authorization : 'Bearer abde123eqweje'
117
+  // })
118
+  // .then((resp) => {
119
+    report(<Assert key="not supported" expect={true} actual={false}/>)
120
+    done()
121
+  // })
122
+
123
+})

+ 116
- 0
test/test-0.5.x.js View File

@@ -0,0 +1,116 @@
1
+import RNTest from './react-native-testkit/'
2
+import React from 'react'
3
+import RNFetchBlob from 'react-native-fetch-blob'
4
+
5
+import {
6
+  StyleSheet,
7
+  Text,
8
+  View,
9
+  ScrollView,
10
+  Dimensions,
11
+  Image,
12
+} from 'react-native';
13
+
14
+const { Assert, Comparer, Info, describe, prop } = RNTest
15
+const { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles } = prop()
16
+
17
+// added after 0.5.0
18
+
19
+describe('Get storage folders', (report, done) => {
20
+
21
+  RNFetchBlob.getSystemDirs().then((dirs) => {
22
+    report(
23
+      <Assert key="system folders should exists" expect={dirs} comparer={Comparer.exists} />,
24
+      <Assert key="check properties"
25
+        expect={dirs}
26
+        comparer={Comparer.hasProperties}
27
+        actual={['PictureDir', 'MovieDir', 'DocumentDir', 'CacheDir']}
28
+      />,
29
+      <Info key="System Folders">
30
+        <Text>{`${JSON.stringify(dirs)}`}</Text>
31
+      </Info>
32
+    )
33
+    done()
34
+  })
35
+
36
+})
37
+
38
+let tmpFilePath = null
39
+
40
+describe('Download file to storage with custom file extension', (report, done) => {
41
+
42
+  RNFetchBlob.config({
43
+      fileCache : true,
44
+      appendExt : 'png'
45
+    })
46
+    .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
47
+    .then((resp) => {
48
+      tmpFilePath = resp.path()
49
+      report(<Info key={`image from ${tmpFilePath}`}>
50
+        <Image source={{ uri : tmpFilePath}} style={styles.image}/>
51
+      </Info>)
52
+      done()
53
+    })
54
+})
55
+
56
+describe('Read cached file via file stream', (report, done) => {
57
+  let data = 'data:image/png;base64, '
58
+  let stream = RNFetchBlob.openReadStream(tmpFilePath, 'base64')
59
+  stream.onData((chunk) => {
60
+    data += chunk
61
+  })
62
+  stream.onEnd(() => {
63
+    report(
64
+      <Assert key="image should have value" expect={0} comparer={Comparer.smaller} actual={data.length}/>,
65
+      <Info key="image from read stream">
66
+        <Image source={{uri : data}} style={styles.image}/>
67
+      </Info>)
68
+    done()
69
+  })
70
+  stream.onError((err) => {
71
+    console.log('stream err', err)
72
+  })
73
+})
74
+
75
+describe('File stream reader error should be able to handled', (report, done) => {
76
+  let stream = RNFetchBlob.openReadStream('^_^ not exists', 'base64')
77
+  stream.onError((err) => {
78
+    report(<Info key="error message">
79
+      <Text>
80
+        {err}
81
+      </Text>
82
+    </Info>)
83
+    done()
84
+
85
+  })
86
+})
87
+
88
+describe('Upload from file storage', (report, done) => {
89
+  let filename = ''
90
+  let filepath = ''
91
+  RNFetchBlob.getSystemDirs().then((dirs) => {
92
+    filename = 'ios.5.0-' + Date.now() + '-from-storage.png'
93
+    filepath = dirs.DocumentDir + '/' + filename
94
+    return RNFetchBlob.config({ path : filepath })
95
+                      .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
96
+  })
97
+  .then((resp) => {
98
+      let path = resp.path()
99
+      return RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
100
+        Authorization : `Bearer ${DROPBOX_TOKEN}`,
101
+        'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
102
+        'Content-Type' : 'application/octet-stream',
103
+      }, 'RNFetchBlob-file://' + path)
104
+      .then((resp) => {
105
+        console.log(resp.text())
106
+        resp = resp.json()
107
+        report(
108
+          <Assert key="confirm the file has been uploaded" expect={filename} actual={resp.name}/>
109
+        )
110
+        done()
111
+      })
112
+  })
113
+
114
+
115
+
116
+})

+ 55
- 0
test/test-init.js View File

@@ -0,0 +1,55 @@
1
+import RNTest from './react-native-testkit/'
2
+import React from 'react'
3
+import RNFetchBlob from 'react-native-fetch-blob'
4
+
5
+import {
6
+  StyleSheet,
7
+  Text,
8
+  View,
9
+  ScrollView,
10
+  Dimensions,
11
+  Platform,
12
+  Dimension,
13
+  Image,
14
+} from 'react-native';
15
+
16
+const { Assert, Comparer, Info, describe, prop } = RNTest
17
+
18
+// test environment variables
19
+
20
+prop('FILENAME', `${Platform.OS}-0.5.0-${Date.now()}.png`)
21
+prop('TEST_SERVER_URL', 'http://192.168.0.14:8123')
22
+prop('DROPBOX_TOKEN', 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4')
23
+prop('styles', {
24
+  image : {
25
+    width: Dimensions.get('window').width*0.9,
26
+    height : Dimensions.get('window').width*0.9,
27
+    margin :16
28
+  }
29
+})
30
+
31
+const { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles, image } = prop()
32
+
33
+// init
34
+
35
+describe('GET image from server', (report, done) => {
36
+
37
+  RNFetchBlob
38
+    .fetch('GET', `${TEST_SERVER_URL}/public/github.png`, {
39
+      Authorization : 'Bearer abde123eqweje'
40
+    })
41
+    .then((resp) => {
42
+
43
+      RNTest.prop('image', resp.base64())
44
+      report(
45
+        <Info key="Response image">
46
+          <Image
47
+            style={styles.image}
48
+            source={{uri : `data:image/png;base64, ${prop('image')}`}}/>
49
+        </Info>)
50
+        done()
51
+    })
52
+})
53
+
54
+// require('./test-0.1.x-0.4.x')
55
+require('./test-0.5.x')

+ 0
- 235
test/tests.js View File

@@ -1,235 +0,0 @@
1
-import RNTest from './react-native-testkit/'
2
-import React from 'react'
3
-import RNFetchBlob from 'react-native-fetch-blob'
4
-
5
-import {
6
-  AppRegistry,
7
-  StyleSheet,
8
-  Text,
9
-  View,
10
-  Platform,
11
-  ScrollView,
12
-  Dimensions,
13
-  Image,
14
-} from 'react-native';
15
-
16
-const FILENAME = `${Platform.OS}-0.5.0-${Date.now()}.png`
17
-// paste your test config here
18
-const TEST_SERVER_URL = 'http://192.168.0.14:8123'
19
-const DROPBOX_TOKEN = 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4'
20
-
21
-const ctx = new RNTest.TestContext()
22
-const Assert = RNTest.Assert
23
-const Comparer = RNTest.Comparer
24
-const Info = RNTest.Info
25
-
26
-let image = null
27
-
28
-const styles = StyleSheet.create({
29
-  image : {
30
-    width:Dimensions.get('window').width*0.9,
31
-    height : Dimensions.get('window').width*0.9,
32
-    margin :16
33
-  }
34
-})
35
-
36
-ctx.describe('GET image from server', (report, done) => {
37
-  RNFetchBlob
38
-    .fetch('GET', `${TEST_SERVER_URL}/public/github.png`, {
39
-      Authorization : 'Bearer abde123eqweje'
40
-    })
41
-    .then((resp) => {
42
-      image = resp.base64()
43
-      report(
44
-        <Info key="Response image">
45
-          <Image
46
-            style={styles.image}
47
-            source={{uri : `data:image/png;base64, ${image}`}}/>
48
-        </Info>)
49
-        done()
50
-    })
51
-
52
-
53
-})
54
-
55
-// ctx.describe('The check if it follows 301/302 redirection', (report, done) => {
56
-//
57
-//   RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/redirect`)
58
-//   .then((resp) => {
59
-//     report(
60
-//       <Assert key="check image size" expect={image.length} actual={resp.base64().length}/>,
61
-//       <Info key="Response image">
62
-//         <Image
63
-//           style={{width:Dimensions.get('window').width*0.9, height : Dimensions.get('window').width*0.9,margin :16}}
64
-//           source={{uri : `data:image/png;base64, ${image}`}}/>
65
-//       </Info>)
66
-//       done()
67
-//   })
68
-//
69
-// })
70
-//
71
-// ctx.describe('Upload octet-stream image to Dropbox', (report, done) => {
72
-//
73
-//   RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
74
-//     Authorization : `Bearer ${DROPBOX_TOKEN}`,
75
-//     'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+FILENAME+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
76
-//     'Content-Type' : 'application/octet-stream',
77
-//   }, image)
78
-//   .then((resp) => {
79
-//     resp = resp.json()
80
-//     report(
81
-//       <Assert key="confirm the file has been uploaded" expect={FILENAME} actual={resp.name}/>
82
-//     )
83
-//     done()
84
-//   })
85
-//
86
-// })
87
-//
88
-// ctx.describe('Upload multipart/form-data', (report, done) => {
89
-//
90
-//   RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
91
-//       Authorization : "Bearer fsXcpmKPrHgAAAAAAAAAEGxFXwhejXM_E8fznZoXPhHbhbNhA-Lytbe6etp1Jznz",
92
-//       'Content-Type' : 'multipart/form-data',
93
-//     }, [
94
-//       { name : 'test-img', filename : 'test-img.png', data: image},
95
-//       { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
96
-//       { name : 'field1', data : 'hello !!'},
97
-//       { name : 'field2', data : 'hello2 !!'}
98
-//     ])
99
-//   .then((resp) => {
100
-//     resp = resp.json()
101
-//     report(
102
-//       <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
103
-//       <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
104
-//     )
105
-//     done()
106
-//   })
107
-//
108
-//
109
-// })
110
-//
111
-// ctx.describe('Compare uploaded multipart image', (report, done) => {
112
-//   let r1 = null
113
-//   RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-img.png`)
114
-//     .then((resp) => {
115
-//       r1 = resp
116
-//       return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-text.txt`)
117
-//     })
118
-//     .then((resp) => {
119
-//       report(
120
-//         <Assert key="check file length" expect={image.length} actual={r1.base64().length}/>,
121
-//         <Assert key="check file content" expect={'hello.txt'} actual={resp.text()}/>
122
-//       )
123
-//       done()
124
-//     })
125
-//
126
-// })
127
-
128
-// added after 0.4.2
129
-
130
-// ctx.describe('Progress report test', (report, done) => {
131
-//   let received = 0
132
-//   RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/1mb-dummy`, {
133
-//       Authorization : 'Bearer abde123eqweje'
134
-//     })
135
-//     .progress((written, total) => {
136
-//       report(<Info key={`progress = ${written} bytes / ${total} bytes`}/>)
137
-//       if(written === total)
138
-//         report(<Assert key="progress goes to 100%" expect={written} actual={total}/>)
139
-//     })
140
-//     .then((resp) => {
141
-//       report(<Assert key="response data should be correct event with progress listener"
142
-//         expect={resp.text().substr(0,10)} actual={"1234567890"}/>)
143
-//       done()
144
-//     })
145
-//
146
-// })
147
-
148
-// FIXME : not yet supported
149
-ctx.describe('Large file download test', (report, done) => {
150
-  let received = 0
151
-  // RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`, {
152
-  //   Authorization : 'Bearer abde123eqweje'
153
-  // })
154
-  // .then((resp) => {
155
-    report(<Assert key="not supported" expect={true} actual={false}/>)
156
-    done()
157
-  // })
158
-
159
-})
160
-
161
-// added after 0.5.0
162
-
163
-ctx.describe('Get storage folders', (report, done) => {
164
-
165
-  RNFetchBlob.getSystemDirs().then((dirs) => {
166
-    report(
167
-      <Assert key="system folders should exists" expect={dirs} comparer={Comparer.exists} />,
168
-      <Assert key="check properties"
169
-        expect={dirs}
170
-        comparer={Comparer.hasProperties}
171
-        actual={['PictureDir', 'MovieDir', 'DocumentDir', 'CacheDir']}
172
-      />,
173
-      <Info key="System Folders">
174
-        <Text>{`${JSON.stringify(dirs)}`}</Text>
175
-      </Info>
176
-    )
177
-    done()
178
-  })
179
-
180
-})
181
-
182
-let tmpFilePath = null
183
-
184
-ctx.describe('Download file to storage with custom file extension', (report, done) => {
185
-
186
-  RNFetchBlob.config({
187
-      fileCache : true,
188
-      appendExt : 'png'
189
-    })
190
-    .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
191
-    .then((resp) => {
192
-      tmpFilePath = resp.path()
193
-      report(<Info key={`image from ${tmpFilePath}`}>
194
-        <Image source={{ uri : tmpFilePath}} style={styles.image}/>
195
-      </Info>)
196
-      done()
197
-    })
198
-})
199
-
200
-ctx.describe('Read cached file via file stream', (report, done) => {
201
-  let data = 'data:image/png;base64, '
202
-  let stream = RNFetchBlob.openReadStream(tmpFilePath, 'base64')
203
-  stream.onData((chunk) => {
204
-    data += chunk
205
-  })
206
-  stream.onEnd(() => {
207
-    report(
208
-      <Assert key="image should have value" expect={0} comparer={Comparer.smaller} actual={data.length}/>,
209
-      <Info key="image from read stream">
210
-        <Image source={{uri : data}} style={styles.image}/>
211
-      </Info>)
212
-    done()
213
-  })
214
-  stream.onError((err) => {
215
-    console.log('stream err', err)
216
-  })
217
-
218
-})
219
-
220
-ctx.describe('File stream reader error should be able to handled', (report, done) => {
221
-
222
-  let stream = RNFetchBlob.openReadStream('^_^ not exists', 'base64')
223
-  stream.onError((err) => {
224
-    report(<Info key="error message">
225
-      <Text>
226
-        {err}
227
-      </Text>
228
-    </Info>)
229
-    done()
230
-
231
-  })
232
-
233
-})
234
-
235
-export default ctx