mirror of
https://github.com/status-im/realm-js.git
synced 2025-01-11 06:46:03 +00:00
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;
|
return testNames;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.registerTests = function(tests) {
|
||||||
|
for (var suiteName in tests) {
|
||||||
|
TESTS[suiteName] = tests[suiteName];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
exports.runTest = function(suiteName, testName) {
|
exports.runTest = function(suiteName, testName) {
|
||||||
var testSuite = TESTS[suiteName];
|
var testSuite = TESTS[suiteName];
|
||||||
var testMethod = testSuite && testSuite[testName];
|
var testMethod = testSuite && testSuite[testName];
|
||||||
|
@ -1,10 +1,15 @@
|
|||||||
/**
|
/* Copyright 2016 Realm Inc - All Rights Reserved
|
||||||
* Sample React Native App
|
* Proprietary and Confidential
|
||||||
* https://github.com/facebook/react-native
|
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
var React = require('react-native');
|
|
||||||
var {
|
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,
|
AppRegistry,
|
||||||
StyleSheet,
|
StyleSheet,
|
||||||
Image,
|
Image,
|
||||||
@ -13,40 +18,32 @@ var {
|
|||||||
TouchableNativeFeedback,
|
TouchableNativeFeedback,
|
||||||
} = React;
|
} = React;
|
||||||
|
|
||||||
var Realm = require('realm');
|
RealmTests.registerTests({
|
||||||
var RealmTests = require('realm-tests');
|
ListViewTest: require('./tests/listview-test'),
|
||||||
var builder = require('xmlbuilder');
|
});
|
||||||
var RNFS = require('react-native-fs');
|
|
||||||
|
|
||||||
function runTests() {
|
function runTests() {
|
||||||
var rootXml = builder.create('testsuites');
|
let rootXml = builder.create('testsuites');
|
||||||
let testNames = RealmTests.getTestNames();
|
let testNames = RealmTests.getTestNames();
|
||||||
|
|
||||||
for (let suiteName in testNames) {
|
for (let suiteName in testNames) {
|
||||||
var itemTestsuite = rootXml.ele('testsuite');
|
let itemTestsuite = rootXml.ele('testsuite');
|
||||||
let nbrTests = 0;
|
let nbrTests = 0;
|
||||||
let nbrFailures = 0;
|
let nbrFailures = 0;
|
||||||
|
|
||||||
let testSuite = RealmTests[suiteName];
|
|
||||||
|
|
||||||
console.log('Starting suite ' + suiteName);
|
console.log('Starting suite ' + suiteName);
|
||||||
|
|
||||||
var suiteTestNames = testNames[suiteName];
|
testNames[suiteName].forEach((testName) => {
|
||||||
for (var index in suiteTestNames) {
|
|
||||||
nbrTests++;
|
nbrTests++;
|
||||||
var testName = suiteTestNames[index];
|
|
||||||
|
|
||||||
var itemTest = itemTestsuite.ele('testcase');
|
let itemTest = itemTestsuite.ele('testcase');
|
||||||
itemTest.att('name', testName);
|
itemTest.att('name', testName);
|
||||||
|
|
||||||
console.log('Starting ' + testName);
|
console.log('Starting ' + testName);
|
||||||
|
RealmTests.runTest(suiteName, 'beforeEach');
|
||||||
if (testSuite.beforeEach) {
|
|
||||||
testSuite.beforeEach();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
testSuite[testName]();
|
RealmTests.runTest(suiteName, testName);
|
||||||
console.log('+ ' + testName);
|
console.log('+ ' + testName);
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
@ -57,11 +54,9 @@ function runTests() {
|
|||||||
nbrFailures++;
|
nbrFailures++;
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
if (testSuite.afterEach) {
|
RealmTests.runTest(suiteName, 'afterEach');
|
||||||
testSuite.afterEach();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// update Junit XML report
|
// update Junit XML report
|
||||||
itemTestsuite.att('name', suiteName);
|
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
|
* Proprietary and Confidential
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -6,70 +6,21 @@
|
|||||||
|
|
||||||
const React = require('react-native');
|
const React = require('react-native');
|
||||||
const Realm = require('realm');
|
const Realm = require('realm');
|
||||||
const RealmTests = require('realm-tests');
|
const tests = require('./tests');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
AppRegistry,
|
AppRegistry,
|
||||||
NativeAppEventEmitter,
|
|
||||||
NativeModules,
|
|
||||||
StyleSheet,
|
StyleSheet,
|
||||||
Text,
|
Text,
|
||||||
TouchableHighlight,
|
TouchableHighlight,
|
||||||
View,
|
View,
|
||||||
} = React;
|
} = 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 {
|
class ReactTests extends React.Component {
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<View style={styles.container}>
|
<View style={styles.container}>
|
||||||
<Text style={styles.button} onPress={runTests}>
|
<Text style={styles.button} onPress={tests.runTests}>
|
||||||
Tap to Run Tests
|
Tap to Run Tests
|
||||||
</Text>
|
</Text>
|
||||||
<Text style={styles.instructions}>
|
<Text style={styles.instructions}>
|
||||||
|
65
tests/react-test-app/tests/index.js
vendored
Normal file
65
tests/react-test-app/tests/index.js
vendored
Normal file
@ -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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
88
tests/react-test-app/tests/listview-test.js
vendored
Normal file
88
tests/react-test-app/tests/listview-test.js
vendored
Normal file
@ -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…
x
Reference in New Issue
Block a user