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

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

4
 import Assert from './components/assert'
4
 import Assert from './components/assert'
5
 import Info from './components/info'
5
 import Info from './components/info'
6
 
6
 
7
+const { describe, run, prop } = TestContext
8
+
7
 export default {
9
 export default {
8
   TestContext,
10
   TestContext,
9
   Reporter,
11
   Reporter,
10
   Info,
12
   Info,
11
   Assert,
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
 //@flow
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
   static setTimeout (val) {
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
    *         should return a promise.
18
    *         should return a promise.
23
    * @return {void}
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
       status : 'waiting',
24
       status : 'waiting',
29
       result : null,
25
       result : null,
30
       asserts : [],
26
       asserts : [],
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
    * Run test cases in sequence.
46
    * Run test cases in sequence.
42
    * @param  {ReactElement} context ReactElement instance context.
47
    * @param  {ReactElement} context ReactElement instance context.
43
    * @return {void}
48
    * @return {void}
44
    */
49
    */
45
-  run (context:ReactElement) {
46
-    this.context = context
50
+  static run (context:ReactElement) {
51
+    RCTContext = context
47
     let promise = Promise.resolve()
52
     let promise = Promise.resolve()
48
     // run test case sequently
53
     // run test case sequently
49
-    for(let i in this.tests) {
54
+    for(let i in tests) {
50
       promise = promise.then(function(update, updateInternal, data) {
55
       promise = promise.then(function(update, updateInternal, data) {
51
         return new Promise((resolve, reject) => {
56
         return new Promise((resolve, reject) => {
52
 
57
 
88
         })
93
         })
89
       }
94
       }
90
       .bind(
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
     return promise
101
     return promise
102
    * @param  {ReactElement<Info | Assert>} ...data Assertion or Info of test.
107
    * @param  {ReactElement<Info | Assert>} ...data Assertion or Info of test.
103
    * @return {void}
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
     let result = test.result || []
112
     let result = test.result || []
108
     Object.assign(test, {result : [...result, ...data]})
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
    * @param  {TestCaseContext} result Test case object
124
    * @param  {TestCaseContext} result Test case object
116
    * @return {void}
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
 
52
 
53
 })
53
 })
54
 
54
 
55
-// FIXME : discard these test cases in feature branch
56
-//
57
 // ctx.describe('The check if it follows 301/302 redirection', (report, done) => {
55
 // ctx.describe('The check if it follows 301/302 redirection', (report, done) => {
58
 //
56
 //
59
 //   RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/redirect`)
57
 //   RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/redirect`)
109
 //
107
 //
110
 //
108
 //
111
 // })
109
 // })
112
-
110
+//
113
 // ctx.describe('Compare uploaded multipart image', (report, done) => {
111
 // ctx.describe('Compare uploaded multipart image', (report, done) => {
114
 //   let r1 = null
112
 //   let r1 = null
115
 //   RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-img.png`)
113
 //   RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-img.png`)
129
 
127
 
130
 // added after 0.4.2
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
 //   let received = 0
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
 // added after 0.5.0
161
 // added after 0.5.0
164
 
162
 
165
 ctx.describe('Get storage folders', (report, done) => {
163
 ctx.describe('Get storage folders', (report, done) => {
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
   let data = 'data:image/png;base64, '
201
   let data = 'data:image/png;base64, '
204
   let stream = RNFetchBlob.openReadStream(tmpFilePath, 'base64')
202
   let stream = RNFetchBlob.openReadStream(tmpFilePath, 'base64')
205
   stream.onData((chunk) => {
203
   stream.onData((chunk) => {
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
 export default ctx
235
 export default ctx