In Maputnik we use two ways to open a file.
- Using the
showOpenFilePicker which allows opening a file and writing back to it, which is very good for the use case of this app
- When the above doesn't exist, like in firefox, we use the regular
<label><a/><input type="file"/></label>, which exited since the dawn of history.
To overcome be able to test this we use the following helper method - it receives the fixture name (file), the button test id, the input test id and checks which approach should be mocked.
This is method is browser agnostic and work well on firefox and chrome alike.
openFileByFixture: (fixture: string, buttonTestId: string, inputTestId: string) => {
cy.window().then((win) => {
const file = {
text: cy.stub().resolves(cy.fixture(fixture).then(JSON.stringify)),
}
const fileHandle = {
getFile: cy.stub().resolves(file),
}
if (!win.showOpenFilePicker) {
this.helper.get.elementByTestId(inputTestId).selectFile("cypress/fixtures/" + fixture, { force: true });
} else {
cy.stub(win, 'showOpenFilePicker').resolves([fileHandle])
this.helper.get.elementByTestId(buttonTestId).click();
}
});
},
I'm proposing to add this method to this library if there's an interest.
In Maputnik we use two ways to open a file.
showOpenFilePickerwhich allows opening a file and writing back to it, which is very good for the use case of this app<label><a/><input type="file"/></label>, which exited since the dawn of history.To overcome be able to test this we use the following helper method - it receives the fixture name (file), the button test id, the input test id and checks which approach should be mocked.
This is method is browser agnostic and work well on firefox and chrome alike.
I'm proposing to add this method to this library if there's an interest.