Files
zulip/frontend_tests/node_tests/transmit.js
Steve Howell 9ab07d1038 util.js: Remove util from window.
We now treat util like a leaf module and
use "require" to import it everywhere it's used.

An earlier version of this commit moved
util into our "shared" library, but we
decided to wait on that.  Once we're ready
to do that, we should only need to do a
simple search/replace on various
require/zrequire statements plus a small
tweak to one of the custom linter checks.

It turns out we don't really need util.js
for our most immediate code-sharing goal,
which is to reuse our markdown code on
mobile.  There's a little bit of cleanup
still remaining to break the dependency,
but it's minor.

The util module still calls the global
blueslip module in one place, but that
code is about to be removed in the next
few commits.

I am pretty confident that once we start
sharing things like the typeahead code
more aggressively, we'll start having
dependencies on util.  The module is barely
more than 300 lines long, so we'll probably
just move the whole thing into shared
rather than break it apart.  Also, we
can continue to nibble away at the
cruftier parts of the module.
2020-02-15 12:20:20 -08:00

191 lines
4.4 KiB
JavaScript

const noop = function () {};
set_global('$', global.make_zjquery());
set_global('page_params', {});
set_global('channel', {});
set_global('navigator', {});
set_global('reload', {});
set_global('reload_state', {});
set_global('sent_messages', {
start_tracking_message: noop,
report_server_ack: noop,
});
set_global('blueslip', global.make_zblueslip());
zrequire('people');
zrequire('transmit');
run_test('transmit_message_ajax', () => {
let success_func_called;
const success = function () {
success_func_called = true;
};
const request = {foo: 'bar'};
channel.post = function (opts) {
assert.equal(opts.url, '/json/messages');
assert.equal(opts.data.foo, 'bar');
opts.success();
};
transmit.send_message(request, success);
assert(success_func_called);
channel.xhr_error_message = function (msg) {
assert.equal(msg, 'Error sending message');
return msg;
};
channel.post = function (opts) {
assert.equal(opts.url, '/json/messages');
assert.equal(opts.data.foo, 'bar');
const xhr = 'whatever';
opts.error(xhr, 'timeout');
};
let error_func_called;
const error = function (response) {
assert.equal(response, 'Error sending message');
error_func_called = true;
};
transmit.send_message(request, success, error);
assert(error_func_called);
});
run_test('transmit_message_ajax_reload_pending', () => {
const success = function () { throw 'unexpected success'; };
reload_state.is_pending = function () {
return true;
};
let reload_initiated;
reload.initiate = function (opts) {
reload_initiated = true;
assert.deepEqual(opts, {
immediate: true,
save_pointer: true,
save_narrow: true,
save_compose: true,
send_after_reload: true,
});
};
const request = {foo: 'bar'};
let error_func_called;
const error = function (response) {
assert.equal(response, 'Error sending message');
error_func_called = true;
};
error_func_called = false;
channel.post = function (opts) {
assert.equal(opts.url, '/json/messages');
assert.equal(opts.data.foo, 'bar');
const xhr = 'whatever';
opts.error(xhr, 'bad request');
};
transmit.send_message(request, success, error);
assert(!error_func_called);
assert(reload_initiated);
});
run_test('reply_message_stream', () => {
const stream_message = {
type: 'stream',
stream: 'social',
topic: 'lunch',
sender_full_name: 'Alice',
sender_id: 123,
};
const content = 'hello';
let send_message_args;
transmit.send_message = (args) => {
send_message_args = args;
};
page_params.user_id = 44;
page_params.queue_id = 66;
sent_messages.get_new_local_id = () => "99";
transmit.reply_message({
message: stream_message,
content: content,
});
assert.deepEqual(send_message_args, {
sender_id: 44,
queue_id: 66,
local_id: '99',
type: 'stream',
to: 'social',
content: '@**Alice** hello',
topic: 'lunch',
});
});
run_test('reply_message_private', () => {
const fred = {
user_id: 3,
email: 'fred@example.com',
full_name: 'Fred Frost',
};
people.add(fred);
people.is_my_user_id = () => false;
const pm_message = {
type: 'private',
display_recipient: [
{id: fred.user_id},
],
};
const content = 'hello';
let send_message_args;
transmit.send_message = (args) => {
send_message_args = args;
};
page_params.user_id = 155;
page_params.queue_id = 177;
sent_messages.get_new_local_id = () => "199";
transmit.reply_message({
message: pm_message,
content: content,
});
assert.deepEqual(send_message_args, {
sender_id: 155,
queue_id: 177,
local_id: '199',
type: 'private',
to: '["fred@example.com"]',
content: 'hello',
});
});
run_test('reply_message_errors', () => {
const bogus_message = {
type: 'bogus',
};
blueslip.set_test_data('error', 'unknown message type: bogus');
transmit.reply_message({
message: bogus_message,
});
blueslip.clear_test_data();
});