-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathreact-native-file-access.ts
192 lines (169 loc) · 4.51 KB
/
react-native-file-access.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* global jest */
import { Platform } from 'react-native';
import type {
ExternalDir,
FetchResult,
FileStat,
FsStat,
HashAlgorithm,
Util as UtilFunctions,
} from 'react-native-file-access';
export const Util: typeof UtilFunctions =
require('react-native-file-access/lib/commonjs/util').Util;
export const Dirs = {
CacheDir: '/mock/CacheDir',
DatabaseDir: '/mock/DatabaseDir',
DocumentDir: '/mock/DocumentDir',
LibraryDir: '/mock/LibraryDir',
MainBundleDir: '/mock/MainBundleDir',
};
class FileSystemMock {
/**
* Data store for mock filesystem.
*/
public filesystem = new Map<string, string>();
/**
* Append content to a file.
*/
public appendFile = jest.fn(async (path: string, data: string) => {
this.filesystem.set(path, (this.filesystem.get(path) ?? '') + data);
});
/**
* Append a file to another file.
*
* Returns number of bytes written.
*/
public concatFiles = jest.fn(async (source: string, target: string) => {
const data = this.getFileOrThrow(source);
this.filesystem.set(target, (this.filesystem.get(target) ?? '') + data);
return data.length;
});
/**
* Copy a file.
*/
public cp = jest.fn(async (source: string, target: string) => {
this.filesystem.set(target, this.getFileOrThrow(source));
});
/**
* Copy a file to external storage
*/
public cpExternal = jest.fn(
async (source: string, targetName: string, dir: ExternalDir) => {
this.filesystem.set(`/${dir}/${targetName}`, this.getFileOrThrow(source));
}
);
/**
* Copy a bundled asset file.
*/
public cpAsset = jest.fn(async (asset: string, target: string) => {
this.filesystem.set(target, `[Mock asset data for '${asset}']`);
});
/**
* Check device available space.
*/
public df = jest.fn<Promise<FsStat>, []>(async () => ({
internal_free: 100,
internal_total: 200,
}));
/**
* Check if a path exists.
*/
public exists = jest.fn(async (path: string) => this.filesystem.has(path));
/**
* Save a network request to a file.
*/
public fetch = jest.fn(
async (
resource: string,
init: {
body?: string;
headers?: { [key: string]: string };
method?: string;
path?: string;
}
): Promise<FetchResult> => {
if (init.path != null) {
this.filesystem.set(init.path, `[Mock fetch data for '${resource}']`);
}
return {
headers: {},
ok: true,
redirected: false,
status: 200,
statusText: 'OK',
url: resource,
};
}
);
/**
* Return the local storage directory for app groups.
*
* This is an Apple only feature.
*/
public getAppGroupDir = jest.fn((groupName: string) => {
if (Platform.OS !== 'ios' && Platform.OS !== 'macos') {
throw new Error('AppGroups are available on Apple devices only');
}
return `${Dirs.DocumentDir}/shared/AppGroup/${groupName}`;
});
/**
* Hash the file content.
*/
public hash = jest.fn(async (path: string, algorithm: HashAlgorithm) => {
if (!this.filesystem.has(path)) {
throw new Error(`File ${path} not found`);
}
return `[${algorithm} hash of '${path}']`;
});
/**
* Check if a path is a directory.
*/
public isDir = jest.fn(async (path: string) => !this.filesystem.has(path));
/**
* List files in a directory.
*/
public ls = jest.fn(async (_path: string) => ['file1', 'file2']);
/**
* Move a file.
*/
public mv = jest.fn(async (source: string, target: string) => {
this.filesystem.set(target, this.getFileOrThrow(source));
this.filesystem.delete(source);
});
/**
* Read the content of a file.
*/
public readFile = jest.fn(async (path: string) => this.getFileOrThrow(path));
/**
* Read file metadata.
*/
public stat = jest.fn(
async (path: string): Promise<FileStat> => ({
filename: path.substring(path.lastIndexOf('/')),
lastModified: 1,
path: path,
size: this.getFileOrThrow(path).length,
type: 'file',
})
);
/**
* Delete a file.
*/
public unlink = jest.fn(async (path: string) => {
this.filesystem.delete(path);
});
/**
* Write content to a file.
*/
public writeFile = jest.fn(async (path: string, data: string) => {
this.filesystem.set(path, data);
});
private getFileOrThrow(path: string): string {
const data = this.filesystem.get(path);
if (data == null) {
throw new Error(`File ${path} not found`);
}
return data;
}
}
export const FileSystem = new FileSystemMock();