Add test for ListViewDataSource to React Test App
This commit is contained in:
parent
1f030a0618
commit
0188517e74
|
@ -31,6 +31,12 @@ exports.getTestNames = function() {
|
|||
return testNames;
|
||||
};
|
||||
|
||||
exports.registerTests = function(tests) {
|
||||
for (var suiteName in tests) {
|
||||
TESTS[suiteName] = tests[suiteName];
|
||||
}
|
||||
};
|
||||
|
||||
exports.runTest = function(suiteName, testName) {
|
||||
var testSuite = TESTS[suiteName];
|
||||
var testMethod = testSuite && testSuite[testName];
|
||||
|
|
|
@ -1,52 +1,49 @@
|
|||
/**
|
||||
* Sample React Native App
|
||||
* https://github.com/facebook/react-native
|
||||
/* Copyright 2016 Realm Inc - All Rights Reserved
|
||||
* Proprietary and Confidential
|
||||
*/
|
||||
'use strict';
|
||||
var React = require('react-native');
|
||||
var {
|
||||
AppRegistry,
|
||||
StyleSheet,
|
||||
Image,
|
||||
Text,
|
||||
View,
|
||||
TouchableNativeFeedback,
|
||||
|
||||
const React = require('react-native');
|
||||
const Realm = require('realm');
|
||||
const RealmTests = require('realm-tests');
|
||||
const builder = require('xmlbuilder');
|
||||
const RNFS = require('react-native-fs');
|
||||
|
||||
const {
|
||||
AppRegistry,
|
||||
StyleSheet,
|
||||
Image,
|
||||
Text,
|
||||
View,
|
||||
TouchableNativeFeedback,
|
||||
} = React;
|
||||
|
||||
var Realm = require('realm');
|
||||
var RealmTests = require('realm-tests');
|
||||
var builder = require('xmlbuilder');
|
||||
var RNFS = require('react-native-fs');
|
||||
RealmTests.registerTests({
|
||||
ListViewTest: require('./tests/listview-test'),
|
||||
});
|
||||
|
||||
function runTests() {
|
||||
var rootXml = builder.create('testsuites');
|
||||
let rootXml = builder.create('testsuites');
|
||||
let testNames = RealmTests.getTestNames();
|
||||
|
||||
for (let suiteName in testNames) {
|
||||
var itemTestsuite = rootXml.ele('testsuite');
|
||||
let itemTestsuite = rootXml.ele('testsuite');
|
||||
let nbrTests = 0;
|
||||
let nbrFailures = 0;
|
||||
|
||||
let testSuite = RealmTests[suiteName];
|
||||
|
||||
console.log('Starting suite ' + suiteName);
|
||||
|
||||
var suiteTestNames = testNames[suiteName];
|
||||
for (var index in suiteTestNames) {
|
||||
testNames[suiteName].forEach((testName) => {
|
||||
nbrTests++;
|
||||
var testName = suiteTestNames[index];
|
||||
|
||||
var itemTest = itemTestsuite.ele('testcase');
|
||||
let itemTest = itemTestsuite.ele('testcase');
|
||||
itemTest.att('name', testName);
|
||||
|
||||
console.log('Starting ' + testName);
|
||||
|
||||
if (testSuite.beforeEach) {
|
||||
testSuite.beforeEach();
|
||||
}
|
||||
RealmTests.runTest(suiteName, 'beforeEach');
|
||||
|
||||
try {
|
||||
testSuite[testName]();
|
||||
RealmTests.runTest(suiteName, testName);
|
||||
console.log('+ ' + testName);
|
||||
}
|
||||
catch (e) {
|
||||
|
@ -57,11 +54,9 @@ function runTests() {
|
|||
nbrFailures++;
|
||||
}
|
||||
finally {
|
||||
if (testSuite.afterEach) {
|
||||
testSuite.afterEach();
|
||||
}
|
||||
RealmTests.runTest(suiteName, 'afterEach');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// update Junit XML report
|
||||
itemTestsuite.att('name', suiteName);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* Copyright 2015 Realm Inc - All Rights Reserved
|
||||
/* Copyright 2016 Realm Inc - All Rights Reserved
|
||||
* Proprietary and Confidential
|
||||
*/
|
||||
|
||||
|
@ -6,70 +6,21 @@
|
|||
|
||||
const React = require('react-native');
|
||||
const Realm = require('realm');
|
||||
const RealmTests = require('realm-tests');
|
||||
const tests = require('./tests');
|
||||
|
||||
const {
|
||||
AppRegistry,
|
||||
NativeAppEventEmitter,
|
||||
NativeModules,
|
||||
StyleSheet,
|
||||
Text,
|
||||
TouchableHighlight,
|
||||
View,
|
||||
} = React;
|
||||
|
||||
// Listen for event to run a particular test.
|
||||
NativeAppEventEmitter.addListener('realm-run-test', (test) => {
|
||||
let error;
|
||||
try {
|
||||
RealmTests.runTest(test.suite, test.name);
|
||||
} catch (e) {
|
||||
error = '' + e;
|
||||
}
|
||||
|
||||
NativeModules.Realm.emit('realm-test-finished', error);
|
||||
});
|
||||
|
||||
// Inform the native test harness about the test suite once it's ready.
|
||||
setTimeout(() => {
|
||||
NativeModules.Realm.emit('realm-test-names', RealmTests.getTestNames());
|
||||
}, 0);
|
||||
|
||||
function runTests() {
|
||||
let testNames = RealmTests.getTestNames();
|
||||
|
||||
for (let suiteName in testNames) {
|
||||
let testSuite = RealmTests[suiteName];
|
||||
|
||||
console.log('Starting ' + suiteName);
|
||||
|
||||
for (let testName of testNames[suiteName]) {
|
||||
if (testSuite.beforeEach) {
|
||||
testSuite.beforeEach();
|
||||
}
|
||||
|
||||
try {
|
||||
testSuite[testName]();
|
||||
console.log('+ ' + testName);
|
||||
}
|
||||
catch (e) {
|
||||
console.log('- ' + testName);
|
||||
console.warn(e.message);
|
||||
}
|
||||
finally {
|
||||
if (testSuite.afterEach) {
|
||||
testSuite.afterEach();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ReactTests extends React.Component {
|
||||
render() {
|
||||
return (
|
||||
<View style={styles.container}>
|
||||
<Text style={styles.button} onPress={runTests}>
|
||||
<Text style={styles.button} onPress={tests.runTests}>
|
||||
Tap to Run Tests
|
||||
</Text>
|
||||
<Text style={styles.instructions}>
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
/* Copyright 2016 Realm Inc - All Rights Reserved
|
||||
* Proprietary and Confidential
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const React = require('react-native');
|
||||
const Realm = require('realm');
|
||||
const RealmTests = require('realm-tests');
|
||||
|
||||
RealmTests.registerTests({
|
||||
ListViewTest: require('./listview-test'),
|
||||
});
|
||||
|
||||
const {
|
||||
NativeAppEventEmitter,
|
||||
NativeModules,
|
||||
} = React;
|
||||
|
||||
module.exports = {
|
||||
runTests,
|
||||
};
|
||||
|
||||
// Listen for event to run a particular test.
|
||||
NativeAppEventEmitter.addListener('realm-run-test', (test) => {
|
||||
let error;
|
||||
try {
|
||||
RealmTests.runTest(test.suite, test.name);
|
||||
} catch (e) {
|
||||
error = '' + e;
|
||||
}
|
||||
|
||||
NativeModules.Realm.emit('realm-test-finished', error);
|
||||
});
|
||||
|
||||
// Inform the native test harness about the test suite once it's ready.
|
||||
setTimeout(() => {
|
||||
NativeModules.Realm.emit('realm-test-names', RealmTests.getTestNames());
|
||||
}, 0);
|
||||
|
||||
function runTests() {
|
||||
let testNames = RealmTests.getTestNames();
|
||||
|
||||
for (let suiteName in testNames) {
|
||||
let testSuite = RealmTests[suiteName];
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
/* Copyright 2016 Realm Inc - All Rights Reserved
|
||||
* Proprietary and Confidential
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const Realm = require('realm');
|
||||
const { ListView } = require('realm/react-native');
|
||||
const { assertEqual, assertTrue } = require('realm-tests/asserts');
|
||||
|
||||
const OBJECT_SCHEMA = {
|
||||
name: 'UniqueObject',
|
||||
primaryKey: 'id',
|
||||
properties: {
|
||||
id: 'int',
|
||||
}
|
||||
};
|
||||
|
||||
function createRealm() {
|
||||
let realm = new Realm({schema: [OBJECT_SCHEMA]});
|
||||
|
||||
realm.write(() => {
|
||||
for (let i = 0; i < 100; i++) {
|
||||
realm.create('UniqueObject', {id: i});
|
||||
}
|
||||
});
|
||||
|
||||
return realm;
|
||||
}
|
||||
|
||||
function createDataSource() {
|
||||
return new ListView.DataSource({
|
||||
rowHasChanged: (a, b) => a.id !== b.id,
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
afterEach() {
|
||||
Realm.clearTestState();
|
||||
},
|
||||
|
||||
testDataSource() {
|
||||
let realm = createRealm();
|
||||
let objects = realm.objects('UniqueObject');
|
||||
objects.sortByProperty('id');
|
||||
|
||||
let dataSource = createDataSource().cloneWithRows(objects);
|
||||
let count = objects.length;
|
||||
|
||||
// Make sure the section header should update.
|
||||
assertTrue(dataSource.sectionHeaderShouldUpdate(0));
|
||||
|
||||
// All rows should need to update.
|
||||
for (let i = 0; i < count; i++) {
|
||||
assertTrue(dataSource.rowShouldUpdate(0, i));
|
||||
}
|
||||
|
||||
// Clone data source with no changes and make sure no rows need to update.
|
||||
dataSource = dataSource.cloneWithRows(objects);
|
||||
for (let i = 0; i < count; i++) {
|
||||
assertTrue(!dataSource.rowShouldUpdate(0, i));
|
||||
}
|
||||
|
||||
// Delete the second object and make sure current data source is unchanged.
|
||||
realm.write(() => realm.delete(objects[1]));
|
||||
for (let i = 0; i < count; i++) {
|
||||
assertTrue(!dataSource.rowShouldUpdate(0, i));
|
||||
}
|
||||
|
||||
// Getting the row data for the second row should return null.
|
||||
assertEqual(dataSource.getRow('s1', 1), null);
|
||||
|
||||
// Clone data source and make sure all rows after the first one need to update.
|
||||
dataSource = dataSource.cloneWithRows(objects);
|
||||
for (let i = 0; i < count - 1; i++) {
|
||||
let changed = dataSource.rowShouldUpdate(0, i);
|
||||
assertTrue(i == 0 ? !changed : changed);
|
||||
}
|
||||
|
||||
// Create an object at the ened and make sure only the last row needs to update.
|
||||
realm.write(() => realm.create('UniqueObject', {id: count}));
|
||||
dataSource = dataSource.cloneWithRows(objects);
|
||||
for (let i = 0; i < count; i++) {
|
||||
let changed = dataSource.rowShouldUpdate(0, i);
|
||||
assertTrue(i < count - 1 ? !changed : changed);
|
||||
}
|
||||
},
|
||||
};
|
Loading…
Reference in New Issue