Bladeren bron

Add test case for #8 redirection responses verify.

Ben Hsieh 8 jaren geleden
bovenliggende
commit
0099ae11cd

+ 94
- 0
test/.flowconfig Bestand weergeven

@@ -0,0 +1,94 @@
1
+[ignore]
2
+
3
+# We fork some components by platform.
4
+.*/*.web.js
5
+.*/*.android.js
6
+
7
+# Some modules have their own node_modules with overlap
8
+.*/node_modules/node-haste/.*
9
+
10
+# Ugh
11
+.*/node_modules/babel.*
12
+.*/node_modules/babylon.*
13
+.*/node_modules/invariant.*
14
+
15
+# Ignore react and fbjs where there are overlaps, but don't ignore
16
+# anything that react-native relies on
17
+.*/node_modules/fbjs/lib/Map.js
18
+.*/node_modules/fbjs/lib/ErrorUtils.js
19
+
20
+# Flow has a built-in definition for the 'react' module which we prefer to use
21
+# over the currently-untyped source
22
+.*/node_modules/react/react.js
23
+.*/node_modules/react/lib/React.js
24
+.*/node_modules/react/lib/ReactDOM.js
25
+
26
+.*/__mocks__/.*
27
+.*/__tests__/.*
28
+
29
+.*/commoner/test/source/widget/share.js
30
+
31
+# Ignore commoner tests
32
+.*/node_modules/commoner/test/.*
33
+
34
+# See https://github.com/facebook/flow/issues/442
35
+.*/react-tools/node_modules/commoner/lib/reader.js
36
+
37
+# Ignore jest
38
+.*/node_modules/jest-cli/.*
39
+
40
+# Ignore Website
41
+.*/website/.*
42
+
43
+# Ignore generators
44
+.*/local-cli/generator.*
45
+
46
+# Ignore BUCK generated folders
47
+.*\.buckd/
48
+
49
+.*/node_modules/is-my-json-valid/test/.*\.json
50
+.*/node_modules/iconv-lite/encodings/tables/.*\.json
51
+.*/node_modules/y18n/test/.*\.json
52
+.*/node_modules/spdx-license-ids/spdx-license-ids.json
53
+.*/node_modules/spdx-exceptions/index.json
54
+.*/node_modules/resolve/test/subdirs/node_modules/a/b/c/x.json
55
+.*/node_modules/resolve/lib/core.json
56
+.*/node_modules/jsonparse/samplejson/.*\.json
57
+.*/node_modules/json5/test/.*\.json
58
+.*/node_modules/ua-parser-js/test/.*\.json
59
+.*/node_modules/builtin-modules/builtin-modules.json
60
+.*/node_modules/binary-extensions/binary-extensions.json
61
+.*/node_modules/url-regex/tlds.json
62
+.*/node_modules/joi/.*\.json
63
+.*/node_modules/isemail/.*\.json
64
+.*/node_modules/tr46/.*\.json
65
+
66
+
67
+[include]
68
+
69
+[libs]
70
+node_modules/react-native/Libraries/react-native/react-native-interface.js
71
+node_modules/react-native/flow
72
+flow/
73
+
74
+[options]
75
+module.system=haste
76
+
77
+esproposal.class_static_fields=enable
78
+esproposal.class_instance_fields=enable
79
+
80
+munge_underscores=true
81
+
82
+module.name_mapper='^image![a-zA-Z0-9$_-]+$' -> 'GlobalImageStub'
83
+module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> 'RelativeImageStub'
84
+
85
+suppress_type=$FlowIssue
86
+suppress_type=$FlowFixMe
87
+suppress_type=$FixMe
88
+
89
+suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(>=0\\.\\(2[0-4]\\|1[0-9]\\|[0-9]\\).[0-9]\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)
90
+suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(>=0\\.\\(2[0-4]\\|1[0-9]\\|[0-9]\\).[0-9]\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)?:? #[0-9]+
91
+suppress_comment=\\(.\\|\n\\)*\\$FlowFixedInNextDeploy
92
+
93
+[version]
94
+0.24.0

test/in-app-test-runner/assert.js → test/in-app-test-runner/components/assert.js Bestand weergeven

@@ -1,4 +1,3 @@
1
-// @flow
2 1
 import React, {Component} from 'react';
