/
index.test.js
61 lines (53 loc) · 1.61 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import React, { Component } from 'react'
import { AppRegistry } from 'react-native'
import App from './src/App'
import { name as appName } from './app.json'
import { Tester, TestHookStore } from 'cavy'
import scenarios from './src/scenarios'
const store = new TestHookStore();
// Validate scenarios.
scenarios.forEach(scenario => {
if (!(scenario.key && scenario.label && scenario.Screen && scenario.spec)) {
throw Error(
'CavyTester: key, label, Screen, and spec must be defined for each scenario'
);
}
if (!/^[a-z0-9]+$/i.exec(scenario.key)) {
throw Error('CavyTester: Scenario keys should be alphanumeric');
}
});
// Map each of our test scenarios, so that each first navigates to the correct
// scene.
const navigateAndRun = (scenario) => {
return (spec) => {
// Override `describe` so that all tests have the 'focus' tag, unless you
// specifically pass a different tag in.
const origDescribe = spec.describe;
spec.describe = (label, f, tag) => {
const testTag = tag || 'focus';
origDescribe.call(spec, label, f, testTag)
}
// Override `it` so that it first presses into the scene.
const origIt = spec.it;
spec.it = (label, f) => {
origIt.call(spec, label, async () => {
await spec.press(scenario.key);
await f();
});
};
scenario.spec(spec);
}
}
class TestableApp extends Component {
render() {
return (
<Tester
specs={scenarios.map(x => navigateAndRun(x))}
store={store}
only={['focus']} >
<App />
</Tester>
);
}
}
AppRegistry.registerComponent(appName, () => TestableApp);