forked from filerjs/filer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-utils.js
195 lines (165 loc) · 3.86 KB
/
test-utils.js
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
193
194
195
var Filer = require('../../src');
var IndexedDBTestProvider = require('./indexeddb.js');
var MemoryTestProvider = require('./memory.js');
var Url = require('url');
var _provider;
var _fs;
function uniqueName() {
if(!uniqueName.seed) {
uniqueName.seed = Date.now();
}
return 'filer-testdb-' + uniqueName.seed++;
}
function findBestProvider() {
var providers = Filer.FileSystem.providers;
return providers.IndexedDB.isSupported() ?
IndexedDBTestProvider : MemoryTestProvider;
}
function getUrlParams() {
// Check if we are running in node
if(!global.location) {
return null;
}
var url = Url.parse(global.location.href, true);
return url.query;
}
function getProviderType() {
var defaultProvider = 'default';
var queryString = getUrlParams();
// If the environment is node or the query string is empty,
// the memory provider will be used.
if(!queryString) {
return defaultProvider;
}
return queryString['filer-provider'] || defaultProvider;
}
// Run fn() in an environment with indexedDB available
// either as-is, or shimmed, removing when done.
function shimIndexedDB(fn) {
var addShim = !Filer.FileSystem.providers.IndexedDB.isSupported();
if(addShim) {
global.indexedDB = require('fake-indexeddb');
}
var result = fn();
if(addShim) {
delete global.indexedDB;
}
return result;
}
function setup(callback) {
// In browser we support specifying the provider via the query string
// (e.g., ?filer-provider=IndexedDB). If not specified, we use
// the Memory provider by default.
var providerType = getProviderType();
var name = uniqueName();
switch(providerType.toLowerCase()) {
case 'indexeddb':
_provider = new IndexedDBTestProvider(name);
break;
case 'memory':
_provider = new MemoryTestProvider(name);
break;
case 'default':
default:
var BestProvider = findBestProvider();
_provider = new BestProvider(name);
break;
}
// Allow passing FS flags on query string
var flags = global.filerArgs && global.filerArgs.flags ?
global.filerArgs.flags : 'FORMAT';
// Create a file system and wait for it to get setup
_provider.init();
function complete(err, fs) {
if(err) throw err;
_fs = fs;
callback();
}
return new Filer.FileSystem({
name: name,
provider: _provider.provider,
flags: flags
}, complete);
}
function fs() {
if(!_fs) {
throw new Error('TestUtil: call setup() before fs()');
}
return _fs;
}
function provider() {
if(!_provider) {
throw new Error('TestUtil: call setup() before provider()');
}
return _provider;
}
function shell(options) {
var _fs = fs();
return new _fs.Shell(options);
}
function cleanup(callback) {
if(!_provider) {
return;
}
_provider.cleanup(function() {
_provider = null;
_fs = null;
callback();
});
}
function typedArrayEqual(a, b) {
if(!(a && b)) {
return false;
}
if(a.length !== b.length) {
return false;
}
for(var i = 0; i < a.length; ++ i) {
if(a[i] !== b[i]) {
return false;
}
}
return true;
}
/**
* Parse JSON with serialized Buffers
*/
const parseBJSON = json =>
JSON.parse(json, (key, value) =>
value && value.type === 'Buffer' ?
Buffer.from(value.data) :
value
);
function createMockFn(implementation = undefined) {
const calls = [];
const mockFn = function(...args) {
calls.push({
args,
});
if (typeof implementation === 'function') {
return implementation(...args);
}
};
Object.defineProperty(mockFn, 'calls', {
get() {
return calls;
}
});
return mockFn;
}
module.exports = {
uniqueName: uniqueName,
setup: setup,
fs: fs,
shell: shell,
provider: provider,
providers: {
IndexedDB: IndexedDBTestProvider,
Memory: MemoryTestProvider
},
cleanup: cleanup,
typedArrayEqual: typedArrayEqual,
parseBJSON,
shimIndexedDB,
createMockFn
};