2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-03-25 23:20:18 +01:00
|
|
|
const {mock_cjs, mock_esm, set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-02-21 15:38:51 +01:00
|
|
|
const $ = require("../zjsunit/zjquery");
|
2021-03-25 22:35:45 +01:00
|
|
|
const {page_params} = require("../zjsunit/zpage_params");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("document", {
|
2020-07-15 00:34:28 +02:00
|
|
|
location: {},
|
2017-11-25 22:22:36 +01:00
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("navigator", {
|
|
|
|
userAgent: "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
|
2018-02-13 23:29:17 +01:00
|
|
|
});
|
2017-11-25 22:22:36 +01:00
|
|
|
|
|
|
|
// Setting these up so that we can test that links to uploads within messages are
|
|
|
|
// automatically converted to server relative links.
|
2020-12-01 00:57:57 +01:00
|
|
|
document.location.protocol = "https:";
|
|
|
|
document.location.host = "foo.com";
|
2017-11-25 22:22:36 +01:00
|
|
|
|
2021-03-11 05:43:45 +01:00
|
|
|
mock_cjs("jquery", $);
|
|
|
|
|
2021-03-10 06:31:13 +01:00
|
|
|
let uppy_stub;
|
|
|
|
function Uppy(options) {
|
|
|
|
return uppy_stub.call(this, options);
|
|
|
|
}
|
|
|
|
Uppy.Plugin = {
|
2019-11-21 05:24:55 +01:00
|
|
|
prototype: {
|
|
|
|
constructor: null,
|
|
|
|
},
|
|
|
|
};
|
2021-03-10 06:31:13 +01:00
|
|
|
mock_cjs("@uppy/core", Uppy);
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2021-03-25 23:20:18 +01:00
|
|
|
mock_esm("../../static/js/csrf", {csrf_token: "csrf_token"});
|
|
|
|
|
2021-03-10 06:31:13 +01:00
|
|
|
const compose_ui = zrequire("compose_ui");
|
|
|
|
const compose_actions = zrequire("compose_actions");
|
zjsunit: Remove rewiremock dependency.
We now just use a module._load hook to inject
stubs into our code.
For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.
We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.
We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.
The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js. This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code. It's often the case that you can simply
use override(), as well.
In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
2021-03-06 12:47:54 +01:00
|
|
|
const upload = zrequire("upload");
|
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
function test(label, f) {
|
|
|
|
run_test(label, (override) => {
|
|
|
|
page_params.max_file_upload_size_mib = 25;
|
|
|
|
f(override);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
test("feature_check", (override) => {
|
2021-03-05 17:27:53 +01:00
|
|
|
const upload_button = $.create("upload-button-stub");
|
|
|
|
upload_button.addClass("notdisplayed");
|
|
|
|
upload.feature_check(upload_button);
|
|
|
|
assert(upload_button.hasClass("notdisplayed"));
|
|
|
|
|
2021-03-05 17:48:22 +01:00
|
|
|
override(window, "XMLHttpRequest", () => ({upload: true}));
|
2021-03-05 17:27:53 +01:00
|
|
|
upload.feature_check(upload_button);
|
|
|
|
assert(!upload_button.hasClass("notdisplayed"));
|
|
|
|
});
|
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
test("make_upload_absolute", () => {
|
2019-11-21 05:24:55 +01:00
|
|
|
let uri = "/user_uploads/5/d4/6lSlfIPIg9nDI2Upj0Mq_EbE/kerala.png";
|
|
|
|
const expected_uri = "https://foo.com/user_uploads/5/d4/6lSlfIPIg9nDI2Upj0Mq_EbE/kerala.png";
|
|
|
|
assert.equal(upload.make_upload_absolute(uri), expected_uri);
|
2018-02-13 22:39:51 +01:00
|
|
|
|
2019-11-21 05:24:55 +01:00
|
|
|
uri = "https://foo.com/user_uploads/5/d4/6lSlfIPIg9nDI2Upj0Mq_EbE/alappuzha.png";
|
|
|
|
assert.equal(upload.make_upload_absolute(uri), uri);
|
|
|
|
});
|
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
test("get_item", () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(upload.get_item("textarea", {mode: "compose"}), $("#compose-textarea"));
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
upload.get_item("send_status_message", {mode: "compose"}),
|
|
|
|
$("#compose-error-msg"),
|
|
|
|
);
|
2019-11-21 05:24:55 +01:00
|
|
|
assert.equal(upload.get_item("file_input_identifier", {mode: "compose"}), "#file_input");
|
|
|
|
assert.equal(upload.get_item("source", {mode: "compose"}), "compose-file-input");
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(upload.get_item("drag_drop_container", {mode: "compose"}), $("#compose"));
|
2020-09-04 21:10:48 +02:00
|
|
|
assert.equal(
|
|
|
|
upload.get_item("markdown_preview_hide_button", {mode: "compose"}),
|
|
|
|
$("#undo_markdown_preview"),
|
|
|
|
);
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2021-02-03 23:23:32 +01:00
|
|
|
assert.equal(
|
|
|
|
upload.get_item("textarea", {mode: "edit", row: 1}),
|
|
|
|
$(`#message_edit_content_${CSS.escape(1)}`),
|
|
|
|
);
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`#message_edit_content_${CSS.escape(2)}`).closest = () => {
|
2021-04-21 16:20:16 +02:00
|
|
|
$(".message_edit_form").set_find_results(".message_edit_save", $(".message_edit_save"));
|
|
|
|
return $(".message_edit_form");
|
2017-11-25 22:22:36 +01:00
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(upload.get_item("send_button", {mode: "edit", row: 2}), $(".message_edit_save"));
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
upload.get_item("send_status_identifier", {mode: "edit", row: 11}),
|
2021-02-03 23:23:32 +01:00
|
|
|
`#message-edit-send-status-${CSS.escape(11)}`,
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
upload.get_item("send_status", {mode: "edit", row: 75}),
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`#message-edit-send-status-${CSS.escape(75)}`),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`#message-edit-send-status-${CSS.escape(2)}`).set_find_results(
|
2020-07-15 00:34:28 +02:00
|
|
|
".send-status-close",
|
|
|
|
$(".send-status-close"),
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
upload.get_item("send_status_close_button", {mode: "edit", row: 2}),
|
|
|
|
$(".send-status-close"),
|
|
|
|
);
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`#message-edit-send-status-${CSS.escape(22)}`).set_find_results(
|
|
|
|
".error-msg",
|
|
|
|
$(".error-msg"),
|
|
|
|
);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(upload.get_item("send_status_message", {mode: "edit", row: 22}), $(".error-msg"));
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
upload.get_item("file_input_identifier", {mode: "edit", row: 123}),
|
2021-02-03 23:23:32 +01:00
|
|
|
`#message_edit_file_input_${CSS.escape(123)}`,
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2019-11-21 05:24:55 +01:00
|
|
|
assert.equal(upload.get_item("source", {mode: "edit", row: 123}), "message-edit-file-input");
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
upload.get_item("drag_drop_container", {mode: "edit", row: 1}),
|
2021-04-21 16:20:16 +02:00
|
|
|
$(".message_edit_form"),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2020-09-04 21:10:48 +02:00
|
|
|
assert.equal(
|
|
|
|
upload.get_item("markdown_preview_hide_button", {mode: "edit", row: 65}),
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`#undo_markdown_preview_${CSS.escape(65)}`),
|
2020-09-04 21:10:48 +02:00
|
|
|
);
|
2019-11-21 05:24:55 +01:00
|
|
|
|
|
|
|
assert.throws(
|
|
|
|
() => {
|
|
|
|
upload.get_item("textarea");
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Error",
|
|
|
|
message: "Missing config",
|
2020-07-02 02:16:03 +02:00
|
|
|
},
|
2019-11-21 05:24:55 +01:00
|
|
|
);
|
|
|
|
assert.throws(
|
|
|
|
() => {
|
|
|
|
upload.get_item("textarea", {mode: "edit"});
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Error",
|
|
|
|
message: "Missing row in config",
|
2020-07-02 02:16:03 +02:00
|
|
|
},
|
2019-11-21 05:24:55 +01:00
|
|
|
);
|
|
|
|
assert.throws(
|
|
|
|
() => {
|
|
|
|
upload.get_item("textarea", {mode: "blah"});
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Error",
|
|
|
|
message: "Invalid upload mode!",
|
2020-07-02 02:16:03 +02:00
|
|
|
},
|
2019-11-21 05:24:55 +01:00
|
|
|
);
|
|
|
|
assert.throws(
|
|
|
|
() => {
|
|
|
|
upload.get_item("invalid", {mode: "compose"});
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Error",
|
|
|
|
message: 'Invalid key name for mode "compose"',
|
2020-07-02 02:16:03 +02:00
|
|
|
},
|
2019-11-21 05:24:55 +01:00
|
|
|
);
|
|
|
|
assert.throws(
|
|
|
|
() => {
|
|
|
|
upload.get_item("invalid", {mode: "edit", row: 20});
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Error",
|
|
|
|
message: 'Invalid key name for mode "edit"',
|
2020-07-02 02:16:03 +02:00
|
|
|
},
|
2019-11-21 05:24:55 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
test("hide_upload_status", () => {
|
2020-07-22 02:59:06 +02:00
|
|
|
$("#compose-send-button").prop("disabled", true);
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose-send-status").addClass("alert-info").show();
|
2019-11-21 05:24:55 +01:00
|
|
|
|
|
|
|
upload.hide_upload_status({mode: "compose"});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose-send-button").prop("disabled"), false);
|
|
|
|
assert.equal($("#compose-send-button").hasClass("alert-info"), false);
|
|
|
|
assert.equal($("#compose-send-button").visible(), false);
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
test("show_error_message", () => {
|
2020-07-22 02:59:06 +02:00
|
|
|
$("#compose-send-button").prop("disabled", true);
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose-send-status").addClass("alert-info").removeClass("alert-error").hide();
|
|
|
|
$("#compose-error-msg").text("");
|
|
|
|
$("#compose-error-msg").hide();
|
2019-11-21 05:24:55 +01:00
|
|
|
|
|
|
|
upload.show_error_message({mode: "compose"}, "Error message");
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose-send-button").prop("disabled"), false);
|
|
|
|
assert($("#compose-send-status").hasClass("alert-error"));
|
|
|
|
assert.equal($("#compose-send-status").hasClass("alert-info"), false);
|
|
|
|
assert($("#compose-send-status").visible());
|
|
|
|
assert.equal($("#compose-error-msg").text(), "Error message");
|
2019-11-21 05:24:55 +01:00
|
|
|
|
|
|
|
upload.show_error_message({mode: "compose"});
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose-error-msg").text(), "translated: An unknown error occurred.");
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
test("upload_files", (override) => {
|
2020-04-14 19:25:01 +02:00
|
|
|
let uppy_cancel_all_called = false;
|
2020-04-16 17:11:06 +02:00
|
|
|
let files = [
|
2019-11-21 05:24:55 +01:00
|
|
|
{
|
|
|
|
name: "budapest.png",
|
|
|
|
type: "image/png",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
let uppy_add_file_called = false;
|
|
|
|
const uppy = {
|
|
|
|
cancelAll: () => {
|
2020-04-14 19:25:01 +02:00
|
|
|
uppy_cancel_all_called = true;
|
2019-11-21 05:24:55 +01:00
|
|
|
},
|
|
|
|
addFile: (params) => {
|
|
|
|
uppy_add_file_called = true;
|
|
|
|
assert.equal(params.source, "compose-file-input");
|
|
|
|
assert.equal(params.name, "budapest.png");
|
|
|
|
assert.equal(params.type, "image/png");
|
|
|
|
assert.equal(params.data, files[0]);
|
|
|
|
},
|
2020-03-08 11:13:57 +01:00
|
|
|
getFiles: () => [...files],
|
2018-08-15 03:57:35 +02:00
|
|
|
};
|
2019-11-21 05:24:55 +01:00
|
|
|
let hide_upload_status_called = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(upload, "hide_upload_status", (config) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
hide_upload_status_called = true;
|
|
|
|
assert(config.mode, "compose");
|
2021-02-28 00:52:20 +01:00
|
|
|
});
|
2020-07-16 23:29:01 +02:00
|
|
|
const config = {mode: "compose"};
|
2020-07-22 02:59:06 +02:00
|
|
|
$("#compose-send-button").prop("disabled", false);
|
2019-11-21 05:24:55 +01:00
|
|
|
upload.upload_files(uppy, config, []);
|
2020-07-22 02:59:06 +02:00
|
|
|
assert(!$("#compose-send-button").prop("disabled"));
|
2017-11-25 22:22:36 +01:00
|
|
|
|
2020-05-08 06:57:19 +02:00
|
|
|
page_params.max_file_upload_size_mib = 0;
|
2019-11-21 05:24:55 +01:00
|
|
|
let show_error_message_called = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(upload, "show_error_message", (config, message) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
show_error_message_called = true;
|
|
|
|
assert.equal(config.mode, "compose");
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
message,
|
|
|
|
"translated: File and image uploads have been disabled for this organization.",
|
|
|
|
);
|
2021-02-28 00:52:20 +01:00
|
|
|
});
|
2019-11-21 05:24:55 +01:00
|
|
|
upload.upload_files(uppy, config, files);
|
|
|
|
assert(show_error_message_called);
|
2017-11-25 22:22:36 +01:00
|
|
|
|
2020-05-08 06:57:19 +02:00
|
|
|
page_params.max_file_upload_size_mib = 25;
|
2019-11-21 05:24:55 +01:00
|
|
|
let on_click_close_button_callback;
|
|
|
|
$(".compose-send-status-close").one = (event, callback) => {
|
|
|
|
assert.equal(event, "click");
|
|
|
|
on_click_close_button_callback = callback;
|
|
|
|
};
|
|
|
|
let compose_ui_insert_syntax_and_focus_called = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose_ui, "insert_syntax_and_focus", (syntax, textarea) => {
|
2020-04-17 17:37:06 +02:00
|
|
|
assert.equal(syntax, "[translated: Uploading budapest.png…]()");
|
2019-11-21 05:24:55 +01:00
|
|
|
assert.equal(textarea, $("#compose-textarea"));
|
|
|
|
compose_ui_insert_syntax_and_focus_called = true;
|
2021-02-28 01:26:48 +01:00
|
|
|
});
|
2019-11-21 05:24:55 +01:00
|
|
|
let compose_ui_autosize_textarea_called = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose_ui, "autosize_textarea", () => {
|
2019-11-21 05:24:55 +01:00
|
|
|
compose_ui_autosize_textarea_called = true;
|
2021-02-28 01:26:48 +01:00
|
|
|
});
|
2020-09-04 21:10:48 +02:00
|
|
|
let markdown_preview_hide_button_clicked = false;
|
|
|
|
$("#undo_markdown_preview").on("click", () => {
|
|
|
|
markdown_preview_hide_button_clicked = true;
|
|
|
|
});
|
2020-07-22 02:59:06 +02:00
|
|
|
$("#compose-send-button").prop("disabled", false);
|
2019-11-21 05:24:55 +01:00
|
|
|
$("#compose-send-status").removeClass("alert-info").hide();
|
2020-09-04 21:10:48 +02:00
|
|
|
$("#undo_markdown_preview").show();
|
2019-11-21 05:24:55 +01:00
|
|
|
upload.upload_files(uppy, config, files);
|
2020-07-22 02:59:06 +02:00
|
|
|
assert($("#compose-send-button").prop("disabled"));
|
2017-11-26 19:58:36 +01:00
|
|
|
assert($("#compose-send-status").hasClass("alert-info"));
|
|
|
|
assert($("#compose-send-status").visible());
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("<p>").text(), "translated: Uploading…");
|
2019-11-21 05:24:55 +01:00
|
|
|
assert(compose_ui_insert_syntax_and_focus_called);
|
|
|
|
assert(compose_ui_autosize_textarea_called);
|
2020-09-04 21:10:48 +02:00
|
|
|
assert(markdown_preview_hide_button_clicked);
|
2019-11-21 05:24:55 +01:00
|
|
|
assert(uppy_add_file_called);
|
2017-11-25 22:22:36 +01:00
|
|
|
|
2020-04-16 17:11:06 +02:00
|
|
|
files = [
|
|
|
|
{
|
|
|
|
name: "budapest.png",
|
|
|
|
type: "image/png",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prague.png",
|
|
|
|
type: "image/png",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
let add_file_counter = 0;
|
|
|
|
uppy.addFile = (file) => {
|
|
|
|
assert.equal(file.name, "budapest.png");
|
|
|
|
add_file_counter += 1;
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("some error");
|
2020-04-16 17:11:06 +02:00
|
|
|
};
|
|
|
|
upload.upload_files(uppy, config, files);
|
|
|
|
assert.equal(add_file_counter, 1);
|
|
|
|
|
2020-12-01 00:02:16 +01:00
|
|
|
set_global("setTimeout", (func) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
func();
|
|
|
|
});
|
|
|
|
hide_upload_status_called = false;
|
2020-04-14 19:25:01 +02:00
|
|
|
uppy_cancel_all_called = false;
|
2020-03-08 11:13:57 +01:00
|
|
|
let compose_ui_replace_syntax_called = false;
|
|
|
|
files = [
|
|
|
|
{
|
|
|
|
name: "budapest.png",
|
|
|
|
type: "image/png",
|
|
|
|
},
|
|
|
|
];
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose_ui, "replace_syntax", (old_syntax, new_syntax, textarea) => {
|
2020-03-08 11:13:57 +01:00
|
|
|
compose_ui_replace_syntax_called = true;
|
2020-04-17 17:37:06 +02:00
|
|
|
assert.equal(old_syntax, "[translated: Uploading budapest.png…]()");
|
2020-03-08 11:13:57 +01:00
|
|
|
assert.equal(new_syntax, "");
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(textarea, $("#compose-textarea"));
|
2021-02-28 01:26:48 +01:00
|
|
|
});
|
2019-11-21 05:24:55 +01:00
|
|
|
on_click_close_button_callback();
|
2020-04-14 19:25:01 +02:00
|
|
|
assert(uppy_cancel_all_called);
|
2019-11-21 05:24:55 +01:00
|
|
|
assert(hide_upload_status_called);
|
2020-03-08 11:13:57 +01:00
|
|
|
assert(compose_ui_autosize_textarea_called);
|
|
|
|
assert(compose_ui_replace_syntax_called);
|
|
|
|
hide_upload_status_called = false;
|
|
|
|
compose_ui_replace_syntax_called = false;
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose-textarea").val("user modified text");
|
2020-03-08 11:13:57 +01:00
|
|
|
on_click_close_button_callback();
|
|
|
|
assert(hide_upload_status_called);
|
|
|
|
assert(compose_ui_autosize_textarea_called);
|
|
|
|
assert(compose_ui_replace_syntax_called);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert($("#compose-textarea").val(), "user modified text");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-11-25 22:22:36 +01:00
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
test("uppy_config", () => {
|
2019-11-21 05:24:55 +01:00
|
|
|
let uppy_stub_called = false;
|
|
|
|
let uppy_set_meta_called = false;
|
|
|
|
let uppy_used_xhrupload = false;
|
|
|
|
let uppy_used_progressbar = false;
|
2018-04-05 04:29:21 +02:00
|
|
|
|
2021-03-10 06:31:13 +01:00
|
|
|
uppy_stub = function (config) {
|
2019-11-21 05:24:55 +01:00
|
|
|
uppy_stub_called = true;
|
|
|
|
assert.equal(config.debug, false);
|
|
|
|
assert.equal(config.autoProceed, true);
|
|
|
|
assert.equal(config.restrictions.maxFileSize, 25 * 1024 * 1024);
|
|
|
|
assert.equal(Object.keys(config.locale.strings).length, 2);
|
|
|
|
assert("exceedsSize" in config.locale.strings);
|
|
|
|
|
|
|
|
return {
|
|
|
|
setMeta: (params) => {
|
|
|
|
uppy_set_meta_called = true;
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(params.csrfmiddlewaretoken, "csrf_token");
|
2019-11-21 05:24:55 +01:00
|
|
|
},
|
|
|
|
use: (func, params) => {
|
|
|
|
const func_name = func.name;
|
|
|
|
if (func_name === "XHRUpload") {
|
|
|
|
uppy_used_xhrupload = true;
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(params.endpoint, "/json/user_uploads");
|
2019-11-21 05:24:55 +01:00
|
|
|
assert.equal(params.formData, true);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(params.fieldName, "file");
|
2019-11-21 05:24:55 +01:00
|
|
|
assert.equal(params.limit, 5);
|
|
|
|
assert.equal(Object.keys(params.locale.strings).length, 1);
|
|
|
|
assert("timedOut" in params.locale.strings);
|
|
|
|
} else if (func_name === "ProgressBar") {
|
|
|
|
uppy_used_progressbar = true;
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(params.target, "#compose-send-status");
|
2019-11-21 05:24:55 +01:00
|
|
|
assert.equal(params.hideAfterFinish, false);
|
|
|
|
} else {
|
|
|
|
/* istanbul ignore next */
|
|
|
|
assert.fail(`Missing tests for ${func_name}`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
on: () => {},
|
2018-04-05 04:29:21 +02:00
|
|
|
};
|
2021-03-10 06:31:13 +01:00
|
|
|
};
|
2019-11-21 05:24:55 +01:00
|
|
|
upload.setup_upload({mode: "compose"});
|
2017-11-25 22:22:36 +01:00
|
|
|
|
2019-11-21 05:24:55 +01:00
|
|
|
assert.equal(uppy_stub_called, true);
|
|
|
|
assert.equal(uppy_set_meta_called, true);
|
|
|
|
assert.equal(uppy_used_xhrupload, true);
|
|
|
|
assert.equal(uppy_used_progressbar, true);
|
|
|
|
});
|
2017-11-25 22:22:36 +01:00
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
test("file_input", (override) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
upload.setup_upload({mode: "compose"});
|
|
|
|
|
|
|
|
const change_handler = $("body").get_on_handler("change", "#file_input");
|
|
|
|
const files = ["file1", "file2"];
|
|
|
|
const event = {
|
|
|
|
target: {
|
2020-07-20 22:18:43 +02:00
|
|
|
files,
|
2020-07-16 23:03:45 +02:00
|
|
|
value: "C:\\fakepath\\portland.png",
|
2019-11-21 05:24:55 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
let upload_files_called = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(upload, "upload_files", (uppy, config, files) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
assert.equal(config.mode, "compose");
|
|
|
|
assert.equal(files, files);
|
|
|
|
upload_files_called = true;
|
2021-02-28 00:52:20 +01:00
|
|
|
});
|
2019-11-21 05:24:55 +01:00
|
|
|
change_handler(event);
|
|
|
|
assert(upload_files_called);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-11-25 22:22:36 +01:00
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
test("file_drop", (override) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
upload.setup_upload({mode: "compose"});
|
2017-11-25 22:22:36 +01:00
|
|
|
|
2019-11-21 05:24:55 +01:00
|
|
|
let prevent_default_counter = 0;
|
|
|
|
const drag_event = {
|
|
|
|
preventDefault: () => {
|
|
|
|
prevent_default_counter += 1;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const dragover_handler = $("#compose").get_on_handler("dragover");
|
|
|
|
dragover_handler(drag_event);
|
|
|
|
assert.equal(prevent_default_counter, 1);
|
|
|
|
|
|
|
|
const dragenter_handler = $("#compose").get_on_handler("dragenter");
|
|
|
|
dragenter_handler(drag_event);
|
|
|
|
assert.equal(prevent_default_counter, 2);
|
|
|
|
|
|
|
|
const files = ["file1", "file2"];
|
|
|
|
const drop_event = {
|
|
|
|
preventDefault: () => {
|
|
|
|
prevent_default_counter += 1;
|
|
|
|
},
|
|
|
|
originalEvent: {
|
|
|
|
dataTransfer: {
|
2020-07-20 22:18:43 +02:00
|
|
|
files,
|
2019-11-21 05:24:55 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const drop_handler = $("#compose").get_on_handler("drop");
|
|
|
|
let upload_files_called = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(upload, "upload_files", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
upload_files_called = true;
|
2021-02-28 00:52:20 +01:00
|
|
|
});
|
2019-11-21 05:24:55 +01:00
|
|
|
drop_handler(drop_event);
|
|
|
|
assert.equal(prevent_default_counter, 3);
|
|
|
|
assert.equal(upload_files_called, true);
|
|
|
|
});
|
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
test("copy_paste", (override) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
upload.setup_upload({mode: "compose"});
|
|
|
|
|
|
|
|
const paste_handler = $("#compose").get_on_handler("paste");
|
|
|
|
let get_as_file_called = false;
|
|
|
|
let event = {
|
|
|
|
originalEvent: {
|
|
|
|
clipboardData: {
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
kind: "file",
|
|
|
|
getAsFile: () => {
|
|
|
|
get_as_file_called = true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
kind: "notfile",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let upload_files_called = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(upload, "upload_files", () => {
|
2019-11-21 05:24:55 +01:00
|
|
|
upload_files_called = true;
|
2021-02-28 00:52:20 +01:00
|
|
|
});
|
2019-11-21 05:24:55 +01:00
|
|
|
|
|
|
|
paste_handler(event);
|
|
|
|
assert(get_as_file_called);
|
|
|
|
assert(upload_files_called);
|
|
|
|
|
|
|
|
upload_files_called = false;
|
|
|
|
event = {
|
|
|
|
originalEvent: {},
|
|
|
|
};
|
|
|
|
paste_handler(event);
|
|
|
|
assert.equal(upload_files_called, false);
|
|
|
|
});
|
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
test("uppy_events", (override) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
const callbacks = {};
|
2020-04-14 16:16:49 +02:00
|
|
|
let uppy_cancel_all_called = false;
|
2019-11-21 05:24:55 +01:00
|
|
|
let state = {};
|
2020-04-14 16:16:49 +02:00
|
|
|
let files = [];
|
2018-04-05 04:29:21 +02:00
|
|
|
|
2021-03-10 06:31:13 +01:00
|
|
|
uppy_stub = function () {
|
2019-11-21 05:24:55 +01:00
|
|
|
return {
|
|
|
|
setMeta: () => {},
|
|
|
|
use: () => {},
|
|
|
|
cancelAll: () => {
|
2020-04-14 16:16:49 +02:00
|
|
|
uppy_cancel_all_called = true;
|
2019-11-21 05:24:55 +01:00
|
|
|
},
|
|
|
|
on: (event_name, callback) => {
|
|
|
|
callbacks[event_name] = callback;
|
|
|
|
},
|
2020-07-02 01:41:40 +02:00
|
|
|
getFiles: () => [...files],
|
2020-04-14 16:16:49 +02:00
|
|
|
removeFile: (file_id) => {
|
2020-07-02 01:41:40 +02:00
|
|
|
files = files.filter((file) => file.id !== file_id);
|
2019-11-21 05:24:55 +01:00
|
|
|
},
|
2020-07-02 01:41:40 +02:00
|
|
|
getState: () => ({
|
|
|
|
info: {
|
|
|
|
type: state.type,
|
|
|
|
details: state.details,
|
|
|
|
message: state.message,
|
|
|
|
},
|
|
|
|
}),
|
2019-11-21 05:24:55 +01:00
|
|
|
};
|
2021-03-10 06:31:13 +01:00
|
|
|
};
|
2019-11-21 05:24:55 +01:00
|
|
|
upload.setup_upload({mode: "compose"});
|
2020-03-07 12:44:17 +01:00
|
|
|
assert.equal(Object.keys(callbacks).length, 5);
|
2017-11-25 22:22:36 +01:00
|
|
|
|
2019-11-21 05:24:55 +01:00
|
|
|
const on_upload_success_callback = callbacks["upload-success"];
|
|
|
|
const file = {
|
|
|
|
name: "copenhagen.png",
|
|
|
|
};
|
|
|
|
let response = {
|
|
|
|
body: {
|
|
|
|
uri: "/user_uploads/4/cb/rue1c-MlMUjDAUdkRrEM4BTJ/copenhagen.png",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let compose_actions_start_called = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose_actions, "start", () => {
|
2019-11-21 05:24:55 +01:00
|
|
|
compose_actions_start_called = true;
|
2021-02-28 21:32:22 +01:00
|
|
|
});
|
2019-11-21 05:24:55 +01:00
|
|
|
let compose_ui_replace_syntax_called = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose_ui, "replace_syntax", (old_syntax, new_syntax, textarea) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
compose_ui_replace_syntax_called = true;
|
2020-04-17 17:37:06 +02:00
|
|
|
assert.equal(old_syntax, "[translated: Uploading copenhagen.png…]()");
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
new_syntax,
|
|
|
|
"[copenhagen.png](https://foo.com/user_uploads/4/cb/rue1c-MlMUjDAUdkRrEM4BTJ/copenhagen.png)",
|
|
|
|
);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(textarea, $("#compose-textarea"));
|
2021-02-28 01:26:48 +01:00
|
|
|
});
|
2019-11-21 05:24:55 +01:00
|
|
|
let compose_ui_autosize_textarea_called = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose_ui, "autosize_textarea", () => {
|
2019-11-21 05:24:55 +01:00
|
|
|
compose_ui_autosize_textarea_called = true;
|
2021-02-28 01:26:48 +01:00
|
|
|
});
|
2019-11-21 05:24:55 +01:00
|
|
|
on_upload_success_callback(file, response);
|
|
|
|
assert(compose_actions_start_called);
|
|
|
|
assert(compose_ui_replace_syntax_called);
|
|
|
|
assert(compose_ui_autosize_textarea_called);
|
2017-11-25 22:22:36 +01:00
|
|
|
|
2019-11-21 05:24:55 +01:00
|
|
|
response = {
|
|
|
|
body: {
|
|
|
|
uri: undefined,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
compose_actions_start_called = false;
|
|
|
|
compose_ui_replace_syntax_called = false;
|
|
|
|
compose_ui_autosize_textarea_called = false;
|
|
|
|
on_upload_success_callback(file, response);
|
|
|
|
assert.equal(compose_actions_start_called, false);
|
|
|
|
assert.equal(compose_ui_replace_syntax_called, false);
|
|
|
|
assert.equal(compose_ui_autosize_textarea_called, false);
|
|
|
|
|
|
|
|
const on_complete_callback = callbacks.complete;
|
2020-12-01 00:02:16 +01:00
|
|
|
set_global("setTimeout", (func) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
func();
|
|
|
|
});
|
|
|
|
let hide_upload_status_called = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(upload, "hide_upload_status", () => {
|
2019-11-21 05:24:55 +01:00
|
|
|
hide_upload_status_called = true;
|
2021-02-28 00:52:20 +01:00
|
|
|
});
|
2020-04-16 18:22:43 +02:00
|
|
|
$("#compose-send-status").removeClass("alert-error");
|
2020-04-14 16:16:49 +02:00
|
|
|
files = [
|
|
|
|
{
|
|
|
|
id: "uppy-zulip/jpeg-1e-image/jpeg-163515-1578367331279",
|
|
|
|
progress: {
|
|
|
|
uploadComplete: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "uppy-github/avatar/png-2v-1e-image/png-329746-1576507125831",
|
|
|
|
progress: {
|
|
|
|
uploadComplete: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
2019-11-21 05:24:55 +01:00
|
|
|
on_complete_callback();
|
|
|
|
assert(hide_upload_status_called);
|
2020-04-14 16:16:49 +02:00
|
|
|
assert.equal(files.length, 0);
|
|
|
|
|
2020-04-16 18:22:43 +02:00
|
|
|
hide_upload_status_called = false;
|
|
|
|
$("#compose-send-status").addClass("alert-error");
|
|
|
|
on_complete_callback();
|
|
|
|
assert(!hide_upload_status_called);
|
|
|
|
|
|
|
|
$("#compose-send-status").removeClass("alert-error");
|
2020-04-14 16:16:49 +02:00
|
|
|
hide_upload_status_called = false;
|
|
|
|
files = [
|
|
|
|
{
|
|
|
|
id: "uppy-zulip/jpeg-1e-image/jpeg-163515-1578367331279",
|
|
|
|
progress: {
|
|
|
|
uploadComplete: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "uppy-github/avatar/png-2v-1e-image/png-329746-1576507125831",
|
|
|
|
progress: {
|
|
|
|
uploadComplete: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
on_complete_callback();
|
|
|
|
assert(!hide_upload_status_called);
|
|
|
|
assert.equal(files.length, 1);
|
2019-11-21 05:24:55 +01:00
|
|
|
|
|
|
|
state = {
|
|
|
|
type: "error",
|
|
|
|
details: "Some Error",
|
|
|
|
message: "Some error message",
|
|
|
|
};
|
|
|
|
const on_info_visible_callback = callbacks["info-visible"];
|
|
|
|
let show_error_message_called = false;
|
2020-04-14 16:16:49 +02:00
|
|
|
uppy_cancel_all_called = false;
|
2020-03-07 12:44:17 +01:00
|
|
|
compose_ui_replace_syntax_called = false;
|
|
|
|
const on_restriction_failed_callback = callbacks["restriction-failed"];
|
2021-03-07 17:01:11 +01:00
|
|
|
override(upload, "show_error_message", (config, message) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
show_error_message_called = true;
|
|
|
|
assert.equal(config.mode, "compose");
|
|
|
|
assert.equal(message, "Some error message");
|
2021-02-28 00:52:20 +01:00
|
|
|
});
|
2019-11-21 05:24:55 +01:00
|
|
|
on_info_visible_callback();
|
2020-04-14 16:16:49 +02:00
|
|
|
assert(uppy_cancel_all_called);
|
2019-11-21 05:24:55 +01:00
|
|
|
assert(show_error_message_called);
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose_ui, "replace_syntax", (old_syntax, new_syntax, textarea) => {
|
2020-03-07 12:44:17 +01:00
|
|
|
compose_ui_replace_syntax_called = true;
|
2020-04-17 17:37:06 +02:00
|
|
|
assert.equal(old_syntax, "[translated: Uploading copenhagen.png…]()");
|
2020-03-07 12:44:17 +01:00
|
|
|
assert.equal(new_syntax, "");
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(textarea, $("#compose-textarea"));
|
2021-02-28 01:26:48 +01:00
|
|
|
});
|
2020-03-07 12:44:17 +01:00
|
|
|
on_restriction_failed_callback(file, null, null);
|
|
|
|
assert(compose_ui_replace_syntax_called);
|
|
|
|
compose_ui_replace_syntax_called = false;
|
2021-02-03 23:16:49 +01:00
|
|
|
$("#compose-textarea").val("user modified text");
|
2020-03-07 12:44:17 +01:00
|
|
|
on_restriction_failed_callback(file, null, null);
|
|
|
|
assert(compose_ui_replace_syntax_called);
|
2021-02-03 23:16:49 +01:00
|
|
|
assert.equal($("#compose-textarea").val(), "user modified text");
|
2019-11-21 05:24:55 +01:00
|
|
|
|
|
|
|
state = {
|
|
|
|
type: "error",
|
|
|
|
message: "No Internet connection",
|
|
|
|
};
|
2020-04-14 16:16:49 +02:00
|
|
|
uppy_cancel_all_called = false;
|
2019-11-21 05:24:55 +01:00
|
|
|
on_info_visible_callback();
|
|
|
|
|
|
|
|
state = {
|
|
|
|
type: "error",
|
|
|
|
details: "Upload Error",
|
|
|
|
};
|
2020-04-14 16:16:49 +02:00
|
|
|
uppy_cancel_all_called = false;
|
2019-11-21 05:24:55 +01:00
|
|
|
on_info_visible_callback();
|
|
|
|
|
|
|
|
const on_upload_error_callback = callbacks["upload-error"];
|
|
|
|
show_error_message_called = false;
|
2020-03-07 12:54:07 +01:00
|
|
|
compose_ui_replace_syntax_called = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(upload, "show_error_message", (config, message) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
show_error_message_called = true;
|
|
|
|
assert.equal(config.mode, "compose");
|
|
|
|
assert.equal(message, "Response message");
|
2021-02-28 00:52:20 +01:00
|
|
|
});
|
2019-11-21 05:24:55 +01:00
|
|
|
response = {
|
|
|
|
body: {
|
|
|
|
msg: "Response message",
|
|
|
|
},
|
|
|
|
};
|
2020-04-14 16:16:49 +02:00
|
|
|
uppy_cancel_all_called = false;
|
2020-03-07 12:54:07 +01:00
|
|
|
on_upload_error_callback(file, null, response);
|
2020-04-14 16:16:49 +02:00
|
|
|
assert(uppy_cancel_all_called);
|
2019-11-21 05:24:55 +01:00
|
|
|
assert(show_error_message_called);
|
2020-03-07 12:54:07 +01:00
|
|
|
assert(compose_ui_replace_syntax_called);
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2020-03-07 12:54:07 +01:00
|
|
|
compose_ui_replace_syntax_called = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(upload, "show_error_message", (config, message) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
show_error_message_called = true;
|
|
|
|
assert.equal(config.mode, "compose");
|
2021-03-24 21:44:43 +01:00
|
|
|
assert.equal(message, undefined);
|
2021-02-28 00:52:20 +01:00
|
|
|
});
|
2020-04-14 16:16:49 +02:00
|
|
|
uppy_cancel_all_called = false;
|
2020-03-07 12:54:07 +01:00
|
|
|
on_upload_error_callback(file, null, null);
|
2020-04-14 16:16:49 +02:00
|
|
|
assert(uppy_cancel_all_called);
|
2019-11-21 05:24:55 +01:00
|
|
|
assert(show_error_message_called);
|
2020-03-07 12:54:07 +01:00
|
|
|
assert(compose_ui_replace_syntax_called);
|
|
|
|
show_error_message_called = false;
|
2021-02-03 23:16:49 +01:00
|
|
|
$("#compose-textarea").val("user modified text");
|
2020-03-07 12:54:07 +01:00
|
|
|
uppy_cancel_all_called = false;
|
|
|
|
on_upload_error_callback(file, null);
|
|
|
|
assert(uppy_cancel_all_called);
|
|
|
|
assert(show_error_message_called);
|
|
|
|
assert(compose_ui_replace_syntax_called);
|
2021-02-03 23:16:49 +01:00
|
|
|
assert.equal($("#compose-textarea").val(), "user modified text");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|