Factor out some common code in React tests

Now iOS and Android share more code. Also converted to using ES6 modules imports and fixed some other linting issues.
This commit is contained in:
Scott Kyle 2016-03-29 14:47:44 -07:00
parent 48394d9c19
commit aa09ba3cd4
5 changed files with 97 additions and 110 deletions

View File

@ -18,7 +18,6 @@
"parser": "babel-eslint",
"parserOptions": {
"ecmaFeatures": {
"forOf": false,
"jsx": true
}
},

View File

@ -17,60 +17,44 @@
////////////////////////////////////////////////////////////////////////////
'use strict';
const React = require('react');
const Realm = require('realm');
const RealmTests = require('realm-tests');
const builder = require('xmlbuilder');
const RNFS = require('react-native-fs');
const {
import {
AppRegistry,
StyleSheet,
Image,
Text,
View,
TouchableNativeFeedback,
} = require('react-native');
} from 'react-native';
RealmTests.registerTests({
ListViewTest: require('./tests/listview-test'),
});
import builder from 'xmlbuilder';
import React from 'react';
import RNFS from 'react-native-fs';
import { getTestNames, runTest } from './tests';
function runTests() {
async function runTests() {
let testNames = getTestNames();
let rootXml = builder.create('testsuites');
let testNames = RealmTests.getTestNames();
for (let suiteName in testNames) {
let itemTestsuite = rootXml.ele('testsuite');
let nbrTests = 0;
let nbrFailures = 0;
console.log('Starting suite ' + suiteName);
console.log('Starting ' + suiteName);
testNames[suiteName].forEach((testName) => {
for (let testName of testNames[suiteName]) {
nbrTests++;
let itemTest = itemTestsuite.ele('testcase');
itemTest.att('name', testName);
console.log('Starting ' + testName);
RealmTests.runTest(suiteName, 'beforeEach');
try {
RealmTests.runTest(suiteName, testName);
console.log('+ ' + testName);
runTest(suiteName, testName);
}
catch (e) {
console.log('- ' + testName);
console.warn(e.message);
itemTest.ele('error', {'message': ''}, e.message);
nbrFailures++;
}
finally {
RealmTests.runTest(suiteName, 'afterEach');
}
});
}
// update Junit XML report
itemTestsuite.att('name', suiteName);
@ -80,55 +64,57 @@ function runTests() {
}
// export unit tests results
var xmlString = rootXml.end({ pretty: true, indent: ' ', newline: '\n' });
var path = '/sdcard/tests.xml';
let xmlString = rootXml.end({
pretty: true,
indent: ' ',
newline: '\n',
});
// write the unit tests reports
RNFS.writeFile(path, xmlString , 'utf8')
.then((success) => {
try {
await RNFS.writeFile('/sdcard/tests.xml', xmlString, 'utf8');
console.log('__REALM_REACT_ANDROID_TESTS_COMPLETED__');
})
.catch((err) => {
console.log(err.message);
});
}
catch (e) {
console.error(e);
}
}
class ReactTests extends React.Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.button} onPress={runTests}>
Running Tests...
</Text>
render() {
return (
<View style={styles.container}>
<Text style={styles.button} onPress={runTests}>
Running Tests...
</Text>
<Image
style={styles.icon}
source={require('image!ic_launcher')}
onLoad={() => runTests()}
/>
</View>
);
}
<Image
style={styles.icon}
source={require('image!ic_launcher')}
onLoad={() => runTests()}
/>
</View>
);
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('ReactTests', () => ReactTests);

View File

@ -18,23 +18,21 @@
'use strict';
const React = require('react');
const Realm = require('realm');
const tests = require('./tests');
const {
import {
AppRegistry,
StyleSheet,
Text,
TouchableHighlight,
View,
} = require('react-native');
} from 'react-native';
import React from 'react';
import { runTests } from './tests';
class ReactTests extends React.Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.button} onPress={tests.runTests}>
<Text style={styles.button} onPress={runTests}>
Tap to Run Tests
</Text>
<Text style={styles.instructions}>
@ -46,7 +44,7 @@ class ReactTests extends React.Component {
}
}
var styles = StyleSheet.create({
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',

View File

@ -18,22 +18,14 @@
'use strict';
const React = require('react-native');
const RealmTests = require('realm-tests');
import { NativeAppEventEmitter, NativeModules } from 'react-native';
import * as RealmTests from 'realm-tests';
import ListViewTest from './listview-test';
RealmTests.registerTests({
ListViewTest: require('./listview-test'),
ListViewTest,
});
const {
NativeAppEventEmitter,
NativeModules,
} = React;
module.exports = {
runTests,
};
// Listen for event to run a particular test.
NativeAppEventEmitter.addListener('realm-run-test', (test) => {
let error;
@ -48,29 +40,41 @@ NativeAppEventEmitter.addListener('realm-run-test', (test) => {
// Inform the native test harness about the test suite once it's ready.
setTimeout(() => {
NativeModules.Realm.emit('realm-test-names', RealmTests.getTestNames());
// The emit() method only exists on iOS, for now.
if (NativeModules.Realm.emit) {
NativeModules.Realm.emit('realm-test-names', getTestNames());
}
}, 0);
function runTests() {
let testNames = RealmTests.getTestNames();
export function getTestNames() {
return RealmTests.getTestNames();
}
export function runTests() {
let testNames = getTestNames();
for (let suiteName in testNames) {
console.log('Starting ' + suiteName);
for (let testName of testNames[suiteName]) {
RealmTests.runTest(suiteName, 'beforeEach');
try {
RealmTests.runTest(suiteName, testName);
console.log('+ ' + testName);
}
catch (e) {
console.warn('- ' + testName);
console.warn(e.message);
}
finally {
RealmTests.runTest(suiteName, 'afterEach');
}
runTest(suiteName, testName);
}
}
}
export function runTest(suiteName, testName) {
RealmTests.runTest(suiteName, 'beforeEach');
try {
RealmTests.runTest(suiteName, testName);
console.log('+ ' + testName);
}
catch (e) {
console.warn('- ' + testName);
console.warn(e.message || e);
throw e;
}
finally {
RealmTests.runTest(suiteName, 'afterEach');
}
}

View File

@ -18,9 +18,9 @@
'use strict';
const Realm = require('realm');
const { ListView } = require('realm/react-native');
const { assertEqual, assertTrue } = require('realm-tests/asserts');
import Realm from 'realm';
import { ListView } from 'realm/react-native';
import { assertEqual, assertTrue } from 'realm-tests/asserts';
const OBJECT_SCHEMA = {
name: 'UniqueObject',
@ -48,7 +48,7 @@ function createDataSource() {
});
}
module.exports = {
export default {
testDataSource() {
let realm = createRealm();
let objects = realm.objects('UniqueObject').sorted('id');