From 0188517e74083c709b2a1052993d85267ea51241 Mon Sep 17 00:00:00 2001 From: Scott Kyle Date: Mon, 15 Feb 2016 14:37:19 -0800 Subject: [PATCH] Add test for ListViewDataSource to React Test App --- tests/lib/index.js | 6 ++ tests/react-test-app/index.android.js | 59 +++++++------- tests/react-test-app/index.ios.js | 55 +------------ tests/react-test-app/tests/index.js | 65 +++++++++++++++ tests/react-test-app/tests/listview-test.js | 88 +++++++++++++++++++++ 5 files changed, 189 insertions(+), 84 deletions(-) create mode 100644 tests/react-test-app/tests/index.js create mode 100644 tests/react-test-app/tests/listview-test.js diff --git a/tests/lib/index.js b/tests/lib/index.js index 744f1106..4446b82f 100644 --- a/tests/lib/index.js +++ b/tests/lib/index.js @@ -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]; diff --git a/tests/react-test-app/index.android.js b/tests/react-test-app/index.android.js index a7e2342e..f95f31f8 100644 --- a/tests/react-test-app/index.android.js +++ b/tests/react-test-app/index.android.js @@ -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); diff --git a/tests/react-test-app/index.ios.js b/tests/react-test-app/index.ios.js index de6bb819..eb549b28 100644 --- a/tests/react-test-app/index.ios.js +++ b/tests/react-test-app/index.ios.js @@ -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 ( - + Tap to Run Tests diff --git a/tests/react-test-app/tests/index.js b/tests/react-test-app/tests/index.js new file mode 100644 index 00000000..5fa3966a --- /dev/null +++ b/tests/react-test-app/tests/index.js @@ -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'); + } + } + } +} diff --git a/tests/react-test-app/tests/listview-test.js b/tests/react-test-app/tests/listview-test.js new file mode 100644 index 00000000..3decda9e --- /dev/null +++ b/tests/react-test-app/tests/listview-test.js @@ -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); + } + }, +};