Skip to content

Commit f24e665

Browse files
author
Preben Ludviksen
committed
Added integration tests for RN module, and runner configuration for iOS.
1 parent 493f158 commit f24e665

File tree

9 files changed

+517
-33
lines changed

9 files changed

+517
-33
lines changed

tests/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ buck-out/
3939
\.buckd/
4040
android/app/libs
4141
android/keystores/debug.keystore
42+
43+
modules_copy/
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import React from 'react';
2+
import ReactNative from 'react-native';
3+
import LoggingTestModule from './LoggingTestModule';
4+
import FileSystem from '../modules_copy/FileSystem'
5+
6+
var View = ReactNative.View;
7+
var TestModule = ReactNative.NativeModules.TestModule;
8+
var invariant = require('fbjs/lib/invariant');
9+
10+
async function testWriteAndReadAndDelete() {
11+
const filename = 'my-file.txt';
12+
let fileExists = await FileSystem.fileExists(filename);
13+
LoggingTestModule.assertFalse(fileExists, 'File should not exist at this point.');
14+
15+
const myContent = 'This is my content.';
16+
await FileSystem.writeToFile(filename, myContent);
17+
fileExists = await FileSystem.fileExists(filename);
18+
LoggingTestModule.assertTrue(fileExists, 'File should exist at this point.');
19+
fileExists = await FileSystem.fileExists(filename, FileSystem.storage.important);
20+
LoggingTestModule.assertFalse(fileExists, 'File should only exist in backedUp storage.');
21+
22+
const readBackContent = await FileSystem.readFile(filename);
23+
LoggingTestModule.assertEqual(readBackContent, myContent);
24+
25+
await FileSystem.delete(filename);
26+
fileExists = await FileSystem.fileExists(filename);
27+
LoggingTestModule.assertFalse(fileExists, 'File should be deleted at this point.');
28+
}
29+
30+
async function testFileAndFolderExistence() {
31+
const directoryName = 'my-folder';
32+
const fileName = 'my-file.txt';
33+
const filePath = `${directoryName}/${fileName}`;
34+
35+
let directoryExists =
36+
await FileSystem.directoryExists(directoryName, FileSystem.storage.auxiliary);
37+
let fileExists = await FileSystem.fileExists(filePath, FileSystem.storage.auxiliary);
38+
LoggingTestModule.assertFalse(directoryExists, 'Directory should not exist at this point.');
39+
LoggingTestModule.assertFalse(fileExists, 'File should not exist at this point.');
40+
41+
await FileSystem.writeToFile(filePath, 'My content', FileSystem.storage.auxiliary);
42+
directoryExists = await FileSystem.directoryExists(directoryName, FileSystem.storage.auxiliary);
43+
fileExists = await FileSystem.fileExists(filePath, FileSystem.storage.auxiliary);
44+
LoggingTestModule.assertTrue(directoryExists, 'Directory should exist at this point.');
45+
LoggingTestModule.assertTrue(fileExists, 'File should exist at this point.');
46+
directoryExists = await FileSystem.directoryExists(filePath, FileSystem.storage.auxiliary);
47+
fileExists = await FileSystem.fileExists(directoryName, FileSystem.storage.auxiliary);
48+
LoggingTestModule.assertFalse(directoryExists, 'Files should not be recognized as directories.');
49+
LoggingTestModule.assertFalse(fileExists, 'Directories should not be recognized as files.');
50+
51+
await FileSystem.delete(directoryName, FileSystem.storage.auxiliary);
52+
directoryExists = await FileSystem.directoryExists(directoryName, FileSystem.storage.auxiliary);
53+
fileExists = await FileSystem.fileExists(filePath, FileSystem.storage.auxiliary);
54+
LoggingTestModule.assertFalse(directoryExists, 'Directory should be deleted at this point.');
55+
LoggingTestModule.assertFalse(fileExists, 'File should be deleted at this point.');
56+
}
57+
58+
class FileSystemTest extends React.Component {
59+
60+
componentDidMount() {
61+
this.runTests(); }
62+
63+
async runTests() {
64+
try {
65+
await testWriteAndReadAndDelete();
66+
await testFileAndFolderExistence();
67+
} catch (error) {
68+
LoggingTestModule.logErrorToConsole(error);
69+
TestModule.markTestPassed(false);
70+
return;
71+
}
72+
TestModule.markTestPassed(true);
73+
}
74+
75+
render() {
76+
return <View />;
77+
}
78+
}
79+
80+
FileSystemTest.displayName = 'FileSystemTest';
81+
82+
module.exports = FileSystemTest;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import React from 'react';
2+
import ReactNative from 'react-native';
3+
var {
4+
AppRegistry,
5+
ScrollView,
6+
StyleSheet,
7+
Text,
8+
TouchableOpacity,
9+
View,
10+
} = ReactNative;
11+
12+
var TESTS = [
13+
require('./FileSystemTest'),
14+
];
15+
16+
TESTS.forEach(
17+
(test) => AppRegistry.registerComponent(test.displayName, () => test)
18+
);
19+
20+
class IntegrationTestsApp extends React.Component {
21+
state = {
22+
test: null,
23+
};
24+
25+
render() {
26+
if (this.state.test) {
27+
return (
28+
<ScrollView>
29+
<this.state.test />
30+
</ScrollView>
31+
);
32+
}
33+
return (
34+
<View style={styles.container}>
35+
<Text style={styles.row}>
36+
Click on a test to run it in this shell for easier debugging and
37+
development. Run all tests in the testing environment with cmd+U in
38+
Xcode.
39+
</Text>
40+
<View style={styles.separator} />
41+
<ScrollView>
42+
{TESTS.map((test) => [
43+
<TouchableOpacity
44+
onPress={() => this.setState({test})}
45+
style={styles.row}>
46+
<Text style={styles.testName}>
47+
{test.displayName}
48+
</Text>
49+
</TouchableOpacity>,
50+
<View style={styles.separator} />
51+
])}
52+
</ScrollView>
53+
</View>
54+
);
55+
}
56+
}
57+
58+
var styles = StyleSheet.create({
59+
container: {
60+
backgroundColor: 'white',
61+
marginTop: 40,
62+
margin: 15,
63+
},
64+
row: {
65+
padding: 10,
66+
},
67+
testName: {
68+
fontWeight: '500',
69+
},
70+
separator: {
71+
height: 1,
72+
backgroundColor: '#bbbbbb',
73+
},
74+
});
75+
76+
AppRegistry.registerComponent('IntegrationTestsApp', () => IntegrationTestsApp);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule LoggingTestModule
10+
*/
11+
'use strict';
12+
13+
var BatchedBridge = require('BatchedBridge');
14+
15+
var warning = require('fbjs/lib/warning');
16+
var invariant = require('fbjs/lib/invariant');
17+
18+
export default class LoggingTestModule {
19+
static logToConsole(str) {
20+
console.log(str);
21+
}
22+
static warning(str) {
23+
warning(false, str);
24+
}
25+
static assertTrue(condition, str) {
26+
if (!condition) {
27+
this.logErrorToConsole(str);
28+
}
29+
}
30+
31+
static assertFalse(condition, str) {
32+
if (condition) {
33+
this.logErrorToConsole(str);
34+
}
35+
}
36+
37+
static assertEqual(obj1, obj2) {
38+
if (obj1 !== obj2) {
39+
this.logErrorToConsole(`Objects not equal: '${obj1}' and '${obj2}'`);
40+
}
41+
}
42+
43+
static logErrorToConsole(str) {
44+
console.error(str);
45+
}
46+
static throwError(str) {
47+
throw new Error(str);
48+
}
49+
};
50+
51+
BatchedBridge.registerCallableModule(
52+
'LoggingTestModule',
53+
LoggingTestModule
54+
);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>BNDL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleVersion</key>
20+
<string>1</string>
21+
</dict>
22+
</plist>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#import <XCTest/XCTest.h>
2+
#import "RCTTestRunner.h"
3+
4+
#define RCT_TEST(name) \
5+
- (void)test##name \
6+
{ \
7+
[_runner runTest:_cmd module:@#name]; \
8+
}
9+
10+
@interface RNFileSystemIntegrationTests : XCTestCase
11+
12+
@end
13+
14+
@implementation RNFileSystemIntegrationTests
15+
{
16+
RCTTestRunner *_runner;
17+
}
18+
19+
- (void)setUp {
20+
setenv("CI_USE_PACKAGER", "TRUE", true);
21+
_runner = RCTInitRunnerForApp(@"integration-test/IntegrationTestsApp", nil);
22+
}
23+
24+
- (void)tearDown {
25+
// Put teardown code here. This method is called after the invocation of each test method in the class.
26+
[super tearDown];
27+
}
28+
29+
- (void)testExample {
30+
// This is an example of a functional test case.
31+
// Use XCTAssert and related functions to verify your tests produce the correct results.
32+
}
33+
34+
RCT_TEST(FileSystemTest)
35+
36+
@end

0 commit comments

Comments
 (0)