3 2
 import {
4 3
   AppRegistry,
@@ -14,7 +13,9 @@ export default class Assert extends Component {
14 13
 
15 14
   props : {
16 15
     expect : any,
17
-    actual : any
16
+    actual : any,
17
+    desc : any,
18
+    compaer : () => bool
18 19
   };
19 20
 
20 21
   constructor(props) {
@@ -33,22 +34,32 @@ export default class Assert extends Component {
33 34
         <Text style={{
34 35
           color : this.getAssertion() ? '#00a825' : '#ff0d0d',
35 36
           margin : 8,
37
+          marginTop : 0,
36 38
           marginLeft : 0
37
-        }}>{ this.getAssertion() ? '✅' : '×' } Assertion description</Text>
39
+        }}>{ this.getAssertion() ? '✅' : '×' } {this.props.desc}</Text>
38 40
         <Text style={{flex : 1, flexDirection : 'row'}}>
39 41
           <Text style={{ color : '#AAA'}}>expect </Text>
40
-          <Text style={{flex : 1}}>{this.props.expect}</Text>
42
+          <Text style={{flex : 1}}>{String(this.props.expect)}</Text>
41 43
         </Text>
42 44
         <Text style={{flex : 1, flexDirection : 'row'}}>
43 45
           <Text style={{color : '#AAA'}}>actual  </Text>
44
-          <Text style={{flex : 1}}>{this.props.actual}</Text>
46
+          <Text style={{flex : 1}}>{String(this.props.actual)}</Text>
45 47
         </Text>
48
+        {this.props.comparer ? <Text style={{flex : 1, flexDirection : 'row'}}>
49
+          <Text style={{color : '#AAA'}}>comparer = </Text>
50
+          <Text style={{flex : 1}}>
51
+            { String(this.props.comparer) }
52
+          </Text>
53
+        </Text> : null}
46 54
       </View>
47 55
     )
48 56
   }
49 57
 
50 58
   getAssertion() {
51
-    return this.props.expect === this.props.actual
59
+    if(this.props.comparer)
60
+      return this.props.comparer(this.props.expect, this.props.actual)
61
+    else
62
+      return this.props.expect === this.props.actual
52 63
   }
53 64
 
54 65
 }

test/in-app-test-runner/detail.js → test/in-app-test-runner/components/detail.js Bestand weergeven


test/in-app-test-runner/info.js → test/in-app-test-runner/components/info.js Bestand weergeven

@@ -1,4 +1,3 @@
1
-// @flow
2 1
 import React, {Component} from 'react';
