Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit dad766f

Browse files
committed
Change testkit API and function
1 parent 5a9067b commit dad766f

File tree

2 files changed

+122
-26
lines changed

2 files changed

+122
-26
lines changed

test/react-native-testkit/components/reporter.js

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,30 @@ export default class Reporter extends Component {
2525
}
2626

2727
renderTests() {
28-
return this.props.context.tests.map((t, i) => {
28+
let tests = this.props.context.tests
29+
return tests.map((t, i) => {
2930

3031
let pass = true
31-
let foundAssertions = false
32+
let foundActions = false
3233

33-
if(Array.isArray(t.result)) {
34+
if(Array.isArray(t.result) && !t.expired) {
3435
t.result = t.result.map((r) => {
35-
if(r.type.name === 'Assert') {
36-
foundAssertions = true
36+
if(r.type.name === 'Assert' || r.type.name === 'Info') {
37+
foundActions = true
3738
let comp = r.props.comparer ? r.props.comparer(r.props.expect, r.props.actual) : (r.props.actual === r.props.expect)
3839
pass = pass && comp
3940
}
4041
return React.cloneElement(r, {desc : r.key})
4142
})
4243
}
43-
44-
t.status = foundAssertions ? (pass ? 'pass' : 'fail') : 'pass'
44+
if(tests[i].running)
45+
t.status = 'running'
46+
else if(this.props.context.tests[i].executed) {
47+
t.status = foundActions ? (pass ? 'pass' : 'fail') : 'skipped'
48+
t.status = t.expired ? 'timeout' : t.status
49+
}
50+
else
51+
t.status = 'waiting'
4552

4653
return (<View key={'rn-test-' + t.desc} style={{
4754
borderBottomWidth : 1.5,
@@ -52,7 +59,7 @@ export default class Reporter extends Component {
5259
flexDirection : 'row'
5360
}}>
5461
<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>
62+
<Text style={[styles.badge, this.getBadge(t.status)]}>{t.status}</Text>
5663
</View>
5764
<View key={t.desc + '-result'} style={{backgroundColor : '#F4F4F4'}}>
5865
{t.result}
@@ -61,13 +68,8 @@ export default class Reporter extends Component {
6168
})
6269
}
6370

64-
getBadge(status: 'running' | 'pass' | 'fail') {
65-
if(status === 'running')
66-
return styles.badgeWaiting
67-
else if(status === 'pass')
68-
return styles.badgePass
69-
else
70-
return styles.badgeFail
71+
getBadge(status: 'waiting' | 'running' | 'pass' | 'fail' | 'timeout') {
72+
return styles[status]
7173
}
7274

7375
}
@@ -84,15 +86,27 @@ const styles = StyleSheet.create({
8486
borderWidth : 2,
8587
textAlign : 'center'
8688
},
87-
badgePass: {
89+
skipped: {
90+
borderColor : '#AAAAAA',
91+
color : '#AAAAAA'
92+
},
93+
waiting: {
94+
borderColor : '#AAAAAA',
95+
color : '#AAAAAA'
96+
},
97+
pass: {
8898
borderColor : '#00a825',
8999
color : '#00a825'
90100
},
91-
badgeWaiting: {
101+
running: {
92102
borderColor : '#e3c423',
93103
color : '#e3c423'
94104
},
95-
badgeFail: {
105+
fail: {
106+
borderColor : '#ff0d0d',
107+
color : '#ff0d0d'
108+
},
109+
timeout: {
96110
borderColor : '#ff0d0d',
97111
color : '#ff0d0d'
98112
}

test/react-native-testkit/lib/test-context.js

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,119 @@
1+
//@flow
12
export default class TestContext {
23

4+
test: Array<TestCase>;
5+
context: ReactElement;
6+
7+
static timeout = 3000;
8+
9+
static setTimeout (val) {
10+
this.timeout = val
11+
}
12+
313
constructor() {
414
this.tests = []
515
this.context = null
616
}
717

8-
describe (desc, fn) {
18+
/**
19+
* Calling this method will push a test case into task queue.
20+
* @param {String} desc Description of test case.
21+
* @param {Function:Promise<any>} fn Body of test case, this function
22+
* should return a promise.
23+
* @return {void}
24+
*/
25+
describe (desc:string, fn:() => Promise<any>) {
926

1027
this.tests.push({
11-
status : 'running',
28+
status : 'waiting',
1229
result : null,
1330
asserts : [],
31+
timeout : 3000,
32+
expired : false,
33+
running : false,
34+
executed : false,
1435
desc, fn,
1536
})
1637

1738
}
1839

19-
run (context) {
40+
/**
41+
* Run test cases in sequence.
42+
* @param {ReactElement} context ReactElement instance context.
43+
* @return {void}
44+
*/
45+
run (context:ReactElement) {
2046
this.context = context
2147
let promise = Promise.resolve()
48+
// run test case sequently
2249
for(let i in this.tests) {
23-
promise = promise.then(function(update, data) {
24-
return this.fn(update, data)
25-
}.bind(
50+
promise = promise.then(function(update, updateInternal, data) {
51+
return new Promise((resolve, reject) => {
52+
53+
let expired = false
54+
updateInternal({
55+
running : true,
56+
})
57+
58+
// set timeout timer
59+
let tm = setTimeout(() => {
60+
updateInternal({
61+
expired : true,
62+
executed : true,
63+
running : false
64+
})
65+
resolve('ETIMEOUT')
66+
}, this.timeout)
67+
68+
// run test body
69+
this.fn.bind(this)(update, data).then((...res) => {
70+
if(!expired) {
71+
clearTimeout(tm)
72+
updateInternal({
73+
executed : true,
74+
running : false
75+
})
76+
resolve(...res)
77+
}
78+
}).catch((err) => {
79+
updateInternal({
80+
executed : true,
81+
running : false
82+
})
83+
})
84+
85+
})
86+
}
87+
.bind(
2688
this.tests[i],
27-
this.update.bind(this, i)
89+
this.update.bind(this, i),
90+
this.updateInternal.bind(this, i)
2891
))
2992
}
3093
return promise
3194
}
3295

96+
/**
97+
* Update test task result of given index.
98+
* @param {number} i Index of test case to be updated.
99+
* @param {ReactElement<Info | Assert>} ...data Assertion or Info of test.
100+
* @return {void}
101+
*/
33102
update(i, ...data) {
34-
Object.assign(this.tests[i], {result : [...data]})
103+
let test = this.tests[i]
104+
let result = test.result || []
105+
Object.assign(test, {result : [...result, ...data]})
106+
this.context.forceUpdate()
107+
}
108+
109+
/**
110+
* Update test result for testkit internal use
111+
* @param {[type]} i Index of test case to be updated.
112+
* @param {TestCaseContext} result Test case object
113+
* @return {void}
114+
*/
115+
updateInternal(i, result) {
116+
Object.assign(this.tests[i], result)
35117
this.context.forceUpdate()
36118
}
37119

0 commit comments

Comments
 (0)