UNPKG

3.12 kBJavaScriptView Raw
1
2'use strict';
3
4var RCTTestModule = require('NativeModules').TestModule;
5var React = require('react-native');
6var createClass = require('create-react-class');
7
8var {
9 Text,
10 View,
11} = React;
12var RNFS = require('react-native-fs');
13var DEBUG = false;
14
15
16// setup in componentDidMount
17var done;
18var updateMessage;
19
20function runTestCase(description, fn) {
21 updateMessage(description);
22 fn();
23}
24
25function expectTrue(condition, message) {
26 if (!condition) {
27 throw new Error(message);
28 }
29}
30
31function expectEqual(lhs, rhs, testname) {
32 expectTrue(
33 lhs === rhs,
34 'Error in test ' + testname + ': expected ' + rhs + ', got ' + lhs
35 );
36}
37
38function expectFSNoError(err) {
39 expectTrue(err === null, 'Unexpected FS error: ' + JSON.stringify(err));
40}
41
42function testWriteAndReadFile() {
43 var path = RNFS.DocumentDirectoryPath + '/test.txt';
44
45 var text = 'Lorem ipsum dolor sit amet';
46 var readText;
47
48 RNFS.writeFile(path, text)
49 .then((success) => {
50 updateMessage('FILE WRITTEN!');
51 return RNFS.readFile(path);
52 })
53 .then((contents) => {
54 updateMessage('FILE READ! Contents:');
55 readText = contents;
56 expectEqual(text, readText, 'testWriteAndReadFile');
57 updateMessage('readFile correctly returned' + readText);
58 })
59 .finally(() => {
60 runTestCase('testCreateAndDeleteFile', testCreateAndDeleteFile);
61 })
62 .done();//promise done needed to throw exception so that in case test fails,error is propagated
63}
64
65
66
67function testCreateAndDeleteFile() {
68 var path = RNFS.DocumentDirectoryPath + '/test.txt';
69 var text = 'Lorem ipsum dolor sit amet';
70 var readText;
71
72 RNFS.writeFile(path, text)
73 .then((success) => {
74 updateMessage('FILE CREATED!');
75 return RNFS.unlink(path);
76 })
77 .spread((success, path) => {
78 updateMessage('FILE DELETED!' + success + ',' + path);
79 return RNFS.stat(path);
80 })
81 .then((statResult) => {
82 updateMessage('*****' + statResult);
83 if (statResult.isFile()) {
84 updateMessage('FILE STILL EXISTS');
85 }
86 })
87 .catch((err) => {
88 updateMessage('catch' + err);
89 expectTrue(true,'File is deleted');
90 })
91 .finally(() => {
92 done(); //testrunners done
93 })
94 .done(); //promise done needed to throw exception so that in case test fails,error is propagated
95}
96
97
98
99
100var FSTest = createClass({
101 getInitialState() {
102 return {
103 messages: 'Initializing...',
104 done: false,
105 };
106 },
107
108 componentDidMount() {
109 done = () => this.setState({
110 done: true
111 }, RCTTestModule.markTestCompleted);
112 updateMessage = (msg) => {
113 this.setState({
114 messages: this.state.messages.concat('\n' + msg)
115 });
116 DEBUG && console.log(msg);
117 };
118 testWriteAndReadFile();
119 },
120
121 render() {
122 return (
123 <View style={{backgroundColor: 'white', padding: 40}}>
124 <Text>
125 {this.constructor.displayName + ': '}
126 {this.state.done ? 'Done' : 'Testing...'}
127 {'\n\n' + this.state.messages}
128 </Text>
129 </View>
130 );
131 }
132});
133
134module.exports = FSTest;