Skip to content

Commit 44bf37f

Browse files
author
Preben Ludviksen
committed
Updated unit and integration tests to verify new absolutePath-method.
1 parent 8cebde2 commit 44bf37f

File tree

6 files changed

+83
-7
lines changed

6 files changed

+83
-7
lines changed

tests/android/app/src/test/java/com/benwixen/rnfilesystem/FakeAndroidContext.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,7 @@ public File[] getObbDirs() {
158158

159159
@Override
160160
public File getCacheDir() {
161-
File cacheDir = new File("cache");
162-
if (!cacheDir.exists()) {
163-
cacheDir.mkdir();
164-
}
165-
return cacheDir;
161+
return null;
166162
}
167163

168164
@Override

tests/android/app/src/test/java/com/benwixen/rnfilesystem/FakeApplicationContext.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,20 @@ public FakeApplicationContext() {
1212

1313
@Override
1414
public File getFilesDir() {
15-
File cacheDir = new File("test-fs/files");
15+
File cacheDir = new File("test-fs/files/");
1616
if (!cacheDir.exists()) {
1717
cacheDir.mkdirs();
1818
}
1919
return cacheDir;
2020
}
21+
22+
@Override
23+
public File getCacheDir() {
24+
File cacheDir = new File("test-fs/cache/");
25+
if (!cacheDir.exists()) {
26+
cacheDir.mkdir();
27+
}
28+
return cacheDir;
29+
}
2130
}
2231

tests/android/app/src/test/java/com/benwixen/rnfilesystem/RNFileSystemUnitTests.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class RNFileSystemUnitTests {
1212

1313
@Test
14-
public void testy() throws IOException {
14+
public void testWriteReadAndDelete() throws IOException {
1515
RNFileSystem fileSystem = new RNFileSystem(new FakeApplicationContext());
1616

1717
String fileName = "my-file.txt";
@@ -33,4 +33,43 @@ public void testy() throws IOException {
3333
fileExists = fileSystem.fileExists(fileName, RNFileSystem.Storage.BACKED_UP);
3434
assertFalse(fileExists);
3535
}
36+
37+
@Test
38+
public void testFileAndFolderExistence() throws IOException {
39+
RNFileSystem fileSystem = new RNFileSystem(new FakeApplicationContext());
40+
41+
String folderName = "my-folder";
42+
String fileName = "my-file.txt";
43+
String filePath = folderName + "/" + fileName;
44+
45+
boolean directoryExists =
46+
fileSystem.directoryExists(folderName, RNFileSystem.Storage.AUXILIARY);
47+
boolean fileExists = fileSystem.fileExists(filePath, RNFileSystem.Storage.AUXILIARY);
48+
assertFalse(directoryExists);
49+
assertFalse(fileExists);
50+
51+
fileSystem.writeToFile(filePath, "My content.", RNFileSystem.Storage.AUXILIARY);
52+
directoryExists = fileSystem.directoryExists(folderName, RNFileSystem.Storage.AUXILIARY);
53+
fileExists = fileSystem.fileExists(filePath, RNFileSystem.Storage.AUXILIARY);
54+
assertTrue(directoryExists);
55+
assertTrue(fileExists);
56+
directoryExists = fileSystem.directoryExists(filePath, RNFileSystem.Storage.AUXILIARY);
57+
fileExists = fileSystem.fileExists(folderName, RNFileSystem.Storage.AUXILIARY);
58+
assertFalse(directoryExists);
59+
assertFalse(fileExists);
60+
61+
fileSystem.deleteFileOrDirectory(folderName, RNFileSystem.Storage.AUXILIARY);
62+
directoryExists = fileSystem.directoryExists(folderName, RNFileSystem.Storage.AUXILIARY);
63+
fileExists = fileSystem.fileExists(filePath, RNFileSystem.Storage.AUXILIARY);
64+
assertFalse(directoryExists);
65+
assertFalse(fileExists);
66+
}
67+
68+
@Test
69+
public void testAbsolutePathConstants() {
70+
RNFileSystem fileSystem = new RNFileSystem(new FakeApplicationContext());
71+
String backedUp =
72+
(String) fileSystem.getConstants().get(RNFileSystem.Storage.BACKED_UP.toString());
73+
assertFalse(backedUp.endsWith("/"));
74+
}
3675
}

tests/integration-test/FileSystemTest.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ async function testFileAndFolderExistence() {
5656
LoggingTestModule.assertFalse(fileExists, 'File should be deleted at this point.');
5757
}
5858

59+
function testAbsolutePath() {
60+
const backedUpPath = FileSystem.absolutePath('my-file.txt', FileSystem.storage.backedUp);
61+
LoggingTestModule.assertIncludes(backedUpPath, ['Documents', 'BackedUp']);
62+
const importantPath = FileSystem.absolutePath('my-file.txt', FileSystem.storage.important);
63+
LoggingTestModule.assertIncludes(importantPath, 'Important');
64+
const auxiliaryPath = FileSystem.absolutePath('my-file.txt', FileSystem.storage.auxiliary);
65+
LoggingTestModule.assertIncludes(auxiliaryPath, 'Auxiliary');
66+
const tempPath = FileSystem.absolutePath('my-file.txt', FileSystem.storage.temporary);
67+
LoggingTestModule.assertIncludes(tempPath, '/');
68+
}
69+
5970
class FileSystemTest extends React.Component {
6071

6172
constructor(props) {
@@ -73,6 +84,7 @@ class FileSystemTest extends React.Component {
7384
try {
7485
await testWriteAndReadAndDelete();
7586
await testFileAndFolderExistence();
87+
testAbsolutePath();
7688
} catch (error) {
7789
LoggingTestModule.logErrorToConsole(error);
7890
if (TestModule) {

tests/integration-test/LoggingTestModule.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ export default class LoggingTestModule {
4040
}
4141
}
4242

43+
static assertIncludes(obj, subelemnt) {
44+
if (subelemnt instanceof Array) {
45+
for (const subel of subelemnt) {
46+
if (obj.includes(subel)) {
47+
return;
48+
}
49+
}
50+
this.logErrorToConsole(`Object '${obj}' does not include any of ${subelemnt}`);
51+
} else {
52+
if (!obj.includes(subelemnt)) {
53+
this.logErrorToConsole(`Object '${obj}' does not include '${subelemnt}'`);
54+
}
55+
}
56+
}
57+
4358
static logErrorToConsole(str) {
4459
console.error(str);
4560
}

tests/ios/RNFileSystemTestsTests/Tests.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,10 @@ - (void)testFileAndFolderExistence {
6262
XCTAssertFalse(fileExists);
6363
}
6464

65+
- (void)testAbsolutePathConstants {
66+
RNFileSystem *fileSystem = [[RNFileSystem alloc] init];
67+
NSString *backedUp = [[fileSystem constantsToExport] valueForKey:STORAGE_BACKED_UP];
68+
XCTAssertFalse([backedUp hasSuffix:@"/"]);
69+
}
6570

6671
@end

0 commit comments

Comments
 (0)