apple2js/test/env/jsdom-with-backdoors.js

35 lines
1.4 KiB
JavaScript
Raw Normal View History

/**
* This is a total and terrible hack that allows us to create otherwise
* uninstantiable jsdom objects. Currently this exposes a way to create
* `FileList` objects.
*
* This was inspired by felipochoa's implementation in GitHub issue:
* https://github.com/jsdom/jsdom/issues/1272. This implementation is
* "better" because it does all of the dirty work during environment
* setup. It still requires typing.
*/
const JsdomEnvironment = require('jest-environment-jsdom');
export default class JsdomEnvironmentWithBackDoors extends JsdomEnvironment {
async setup() {
await super.setup();
const jsdomUtils = require('jsdom/lib/jsdom/living/generated/utils');
const jsdomFileList = require('jsdom/lib/jsdom/living/generated/FileList');
this.global.backdoors = {
newFileList: (...files) => {
const impl = jsdomFileList.createImpl(this.global);
const fileList = Object.assign([...files], {
item: i => fileList[i],
[jsdomUtils.implSymbol]: impl,
});
impl[jsdomUtils.wrapperSymbol] = fileList;
const fileListCtor = this.global[jsdomUtils.ctorRegistrySymbol].FileList;
Object.setPrototypeOf(fileList, fileListCtor.prototype);
return fileList;
},
};
}
}