3 2
 import {
4 3
   AppRegistry,
@@ -10,8 +9,8 @@ import {
10 9
 export default class Info extends Component {
11 10
 
12 11
   props : {
13
-    description : string,
14
-  }
12
+    desc : string,
13
+  };
15 14
 
16 15
   render() {
17 16
     return (
@@ -22,10 +21,10 @@ export default class Info extends Component {
22 21
       }}>
23 22
         <View style={{ alignSelf : 'stretch'}}>
24 23
           <Text style={{color : '#777', alignSelf : 'stretch', textAlign : 'center', margin : 8}}>
25
-            {this.props.description}
24
+            {this.props.desc}
26 25
           </Text>
27 26
         </View>
28
-        <View style={{alignSelf : 'stretch'}}>{this.props.children}</View>
27
+        <View style={{margin : 8, alignSelf : 'stretch'}}>{this.props.children}</View>
29 28
       </View>
30 29
     )
31 30
   }

test/in-app-test-runner/reporter.js → test/in-app-test-runner/components/reporter.js Bestand weergeven

@@ -13,6 +13,10 @@ import Assert from './assert.js'
13 13
 
14 14
 export default class Reporter extends Component {
15 15
 
16
+  props : {
17
+    context : TestContext
18
+  };
19
+
16 20
   render() {
17 21
     return (
18 22
       <ScrollView key="rn-test-scroller" style={styles.container}>
@@ -26,14 +30,18 @@ export default class Reporter extends Component {
26 30
       let pass = true
27 31
       let foundAssertions = false
28 32
 
29
-      Array.isArray(t.result) && t.result.forEach((r) => {
30
-        if(r.type.name === 'Assert') {
31
-          foundAssertions = true
32
-          pass = pass && (r.props.actual === r.props.expect)
33
-        }
34
-      })
33
+      if(Array.isArray(t.result)) {
34
+        t.result = t.result.map((r) => {
35
+          if(r.type.name === 'Assert') {
36
+            foundAssertions = true
37
+            let comp = r.props.comparer ? r.props.comparer(r.props.expect, r.props.actual) : (r.props.actual === r.props.expect)
38
+            pass = pass && comp
39
+          }
40
+          return React.cloneElement(r, {desc : r.key})
41
+        })
42
+      }
35 43
 
36
-      t.status = foundAssertions ? (pass ? 'pass' : 'fail') : t.status
44
+      t.status = foundAssertions ? (pass ? 'pass' : 'fail') : 'pass'
37 45
 
38 46
       return (<View key={'rn-test-' + t.desc} style={{
39 47
         borderBottomWidth : 1.5,
@@ -43,8 +51,8 @@ export default class Reporter extends Component {
43 51
           alignItems : 'center',
44 52
           flexDirection : 'row'
45 53
         }}>
46
-          <Text style={[styles.badge, {flex : 1, borderWidth : 0}]}>{t.desc}</Text>
47
-          <Text style={[styles.badge, this.getBadge(t.status)]}>{t.status}</Text>
54
+          <Text style={[styles.badge, {flex : 1, borderWidth : 0, textAlign : 'left'}]}>{t.desc}</Text>
55
+          <Text style={[styles.badge, this.getBadge(t.status)]}>{t.status ? 'pass' : 'fail'}</Text>
48 56
         </View>
49 57
         <View key={t.desc + '-result'} style={{backgroundColor : '#F4F4F4'}}>
50 58
           {t.result}
@@ -53,9 +61,9 @@ export default class Reporter extends Component {
53 61
     })
54 62
   }
55 63
 
56
-  getBadge(status) {
64
+  getBadge(status: 'running' | 'pass' | 'fail') {
57 65
     if(status === 'running')
58
-      return styles.badgeRunning
66
+      return styles.badgeWaiting
59 67
     else if(status === 'pass')
60 68
       return styles.badgePass
61 69
     else
@@ -74,12 +82,13 @@ const styles = StyleSheet.create({
74 82
     padding : 4,
75 83
     borderRadius : 4,
76 84
     borderWidth : 2,
85
+    textAlign : 'center'
77 86
   },
78 87
   badgePass: {
79 88
     borderColor : '#00a825',
80 89
     color : '#00a825'
81 90
   },
82
-  badgeRunning: {
91
+  badgeWaiting: {
83 92
     borderColor : '#e3c423',
84 93
     color : '#e3c423'
85 94
   },

+ 7
- 5
test/in-app-test-runner/index.js Bestand weergeven

@@ -1,11 +1,13 @@
1
-import TestContext from './app-test'
2
-import Reporter from './reporter'
3
-import Assert from './assert'
4
-import Info from './info'
1
+import TestContext from './lib/test-context'
2
+import Comparer from './lib/comparer'
3
+import Reporter from './components/reporter'
4
+import Assert from './components/assert'
5
+import Info from './components/info'
5 6
 
6 7
 export default {
7 8
   TestContext,
8 9
   Reporter,
9 10
   Info,
10
-  Assert
11
+  Assert,
12
+  Comparer
11 13
 }

+ 10
- 0
test/in-app-test-runner/lib/comparer.js Bestand weergeven

@@ -0,0 +1,10 @@
1
+export default {
2
+  greater : (a, b) => a > b,
3
+  smaller : (a, b) => a < b,
4
+  instanceOf : (a, b) => a instanceof b,
5
+  typeof : (a, b) => typeof a === b,
6
+  IsNull : (a, b) => a === null,
7
+  exists : (a, b) => a !== void 0,
8
+  hasValue : (a, b) => (a !== void 0) && (Array.isArray(a) ? a.length !==0 : true),
9
+  isArray : (a, b) => Array.isArray(a),
10
+}

test/in-app-test-runner/app-test.js → test/in-app-test-runner/lib/test-context.js Bestand weergeven

@@ -1,4 +1,4 @@
1
-export default class RNAppTest {
1
+export default class TestContext {
2 2
 
3 3
   constructor() {
4 4
     this.tests = []
@@ -30,8 +30,8 @@ export default class RNAppTest {
30 30
     return promise
31 31
   }
32 32
 
33
-  update(i, data) {
34
-    Object.assign(this.tests[i], data)
33
+  update(i, ...data) {
34
+    Object.assign(this.tests[i], {result : [...data]})
35 35
     this.context.forceUpdate()
36 36
   }
37 37
 

+ 4
- 0
test/in-app-test-runner/types.js Bestand weergeven

@@ -0,0 +1,4 @@
1
+type TestContext = {
2
+    context? : any,
3
+    tests : Array
4
+}

+ 11
- 0
test/package.json Bestand weergeven

@@ -0,0 +1,11 @@
1
+{
2
+  "name": "react-native-module-testor",
3
+  "version": "0.1.0",
4
+  "description": "An in-app test framework for react-native.",
5
+  "main": "index.js",
6
+  "keywords": [
7
+    "react"
8
+  ],
9
+  "author": "wkh237",
10
+  "license": "MIT"
11
+}

+ 4
- 0
test/test-server/server.js Bestand weergeven

@@ -18,6 +18,10 @@ app.use(upload.any())
18 18
 
19 19
 app.use('/public', express.static('./public'))
20 20
 
21
+app.get('/redirect', function(req, res) {
22
+  res.redirect('/public/github.png')
23
+})
24
+
21 25
 app.post('/upload', function(req, res){
22 26
 
23 27
   console.log(req.headers)

+ 33
- 46
test/tests.js Bestand weergeven

@@ -15,8 +15,8 @@ import {
15 15
 
16 16
 const FILENAME = `${Platform.OS}-0.4.0-${Date.now()}.png`
17 17
 // paste your test config here
18
-const TEST_SERVER_URL = 'http://192.168.17.207:8123'
19
-const DROPBOX_TOKEN = 'fsXcpmKPrHgAAAAAAAAAEGxFXwhejXM_E8fznZoXPhHbhbNhA-Lytbe6etp1Jznz'
18
+const TEST_SERVER_URL = 'http://192.168.17.193:8123'
19
+const DROPBOX_TOKEN = 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4'
20 20
 
21 21
 const ctx = new RNTest.TestContext()
22 22
 const Assert = RNTest.Assert
@@ -31,17 +31,25 @@ ctx.describe('GET image from server', async function(report) {
31 31
     })
32 32
 
33 33
   image = resp.base64()
34
-  report({
35
-    status : 'pass',
36
-    result : [
37
-      <Info key="11" description="Response image">
38
-        <Image key="1"
34
+  report(
35
+      <Info key="Response image">
36
+        <Image
39 37
           style={{width:Dimensions.get('window').width*0.9, height : Dimensions.get('window').width*0.9,margin :16}}
40 38
           source={{uri : `data:image/png;base64, ${image}`}}/>
41
-      </Info>
42
-    ]
43
-  })
44
-  return image
39
+      </Info>)
40
+
41
+})
42
+
43
+ctx.describe('The check if it follows 301/302 redirection', async function(report) {
44
+
45
+  let resp = await RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/redirect`)
46
+  report(
47
+    <Assert key="check image size" expect={image.length} actual={resp.base64().length}/>,
48
+      <Info key="Response image">
49
+        <Image
50
+          style={{width:Dimensions.get('window').width*0.9, height : Dimensions.get('window').width*0.9,margin :16}}
51
+          source={{uri : `data:image/png;base64, ${image}`}}/>
52
+      </Info>)
45 53
 
46 54
 })
47 55
 
@@ -53,12 +61,9 @@ ctx.describe('Upload octet-stream image to Dropbox', async function(report) {
53 61
     'Content-Type' : 'application/octet-stream',
54 62
   }, image)
55 63
   resp = resp.json()
56
-  report({
57
-    status : 'pass',
58
-    result : [
59
-      <Assert key="1" expect={FILENAME} actual={resp.name}/>
60
-    ],
61
-  })
64
+  report(
65
+      <Assert key="confirm the file has been uploaded" expect={FILENAME} actual={resp.name}/>
66
+  )
62 67
 
63 68
 })
64 69
 
@@ -76,39 +81,21 @@ ctx.describe('Upload multipart/form-data', async function(report, data) {
76 81
 
77 82
   resp = resp.json()
78 83
 
79
-  report({
80
-    status : 'pass',
81
-    result : [
82
-      <Assert key="1" expect="hello !!" actual={resp.fields.field1}/>,
83
-      <Assert key="2" expect="hello2 !!" actual={resp.fields.field2}/>,
84
-    ],
85
-  })
84
+  report(
85
+      <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
86
+      <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
87
+  )
86 88
 
87 89
 })
88 90
 
89 91
 ctx.describe('Compare uploaded multipart image', async function(report) {
90
-  // try {
91
-    let resp = await RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-img.png`)
92
-    let resp2 = await RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-text.txt`)
93
-    console.log(resp)
94
-    console.log(resp2)
95
-    report({
96
-      status : 'pass',
97
-      result : [
98
-        <Assert key="1" expect={image.length} actual={resp.base64().length}/>,
99
-        <Assert key="2" expect={'hello.txt'} actual={resp2.text()}/>
100
-      ]
101
-    })
102
-  // } catch(err) {
103
-  //
104
-  //   report({
105
-  //     status : 'fail',
106
-  //     result :[
107
-  //       <Info key="a" description="Detail">
108
-  //         <Text>{JSON.stringify(err)}</Text>
109
-  //       </Info>]
110
-  //   })
111
-  // }
92
+  let resp = await RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-img.png`)
93
+  let resp2 = await RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-text.txt`)
94
+
95
+  report(
96
+      <Assert key="check file length" expect={image.length} actual={resp.base64().length}/>,
97
+      <Assert key="check file content" expect={'hello.txt'} actual={resp2.text()}/>
98
+  )
112 99
 
113 100
 })
114 101