2023-11-28 17:45:20 +08:00
|
|
|
import { click, render, waitFor } from "@ember/test-helpers";
|
2023-10-11 02:38:59 +08:00
|
|
|
import hbs from "htmlbars-inline-precompile";
|
|
|
|
import { module, test } from "qunit";
|
2022-12-22 21:35:18 +08:00
|
|
|
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
|
2022-11-02 21:41:30 +08:00
|
|
|
import pretender from "discourse/tests/helpers/create-pretender";
|
2024-11-10 09:06:03 +08:00
|
|
|
import { createFile } from "discourse/tests/helpers/qunit-helpers";
|
2022-11-02 21:41:30 +08:00
|
|
|
|
|
|
|
const fakeUpload = {
|
|
|
|
type: ".png",
|
|
|
|
extension: "png",
|
|
|
|
name: "myfile.png",
|
|
|
|
short_path: "/images/avatar.png",
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockUploadResponse = {
|
|
|
|
extension: "jpeg",
|
|
|
|
filesize: 126177,
|
|
|
|
height: 800,
|
|
|
|
human_filesize: "123 KB",
|
|
|
|
id: 202,
|
|
|
|
original_filename: "avatar.PNG.jpg",
|
|
|
|
retain_hours: null,
|
|
|
|
short_path: "/images/avatar.png",
|
|
|
|
short_url: "upload://yoj8pf9DdIeHRRULyw7i57GAYdz.jpeg",
|
|
|
|
thumbnail_height: 320,
|
|
|
|
thumbnail_width: 690,
|
|
|
|
url: "/images/avatar.png",
|
|
|
|
width: 1920,
|
|
|
|
};
|
|
|
|
|
|
|
|
function setupUploadPretender() {
|
|
|
|
pretender.post(
|
|
|
|
"/uploads.json",
|
|
|
|
() => {
|
|
|
|
return [200, { "Content-Type": "application/json" }, mockUploadResponse];
|
|
|
|
},
|
|
|
|
500 // this delay is important to slow down the uploads a bit so we can click elements in the UI like the cancel button
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
module("Discourse Chat | Component | chat-composer-uploads", function (hooks) {
|
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
2022-12-22 21:35:18 +08:00
|
|
|
test("loading uploads from an outside source (e.g. draft or editing message)", async function (assert) {
|
2023-03-03 20:09:25 +08:00
|
|
|
this.existingUploads = [fakeUpload];
|
|
|
|
|
2022-12-22 21:35:18 +08:00
|
|
|
await render(hbs`
|
2023-03-03 20:09:25 +08:00
|
|
|
<ChatComposerUploads @existingUploads={{this.existingUploads}} @fileUploadElementId="chat-widget-uploader" />
|
2022-12-22 21:35:18 +08:00
|
|
|
`);
|
2022-11-02 21:41:30 +08:00
|
|
|
|
2024-11-10 09:06:03 +08:00
|
|
|
assert.dom(".chat-composer-upload").exists({ count: 1 });
|
2022-12-22 21:35:18 +08:00
|
|
|
});
|
2022-11-02 21:41:30 +08:00
|
|
|
|
2022-12-22 21:35:18 +08:00
|
|
|
test("upload starts and completes", async function (assert) {
|
|
|
|
setupUploadPretender();
|
2023-03-03 20:09:25 +08:00
|
|
|
this.set("onUploadChanged", () => {});
|
2022-12-22 21:35:18 +08:00
|
|
|
|
|
|
|
await render(hbs`
|
|
|
|
<ChatComposerUploads @fileUploadElementId="chat-widget-uploader" @onUploadChanged={{this.onUploadChanged}} />
|
|
|
|
`);
|
|
|
|
|
|
|
|
const done = assert.async();
|
2023-05-08 16:48:56 +08:00
|
|
|
this.appEvents = this.container.lookup("service:app-events");
|
2022-12-22 21:35:18 +08:00
|
|
|
this.appEvents.on(
|
|
|
|
"upload-mixin:chat-composer-uploader:upload-success",
|
|
|
|
(fileName, upload) => {
|
|
|
|
assert.strictEqual(fileName, "avatar.png");
|
|
|
|
assert.deepEqual(upload, mockUploadResponse);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
this.appEvents.trigger(
|
|
|
|
"upload-mixin:chat-composer-uploader:add-files",
|
|
|
|
createFile("avatar.png")
|
|
|
|
);
|
|
|
|
|
|
|
|
await waitFor(".chat-composer-upload");
|
2023-03-03 20:09:25 +08:00
|
|
|
|
|
|
|
assert.dom(".chat-composer-upload").exists({ count: 1 });
|
2022-11-02 21:41:30 +08:00
|
|
|
});
|
|
|
|
|
2022-12-22 21:35:18 +08:00
|
|
|
test("removing a completed upload", async function (assert) {
|
|
|
|
this.set("changedUploads", null);
|
2023-03-03 20:09:25 +08:00
|
|
|
this.set("onUploadChanged", () => {});
|
|
|
|
|
|
|
|
this.existingUploads = [fakeUpload];
|
2022-11-02 21:41:30 +08:00
|
|
|
|
2022-12-22 21:35:18 +08:00
|
|
|
await render(hbs`
|
2023-03-03 20:09:25 +08:00
|
|
|
<ChatComposerUploads @existingUploads={{this.existingUploads}} @fileUploadElementId="chat-widget-uploader" @onUploadChanged={{this.onUploadChanged}} />
|
2022-12-22 21:35:18 +08:00
|
|
|
`);
|
2022-11-02 21:41:30 +08:00
|
|
|
|
2023-03-03 20:09:25 +08:00
|
|
|
assert.dom(".chat-composer-upload").exists({ count: 1 });
|
2022-11-02 21:41:30 +08:00
|
|
|
|
2023-04-25 16:23:03 +08:00
|
|
|
await click(".chat-composer-upload__remove-btn");
|
2023-03-03 20:09:25 +08:00
|
|
|
|
|
|
|
assert.dom(".chat-composer-upload").exists({ count: 0 });
|
2022-11-02 21:41:30 +08:00
|
|
|
});
|
|
|
|
|
2022-12-22 21:35:18 +08:00
|
|
|
test("cancelling in progress upload", async function (assert) {
|
|
|
|
setupUploadPretender();
|
|
|
|
|
|
|
|
this.set("changedUploads", null);
|
|
|
|
this.set("onUploadChanged", (uploads) => {
|
|
|
|
this.set("changedUploads", uploads);
|
|
|
|
});
|
|
|
|
|
|
|
|
await render(hbs`
|
|
|
|
<ChatComposerUploads @fileUploadElementId="chat-widget-uploader" @onUploadChanged={{this.onUploadChanged}} />
|
|
|
|
`);
|
|
|
|
|
|
|
|
const image = createFile("avatar.png");
|
|
|
|
const done = assert.async();
|
2023-05-08 16:48:56 +08:00
|
|
|
this.appEvents = this.container.lookup("service:app-events");
|
2022-12-22 21:35:18 +08:00
|
|
|
|
|
|
|
this.appEvents.on(
|
|
|
|
`upload-mixin:chat-composer-uploader:upload-cancelled`,
|
|
|
|
(fileId) => {
|
2024-10-28 22:01:44 +08:00
|
|
|
assert.true(
|
|
|
|
fileId.includes("chat-composer-uploader-avatar/"),
|
2022-12-22 21:35:18 +08:00
|
|
|
"upload was cancelled"
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
this.appEvents.trigger(
|
|
|
|
"upload-mixin:chat-composer-uploader:add-files",
|
|
|
|
image
|
|
|
|
);
|
|
|
|
|
|
|
|
await waitFor(".chat-composer-upload");
|
2024-11-10 09:06:03 +08:00
|
|
|
assert.dom(".chat-composer-upload").exists({ count: 1 });
|
2022-12-22 21:35:18 +08:00
|
|
|
|
2023-04-25 16:23:03 +08:00
|
|
|
await click(".chat-composer-upload__remove-btn");
|
2024-11-10 09:06:03 +08:00
|
|
|
assert.dom(".chat-composer-upload").doesNotExist();
|
2022-11-02 21:41:30 +08:00
|
|
|
});
|
|
|
|
});
|