2022-12-22 08:35:18 -05:00
|
|
|
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
|
2022-11-02 09:41:30 -04:00
|
|
|
import { exists, query } from "discourse/tests/helpers/qunit-helpers";
|
|
|
|
import hbs from "htmlbars-inline-precompile";
|
2022-12-22 08:35:18 -05:00
|
|
|
import { module, test } from "qunit";
|
|
|
|
import { render, settled } from "@ember/test-helpers";
|
2022-11-02 09:41:30 -04:00
|
|
|
|
|
|
|
const IMAGE_FIXTURE = {
|
|
|
|
id: 290,
|
|
|
|
url: null, // Nulled out to avoid actually setting the img src - avoids an HTTP request
|
|
|
|
original_filename: "image.jpg",
|
|
|
|
filesize: 172214,
|
|
|
|
width: 1024,
|
|
|
|
height: 768,
|
|
|
|
thumbnail_width: 666,
|
|
|
|
thumbnail_height: 500,
|
|
|
|
extension: "jpeg",
|
|
|
|
short_url: "upload://mnCnqY5tunCFw2qMgtPnu1mu1C9.jpeg",
|
|
|
|
short_path: "/uploads/short-url/mnCnqY5tunCFw2qMgtPnu1mu1C9.jpeg",
|
|
|
|
retain_hours: null,
|
|
|
|
human_filesize: "168 KB",
|
|
|
|
dominant_color: "788370", // rgb(120, 131, 112)
|
|
|
|
};
|
|
|
|
|
|
|
|
const VIDEO_FIXTURE = {
|
|
|
|
id: 290,
|
2023-02-06 01:00:03 -05:00
|
|
|
url: null, // Nulled out to avoid actually setting the src - avoids an HTTP request
|
2022-11-02 09:41:30 -04:00
|
|
|
original_filename: "video.mp4",
|
|
|
|
filesize: 172214,
|
|
|
|
width: 1024,
|
|
|
|
height: 768,
|
|
|
|
thumbnail_width: 666,
|
|
|
|
thumbnail_height: 500,
|
|
|
|
extension: "mp4",
|
|
|
|
short_url: "upload://mnCnqY5tunCFw2qMgtPnu1mu1C9.mp4",
|
|
|
|
short_path: "/uploads/short-url/mnCnqY5tunCFw2qMgtPnu1mu1C9.mp4",
|
|
|
|
retain_hours: null,
|
|
|
|
human_filesize: "168 KB",
|
|
|
|
};
|
|
|
|
|
2023-02-06 01:00:03 -05:00
|
|
|
const AUDIO_FIXTURE = {
|
|
|
|
id: 290,
|
|
|
|
url: null, // Nulled out to avoid actually setting the src - avoids an HTTP request
|
|
|
|
original_filename: "song.mp3",
|
|
|
|
filesize: 172214,
|
|
|
|
width: 1024,
|
|
|
|
height: 768,
|
|
|
|
thumbnail_width: 666,
|
|
|
|
thumbnail_height: 500,
|
|
|
|
extension: "mp3",
|
|
|
|
short_url: "upload://mnCnqY5tunCFw2qMgtPnu1mu1C9.mp3",
|
|
|
|
short_path: "/uploads/short-url/mnCnqY5tunCFw2qMgtPnu1mu1C9.mp3",
|
|
|
|
retain_hours: null,
|
|
|
|
human_filesize: "168 KB",
|
|
|
|
};
|
|
|
|
|
2022-11-02 09:41:30 -04:00
|
|
|
const TXT_FIXTURE = {
|
|
|
|
id: 290,
|
|
|
|
url: "https://example.com/file.txt",
|
|
|
|
original_filename: "file.txt",
|
|
|
|
filesize: 172214,
|
|
|
|
extension: "txt",
|
|
|
|
short_url: "upload://mnCnqY5tunCFw2qMgtPnu1mu1C9.jpeg",
|
|
|
|
short_path: "/uploads/short-url/mnCnqY5tunCFw2qMgtPnu1mu1C9.jpeg",
|
|
|
|
retain_hours: null,
|
|
|
|
human_filesize: "168 KB",
|
|
|
|
};
|
|
|
|
|
|
|
|
module("Discourse Chat | Component | chat-upload", function (hooks) {
|
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
test("with an image", async function (assert) {
|
|
|
|
this.set("upload", IMAGE_FIXTURE);
|
|
|
|
|
|
|
|
await render(hbs`<ChatUpload @upload={{this.upload}} />`);
|
|
|
|
|
|
|
|
assert.true(exists("img.chat-img-upload"), "displays as an image");
|
|
|
|
const image = query("img.chat-img-upload");
|
|
|
|
assert.strictEqual(image.loading, "lazy", "is lazy loading");
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
image.style.backgroundColor,
|
|
|
|
"rgb(120, 131, 112)",
|
|
|
|
"sets background to dominant color"
|
|
|
|
);
|
|
|
|
|
|
|
|
image.dispatchEvent(new Event("load")); // Fake that the image has loaded
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
image.style.backgroundColor,
|
|
|
|
"",
|
|
|
|
"removes the background color once the image has loaded"
|
|
|
|
);
|
2022-11-02 09:41:30 -04:00
|
|
|
});
|
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
test("with a video", async function (assert) {
|
|
|
|
this.set("upload", VIDEO_FIXTURE);
|
|
|
|
|
|
|
|
await render(hbs`<ChatUpload @upload={{this.upload}} />`);
|
|
|
|
|
|
|
|
assert.true(exists("video.chat-video-upload"), "displays as an video");
|
|
|
|
const video = query("video.chat-video-upload");
|
|
|
|
assert.true(video.hasAttribute("controls"), "has video controls");
|
|
|
|
assert.strictEqual(
|
|
|
|
video.getAttribute("preload"),
|
|
|
|
"metadata",
|
|
|
|
"video has correct preload settings"
|
|
|
|
);
|
2022-11-02 09:41:30 -04:00
|
|
|
});
|
|
|
|
|
2023-02-06 01:00:03 -05:00
|
|
|
test("with a audio", async function (assert) {
|
|
|
|
this.set("upload", AUDIO_FIXTURE);
|
|
|
|
|
|
|
|
await render(hbs`<ChatUpload @upload={{this.upload}} />`);
|
|
|
|
|
|
|
|
assert.true(exists("audio.chat-audio-upload"), "displays as an audio");
|
|
|
|
const audio = query("audio.chat-audio-upload");
|
|
|
|
assert.true(audio.hasAttribute("controls"), "has audio controls");
|
|
|
|
assert.strictEqual(
|
|
|
|
audio.getAttribute("preload"),
|
|
|
|
"metadata",
|
|
|
|
"audio has correct preload settings"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
test("non image upload", async function (assert) {
|
|
|
|
this.set("upload", TXT_FIXTURE);
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
await render(hbs`<ChatUpload @upload={{this.upload}} />`);
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
assert.true(exists("a.chat-other-upload"), "displays as a link");
|
|
|
|
const link = query("a.chat-other-upload");
|
|
|
|
assert.strictEqual(link.href, TXT_FIXTURE.url, "has the correct URL");
|
2022-11-02 09:41:30 -04:00
|
|
|
});
|
|
|
|
});
|