2017-11-25 22:22:36 +01:00
|
|
|
set_global('$', global.make_zjquery());
|
|
|
|
set_global('document', {
|
|
|
|
location: { },
|
|
|
|
});
|
|
|
|
set_global('i18n', global.stub_i18n);
|
|
|
|
set_global('page_params', { });
|
|
|
|
set_global('csrf_token', { });
|
|
|
|
|
|
|
|
// Setting these up so that we can test that links to uploads within messages are
|
|
|
|
// automatically converted to server relative links.
|
|
|
|
global.document.location.protocol = 'https:';
|
|
|
|
global.document.location.host = 'foo.com';
|
|
|
|
|
|
|
|
zrequire('compose_ui');
|
|
|
|
zrequire('compose_state');
|
|
|
|
zrequire('compose');
|
|
|
|
zrequire('upload');
|
|
|
|
|
|
|
|
(function test_upload_started() {
|
|
|
|
$("#compose-send-button").prop('disabled', false);
|
2017-11-26 19:58:36 +01:00
|
|
|
$("#compose-send-status").removeClass("alert-info").hide();
|
2017-11-26 20:01:37 +01:00
|
|
|
$(".compose-send-status-close").one = function (ev_name, handler) {
|
2017-11-25 22:22:36 +01:00
|
|
|
assert.equal(ev_name, 'click');
|
|
|
|
assert(handler);
|
|
|
|
};
|
2017-11-26 20:03:46 +01:00
|
|
|
$("#compose-error-msg").html('');
|
2017-11-25 22:22:36 +01:00
|
|
|
var test_html = '<div class="progress progress-striped active">' +
|
2017-11-26 20:05:18 +01:00
|
|
|
'<div class="bar" id="compose-upload-bar" style="width: 00%;">' +
|
2017-11-25 22:22:36 +01:00
|
|
|
'</div></div>';
|
|
|
|
$("<p>").after = function (html) {
|
|
|
|
assert.equal(html, test_html);
|
|
|
|
return 'fake-html';
|
|
|
|
};
|
|
|
|
|
|
|
|
upload.uploadStarted();
|
|
|
|
|
|
|
|
assert.equal($("#compose-send-button").attr("disabled"), '');
|
2017-11-26 19:58:36 +01:00
|
|
|
assert($("#compose-send-status").hasClass("alert-info"));
|
|
|
|
assert($("#compose-send-status").visible());
|
2017-11-25 22:22:36 +01:00
|
|
|
assert.equal($("<p>").text(), 'translated: Uploading…');
|
2017-11-26 20:03:46 +01:00
|
|
|
assert.equal($("#compose-error-msg").html(), 'fake-html');
|
2017-11-25 22:22:36 +01:00
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_progress_updated() {
|
|
|
|
var width_update_checked = false;
|
2017-11-26 20:05:18 +01:00
|
|
|
$("#compose-upload-bar").width = function (width_percent) {
|
2017-11-25 22:22:36 +01:00
|
|
|
assert.equal(width_percent, '39%');
|
|
|
|
width_update_checked = true;
|
|
|
|
};
|
|
|
|
upload.progressUpdated(1, '', 39);
|
|
|
|
assert(width_update_checked);
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_upload_error() {
|
|
|
|
function setup_test() {
|
2017-11-26 19:58:36 +01:00
|
|
|
$("#compose-send-status").removeClass("alert-error");
|
|
|
|
$("#compose-send-status").addClass("alert-info");
|
2017-11-25 22:22:36 +01:00
|
|
|
$("#compose-send-button").attr("disabled", 'disabled');
|
2017-11-26 20:03:46 +01:00
|
|
|
$("#compose-error-msg").text('');
|
2017-11-25 22:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function assert_side_effects(msg, check_html=false) {
|
2017-11-26 19:58:36 +01:00
|
|
|
assert($("#compose-send-status").hasClass("alert-error"));
|
|
|
|
assert(!$("#compose-send-status").hasClass("alert-info"));
|
2017-11-25 22:22:36 +01:00
|
|
|
assert.equal($("#compose-send-button").prop("disabled"), false);
|
|
|
|
if (check_html) {
|
2017-11-26 20:03:46 +01:00
|
|
|
assert.equal($("#compose-error-msg").html(), msg);
|
2017-11-25 22:22:36 +01:00
|
|
|
} else {
|
2017-11-26 20:03:46 +01:00
|
|
|
assert.equal($("#compose-error-msg").text(), msg);
|
2017-11-25 22:22:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function test(err, file, msg) {
|
|
|
|
setup_test();
|
|
|
|
upload.uploadError(err, file);
|
|
|
|
// The text function and html function in zjquery is not in sync
|
|
|
|
// with each other. QuotaExceeded changes html while all other errors
|
|
|
|
// changes body.
|
|
|
|
if (err === 'QuotaExceeded') {
|
|
|
|
assert_side_effects(msg, true);
|
|
|
|
} else {
|
|
|
|
assert_side_effects(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg_prefix = 'translated: ';
|
|
|
|
var msg_1 = 'File upload is not yet available for your browser.';
|
|
|
|
var msg_2 = 'Unable to upload that many files at once.';
|
|
|
|
var msg_3 = '"foobar.txt" was too large; the maximum file size is 25MiB.';
|
|
|
|
var msg_4 = 'Sorry, the file was too large.';
|
|
|
|
var msg_5 = 'Upload would exceed your maximum quota. You can delete old attachments to ' +
|
|
|
|
'free up space. <a href="#settings/uploaded-files">translated: Click here</a>';
|
|
|
|
|
|
|
|
var msg_6 = 'An unknown error occurred.';
|
|
|
|
|
|
|
|
test('BrowserNotSupported', {}, msg_prefix + msg_1);
|
|
|
|
test('TooManyFiles', {}, msg_prefix + msg_2);
|
|
|
|
test('FileTooLarge', {name: 'foobar.txt'}, msg_prefix + msg_3);
|
|
|
|
test('REQUEST ENTITY TOO LARGE', {}, msg_prefix + msg_4);
|
|
|
|
test('QuotaExceeded', {}, msg_prefix + msg_5);
|
|
|
|
test('Do-not-match-any-case', {}, msg_prefix + msg_6);
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_upload_finish() {
|
|
|
|
function test(i, response, textbox_val) {
|
|
|
|
var compose_ui_autosize_textarea_checked = false;
|
|
|
|
var compose_actions_start_checked = false;
|
|
|
|
|
|
|
|
function setup() {
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").val('');
|
2017-11-25 22:22:36 +01:00
|
|
|
compose_ui.autosize_textarea = function () {
|
|
|
|
compose_ui_autosize_textarea_checked = true;
|
|
|
|
};
|
|
|
|
compose_state.set_message_type();
|
|
|
|
global.compose_actions = {
|
|
|
|
start: function (msg_type) {
|
|
|
|
assert.equal(msg_type, 'stream');
|
|
|
|
compose_actions_start_checked = true;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
$("#compose-send-button").attr('disabled', 'disabled');
|
2017-11-26 19:58:36 +01:00
|
|
|
$("#compose-send-status").addClass("alert-info");
|
|
|
|
$("#compose-send-status").show();
|
2017-11-25 22:22:36 +01:00
|
|
|
$('#file_input').clone = function (param) {
|
|
|
|
assert(param);
|
|
|
|
return $('#file_input');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function assert_side_effects() {
|
2017-11-26 20:37:44 +01:00
|
|
|
assert.equal($("#compose-textarea").val(), textbox_val);
|
2017-11-25 22:22:36 +01:00
|
|
|
if (response.uri) {
|
|
|
|
assert(compose_actions_start_checked);
|
|
|
|
assert(compose_ui_autosize_textarea_checked);
|
|
|
|
assert.equal($("#compose-send-button").prop('disabled'), false);
|
2017-11-26 19:58:36 +01:00
|
|
|
assert(!$('#compose-send-status').hasClass('alert-info'));
|
|
|
|
assert(!$('#compose-send-status').visible());
|
2017-11-25 22:22:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setup();
|
|
|
|
upload.uploadFinished(i, {}, response);
|
|
|
|
assert_side_effects();
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg_1 = '[pasted image](https://foo.com/uploads/122456) ';
|
|
|
|
var msg_2 = '[foobar.jpeg](https://foo.com/user_uploads/foobar.jpeg) ';
|
|
|
|
|
|
|
|
test(-1, {}, '');
|
|
|
|
test(-1, {uri: 'https://foo.com/uploads/122456'}, msg_1);
|
|
|
|
test(1, {uri: '/user_uploads/foobar.jpeg'}, msg_2);
|
|
|
|
}());
|