apple2js/test/env/jsdom-with-backdoors.js
Ian Flanigan 52a1c65fe4
Create a FileChooser component using showOpenFilePicker (#116)
* Create a FileChooser component using showOpenFilePicker

Before, `FileModal` always used a file input control for selecting
local files. This allowed the emulator to read from the file, but
precluded writing back to the file.

With this change, the `FileModal` delegates to the new `FileChooser`
component. The `FileChooser` will use `showOpenFilePicker` if it is
available and a regular file input if it's not.

Using `showOpenFilePicker` has the advantage of allowing the emulator
to write back to the file (if the user grants permission). While the
emulator does not yet take advantage of this write capability, that
will come.

* Addressed comments

*   useState() instead of direct DOM manipulation
*   backed out eslint changes in favor of suppressing the warning
2022-05-28 10:52:48 -07:00

35 lines
1.4 KiB
JavaScript

/**
* 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;
},
};
}
}