[FIX] web: fix file upload service event listeners

The event listeners were added to the upload object instead of the xhr
object making them being called before they were supposed to.

closes odoo/odoo#102061

X-original-commit: 804467d7b0a583ab36a47212d482b41d29ca2203
Related: odoo/enterprise#32247
Signed-off-by: William Braeckman (wbr) <wbr@odoo.com>
This commit is contained in:
William Braeckman
2022-10-04 17:01:29 +02:00
parent 9cc35e276d
commit ee9a282c19
3 changed files with 15 additions and 15 deletions
@@ -33,11 +33,12 @@ QUnit.module('MrpDocumentsKanbanView', {
this.ORIGINAL_CREATE_XHR = fileUploadService.createXhr;
this.patchDocumentXHR = (mockedXHRs, customSend) => {
fileUploadService.createXhr = () => {
const xhr = {
const xhr = new window.EventTarget();
Object.assign(xhr, {
upload: new window.EventTarget(),
open() { },
open() {},
send(data) { customSend && customSend(data); },
};
});
mockedXHRs.push(xhr);
return xhr;
};
@@ -68,13 +68,13 @@ export const fileUploadService = {
upload.state = "loading";
});
// Load listener
xhr.upload.addEventListener("load", async () => {
xhr.addEventListener("load", () => {
delete uploads[upload.id];
upload.state = "loaded";
bus.trigger("FILE_UPLOAD_LOADED", { upload });
});
// Error listener
xhr.upload.addEventListener("error", async () => {
xhr.addEventListener("error", async () => {
delete uploads[upload.id];
upload.state = "error";
// Disable this option if you need more explicit error handling.
@@ -87,7 +87,7 @@ export const fileUploadService = {
bus.trigger("FILE_UPLOAD_ERROR", { upload });
});
// Abort listener, considered as error
xhr.upload.addEventListener("abort", async () => {
xhr.addEventListener("abort", async () => {
delete uploads[upload.id];
upload.state = "abort";
bus.trigger("FILE_UPLOAD_ERROR", { upload });
@@ -49,13 +49,12 @@ QUnit.module("Components", ({ beforeEach }) => {
patchUpload = (customSend) => {
patchWithCleanup(fileUploadService, {
createXhr() {
const xhr = {
const xhr = new window.EventTarget();
Object.assign(xhr, {
upload: new window.EventTarget(),
open() {},
send(data) {
customSend && customSend(data);
},
};
send(data) { customSend && customSend(data); },
});
return xhr;
},
});
@@ -92,7 +91,7 @@ QUnit.module("Components", ({ beforeEach }) => {
const fileUploadService = env.services.file_upload;
fileUploadService.upload("/test/", []);
await nextTick();
fileUploadService.uploads[1].xhr.upload.dispatchEvent(new Event("load"));
fileUploadService.uploads[1].xhr.dispatchEvent(new Event("load"));
await nextTick();
assert.containsNone(target, ".file_upload");
});
@@ -104,7 +103,7 @@ QUnit.module("Components", ({ beforeEach }) => {
const fileUploadService = env.services.file_upload;
fileUploadService.upload("/test/", []);
await nextTick();
fileUploadService.uploads[1].xhr.upload.dispatchEvent(new Event("error"));
fileUploadService.uploads[1].xhr.dispatchEvent(new Event("error"));
await nextTick();
assert.containsNone(target, ".file_upload");
});
@@ -116,7 +115,7 @@ QUnit.module("Components", ({ beforeEach }) => {
const fileUploadService = env.services.file_upload;
fileUploadService.upload("/test/", []);
await nextTick();
fileUploadService.uploads[1].xhr.upload.dispatchEvent(new Event("abort"));
fileUploadService.uploads[1].xhr.dispatchEvent(new Event("abort"));
await nextTick();
assert.containsNone(target, ".file_upload");
});
@@ -130,7 +129,7 @@ QUnit.module("Components", ({ beforeEach }) => {
await nextTick();
patchWithCleanup(env.services.dialog, {
add: () => {
fileUploadService.uploads[1].xhr.upload.dispatchEvent(new Event("abort"));
fileUploadService.uploads[1].xhr.dispatchEvent(new Event("abort"));
},
});
await click(target, ".o-file-upload-progress-bar-abort", true);