1 |
|
2 | 'use strict';
|
3 |
|
4 | var RCTTestModule = require('NativeModules').TestModule;
|
5 | var React = require('react-native');
|
6 | var createClass = require('create-react-class');
|
7 |
|
8 | var {
|
9 | Text,
|
10 | View,
|
11 | } = React;
|
12 | var RNFS = require('react-native-fs');
|
13 | var DEBUG = false;
|
14 |
|
15 |
|
16 |
|
17 | var done;
|
18 | var updateMessage;
|
19 |
|
20 | function runTestCase(description, fn) {
|
21 | updateMessage(description);
|
22 | fn();
|
23 | }
|
24 |
|
25 | function expectTrue(condition, message) {
|
26 | if (!condition) {
|
27 | throw new Error(message);
|
28 | }
|
29 | }
|
30 |
|
31 | function expectEqual(lhs, rhs, testname) {
|
32 | expectTrue(
|
33 | lhs === rhs,
|
34 | 'Error in test ' + testname + ': expected ' + rhs + ', got ' + lhs
|
35 | );
|
36 | }
|
37 |
|
38 | function expectFSNoError(err) {
|
39 | expectTrue(err === null, 'Unexpected FS error: ' + JSON.stringify(err));
|
40 | }
|
41 |
|
42 | function 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();
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 | function 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();
|
93 | })
|
94 | .done();
|
95 | }
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | var 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 |
|
134 | module.exports = FSTest;
|