Browse Source

Change test-kit structure

Ben Hsieh 8 years ago
parent
commit
4e68acfbc9

+ 3
- 6
test/react-native-testkit/components/reporter.js View File

@@ -10,13 +10,10 @@ import {
10 10
 } from 'react-native';
11 11
 
12 12
 import Assert from './assert.js'
13
+import RNTEST from '../index.js'
13 14
 
14 15
 export default class Reporter extends Component {
15 16
 
16
-  props : {
17
-    context : TestContext
18
-  };
19
-
20 17
   render() {
21 18
     return (
22 19
       <ScrollView key="rn-test-scroller" style={styles.container}>
@@ -25,7 +22,7 @@ export default class Reporter extends Component {
25 22
   }
26 23
 
27 24
   renderTests() {
28
-    let tests = this.props.context.tests
25
+    let tests = RNTEST.TestContext.getTests()
29 26
     return tests.map((t, i) => {
30 27
 
31 28
       let pass = true
@@ -43,7 +40,7 @@ export default class Reporter extends Component {
43 40
       }
44 41
       if(tests[i].running)
45 42
         t.status = 'running'
46
-      else if(this.props.context.tests[i].executed) {
43
+      else if(tests[i].executed) {
47 44
         t.status = foundActions ? (pass ? 'pass' : 'fail') : 'skipped'
48 45
         t.status = t.expired ? 'timeout' : t.status
49 46
       }

+ 6
- 1
test/react-native-testkit/index.js View File

@@ -4,10 +4,15 @@ import Reporter from './components/reporter'
4 4
 import Assert from './components/assert'
5 5
 import Info from './components/info'
6 6
 
7
+const { describe, run, prop } = TestContext
8
+
7 9
 export default {
8 10
   TestContext,
9 11
   Reporter,
10 12
   Info,
11 13
   Assert,
12
-  Comparer
14
+  Comparer,
15
+  describe,
16
+  run,
17
+  prop
13 18
 }

+ 33
- 24
test/react-native-testkit/lib/test-context.js View File

@@ -1,18 +1,14 @@
1 1
 //@flow
2
-export default class TestContext {
3 2
 
4
-  test: Array<TestCase>;
5
-  context: ReactElement;
3
+let tests: Array<TestCase> = []
4
+let RCTContext: ReactElement = null
5
+let props:any = {}
6
+let timeout = 3000
6 7
 
7
-  static timeout = 3000;
8
+export default class TestContext {
8 9
 
9 10
   static setTimeout (val) {
10
-    this.timeout  = val
11
-  }
12
-
13
-  constructor() {
14
-    this.tests = []
15
-    this.context = null
11
+    timeout = val
16 12
   }
17 13
 
18 14
   /**
@@ -22,9 +18,9 @@ export default class TestContext {
22 18
    *         should return a promise.
23 19
    * @return {void}
24 20
    */
25
-  describe (desc:string, fn:() => Promise<any>) {
21
+  static describe (desc:string, fn:Promise<any>) {
26 22
 
27
-    this.tests.push({
23
+    tests.push({
28 24
       status : 'waiting',
29 25
       result : null,
30 26
       asserts : [],
@@ -37,16 +33,25 @@ export default class TestContext {
37 33
 
38 34
   }
39 35
 
36
+  static prop (name:string, val:any):TestContext {
37
+    if(name === undefined && val === undefined)
38
+      return props
39
+    if(val === undefined)
40
+      return props[name]
41
+    props[name] = val
42
+    return TestContext
43
+  }
44
+
40 45
   /**
41 46
    * Run test cases in sequence.
42 47
    * @param  {ReactElement} context ReactElement instance context.
43 48
    * @return {void}
44 49
    */
45
-  run (context:ReactElement) {
46
-    this.context = context
50
+  static run (context:ReactElement) {
51
+    RCTContext = context
47 52
     let promise = Promise.resolve()
48 53
     // run test case sequently
49
-    for(let i in this.tests) {
54
+    for(let i in tests) {
50 55
       promise = promise.then(function(update, updateInternal, data) {
51 56
         return new Promise((resolve, reject) => {
52 57
 
@@ -88,9 +93,9 @@ export default class TestContext {
88 93
         })
89 94
       }
90 95
       .bind(
91
-        this.tests[i],
92
-        this.update.bind(this, i),
93
-        this.updateInternal.bind(this, i)
96
+        tests[i],
97
+        TestContext.update.bind(TestContext, i),
98
+        TestContext.updateInternal.bind(TestContext, i)
94 99
       ))
95 100
     }
96 101
     return promise
@@ -102,11 +107,15 @@ export default class TestContext {
102 107
    * @param  {ReactElement<Info | Assert>} ...data Assertion or Info of test.
103 108
    * @return {void}
104 109
    */
105
-  update(i, ...data) {
106
-    let test = this.tests[i]
110
+  static update(i, ...data) {
111
+    let test = tests[i]
107 112
     let result = test.result || []
108 113
     Object.assign(test, {result : [...result, ...data]})
109
-    this.context.forceUpdate()
114
+    RCTContext.forceUpdate()
115
+  }
116
+
117
+  static getTests() {
118
+    return tests
110 119
   }
111 120
 
112 121
   /**
@@ -115,9 +124,9 @@ export default class TestContext {
115 124
    * @param  {TestCaseContext} result Test case object
116 125
    * @return {void}
117 126
    */
118
-  updateInternal(i, result) {
119
-    Object.assign(this.tests[i], result)
120
-    this.context.forceUpdate()
127
+  static updateInternal(i, result) {
128
+    Object.assign(tests[i], result)
129
+    RCTContext.forceUpdate()
121 130
   }
122 131
 
123 132
 }

+ 44
- 31
test/tests.js View File

@@ -52,8 +52,6 @@ ctx.describe('GET image from server', (report, done) => {
52 52
 
53 53
 })
54 54
 
55
-// FIXME : discard these test cases in feature branch
56
-//
57 55
 // ctx.describe('The check if it follows 301/302 redirection', (report, done) => {
58 56
 //
59 57
 //   RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/redirect`)
@@ -109,7 +107,7 @@ ctx.describe('GET image from server', (report, done) => {
109 107
 //
110 108
 //
111 109
 // })
112
-
110
+//
113 111
 // ctx.describe('Compare uploaded multipart image', (report, done) => {
114 112
 //   let r1 = null
115 113
 //   RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-img.png`)
@@ -129,37 +127,37 @@ ctx.describe('GET image from server', (report, done) => {
129 127
 
130 128
 // added after 0.4.2
131 129
 
132
-ctx.describe('Progress report test', (report, done) => {
133
-  let received = 0
134
-  RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/1mb-dummy`, {
135
-      Authorization : 'Bearer abde123eqweje'
136
-    })
137
-    .progress((written, total) => {
138
-      report(<Info key={`progress = ${written} bytes / ${total} bytes`}/>)
139
-      if(written === total)
140
-        report(<Assert key="progress goes to 100%" expect={written} actual={total}/>)
141
-    })
142
-    .then((resp) => {
143
-      report(<Assert key="response data should be correct event with progress listener"
144
-        expect={resp.text().substr(0,10)} actual={"1234567890"}/>)
145
-      done()
146
-    })
147
-
148
-})
149
-
150
-// FIXME : not yet supported
151
-// ctx.describe('Large file download test', (report, done) => {
130
+// ctx.describe('Progress report test', (report, done) => {
152 131
 //   let received = 0
153
-//   // RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`, {
154
-//   //   Authorization : 'Bearer abde123eqweje'
155
-//   // })
156
-//   // .then((resp) => {
157
-//     report(<Assert key="not supported" expect={true} actual={false}/>)
158
-//     done()
159
-//   // })
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
+//     })
160 145
 //
161 146
 // })
162 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
+
163 161
 // added after 0.5.0
164 162
 
165 163
 ctx.describe('Get storage folders', (report, done) => {
@@ -199,7 +197,7 @@ ctx.describe('Download file to storage with custom file extension', (report, don
199 197
     })
200 198
 })
201 199
 
202
-ctx.describe('Read cache file with file stream', (report, done) => {
200
+ctx.describe('Read cached file via file stream', (report, done) => {
203 201
   let data = 'data:image/png;base64, '
204 202
   let stream = RNFetchBlob.openReadStream(tmpFilePath, 'base64')
205 203
   stream.onData((chunk) => {
@@ -219,4 +217,19 @@ ctx.describe('Read cache file with file stream', (report, done) => {
219 217
 
220 218
 })
221 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
+
222 235
 export default ctx