Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/multiple concurrent file uploads #251

Merged
merged 14 commits into from
Jan 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import 'cypress-file-upload';
```

Note: With Typescript, ensure the following is in your `cypress\tsconfig.json` file:

abramenal marked this conversation as resolved.
Show resolved Hide resolved
```
"compilerOptions": {
"types": ["cypress", "cypress-file-upload"]
Expand All @@ -59,6 +60,11 @@ cy.get('[data-cy="file-input"]')
.attachFile(yourFixturePath)
.attachFile(yourBestPicture);

/* You can also attach multiple files concurrently to avoid triggering multiple events */

const yourBestPicture = 'meow.png';
cy.get('[data-cy="file-input"]').attachFile([yourFixturePath, yourBestPicture]);

/* If your file encoding is not supported out of the box, make sure to pass it explicitly */

const weirdo = 'test.shp';
Expand All @@ -78,18 +84,23 @@ cy.get('[data-cy="file-input"]').attachFile({ filePath: data, fileName: 'users.j
const special = 'file.spss';
cy.fixture(special, 'binary')
.then(Cypress.Blob.binaryStringToBlob)
.then((fileContent) => {
.then(fileContent => {
cy.get('[data-cy="file-input"]').attachFile({ fileContent, filePath: special, encoding: 'utf-8' });
})
});

/* when providing fileContent is possible to ignore filePath but fileName and mime type must be provided */

const special = 'file.spss';
cy.fixture(special, 'binary')
.then(Cypress.Blob.binaryStringToBlob)
.then((fileContent) => {
cy.get('[data-cy="file-input"]').attachFile({ fileContent, fileName: 'special', mimeType: 'application/octet-stream', encoding: 'utf-8' });
})
.then(fileContent => {
cy.get('[data-cy="file-input"]').attachFile({
fileContent,
fileName: 'special',
mimeType: 'application/octet-stream',
encoding: 'utf-8',
});
});
```

**Trying to upload a file that does not supported by Cypress by default?** Make sure you pass `encoding` property (see [API](#api)).
Expand Down Expand Up @@ -222,6 +233,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/all-contri

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
Expand Down
23 changes: 23 additions & 0 deletions lib/dom/dispatchEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @description dispatches custom event with dataTransfer object provided
*
* @param {HTMLElement} target
* @param {string} event
* @param {DataTransfer} dataTransfer
*/
export default function dispatchEvent(target, event, dataTransfer) {
const eventPayload = {
bubbles: true,
cancelable: true,
detail: dataTransfer,
};

try {
const e = new CustomEvent(event, eventPayload);
Object.assign(e, { dataTransfer });

target.dispatchEvent(e);
} catch (e) {
// make sure event triggering won't break if subject element is not visible or in DOM anymore
}
}
1 change: 1 addition & 0 deletions lib/dom/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as dispatchEvent } from './dispatchEvent';
export { default as isElementVisible } from './isElementVisible';
export { default as isShadowElement } from './isShadowElement';
1 change: 1 addition & 0 deletions lib/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { default as getFileBlobAsync } from './getFileBlobAsync';
export { default as getFileEncoding } from './getFileEncoding';
export { default as getFileMimeType } from './getFileMimeType';
export { default as getFileContent } from './getFileContent';
export { default as resolveFile } from './resolveFile';
29 changes: 29 additions & 0 deletions lib/file/resolveFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import getFileContent from './getFileContent';
import getFileMimeType from './getFileMimeType';
import getFileEncoding from './getFileEncoding';
import getFileBlobAsync from './getFileBlobAsync';

export default function resolveFile(fixture, window) {
const { filePath, encoding, mimeType, fileName } = fixture;

const fileMimeType = mimeType || getFileMimeType(filePath);
const fileEncoding = encoding || getFileEncoding(filePath);

return new Cypress.Promise(resolve =>
getFileContent({
filePath,
fileContent: fixture.fileContent,
fileEncoding,
})
.then(fileContent =>
getFileBlobAsync({
fileContent,
fileName,
mimeType: fileMimeType,
encoding: fileEncoding,
window,
}),
)
.then(resolve),
);
}
Loading