aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-06-13 10:23:04 -0400
committerGitHub <noreply@github.com>2020-06-13 10:23:04 -0400
commit8a7ff6a18c78bbc2048dd4017597ccc4f5ee4106 (patch)
tree58333fc1a5a3da13180e8d0bbd5e65d79b34fa27 /test
parent5cba421201b596f4430dd45f3715ad515661b109 (diff)
Replace XMLHttpRequest (#562)
* Replace XMLHttpRequest with fetch * Implement fetch placeholder for tests
Diffstat (limited to 'test')
-rw-r--r--test/test-database.js66
1 files changed, 11 insertions, 55 deletions
diff --git a/test/test-database.js b/test/test-database.js
index e8a4a343..63989857 100644
--- a/test/test-database.js
+++ b/test/test-database.js
@@ -38,60 +38,6 @@ const chrome = {
}
};
-class XMLHttpRequest {
- constructor() {
- this._eventCallbacks = new Map();
- this._url = '';
- this._responseText = null;
- }
-
- overrideMimeType() {
- // NOP
- }
-
- addEventListener(eventName, callback) {
- let callbacks = this._eventCallbacks.get(eventName);
- if (typeof callbacks === 'undefined') {
- callbacks = [];
- this._eventCallbacks.set(eventName, callbacks);
- }
- callbacks.push(callback);
- }
-
- open(action, url2) {
- this._url = url2;
- }
-
- send() {
- const filePath = url.fileURLToPath(this._url);
- Promise.resolve()
- .then(() => {
- let source;
- try {
- source = fs.readFileSync(filePath, {encoding: 'utf8'});
- } catch (e) {
- this._trigger('error');
- return;
- }
- this._responseText = source;
- this._trigger('load');
- });
- }
-
- get responseText() {
- return this._responseText;
- }
-
- _trigger(eventName, ...args) {
- const callbacks = this._eventCallbacks.get(eventName);
- if (typeof callbacks === 'undefined') { return; }
-
- for (let i = 0, ii = callbacks.length; i < ii; ++i) {
- callbacks[i](...args);
- }
- }
-}
-
class Image {
constructor() {
this._src = '';
@@ -138,11 +84,21 @@ class Image {
}
}
+async function fetch(url2) {
+ const filePath = url.fileURLToPath(url2);
+ await Promise.resolve();
+ const content = fs.readFileSync(filePath, {encoding: null});
+ return {
+ text: async () => Promise.resolve(content.toString('utf8')),
+ json: async () => Promise.resolve(JSON.parse(content.toString('utf8')))
+ };
+}
+
const vm = new VM({
chrome,
Image,
- XMLHttpRequest,
+ fetch,
indexedDB: global.indexedDB,
IDBKeyRange: global.IDBKeyRange,
JSZip: yomichanTest.JSZip,