Files
zulip/frontend_tests/node_tests/message_list_data.js
Anders Kaseorg ac7b09d57e js: Convert _.map(a, …) to a.map(…).
And convert the corresponding function expressions to arrow style
while we’re here.

import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import K from "ast-types/gen/kinds";
import fs from "fs";
import path from "path";
import process from "process";

const checkExpression = (node: n.Node): node is K.ExpressionKind =>
  n.Expression.check(node);

for (const file of process.argv.slice(2)) {
  console.log("Parsing", file);
  const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
    parser: path.extname(file) === ".ts" ? tsParser : babelParser,
  });
  let changed = false;

  recast.visit(ast, {
    visitCallExpression(path) {
      const { callee, arguments: args } = path.node;
      if (
        n.MemberExpression.check(callee) &&
        !callee.computed &&
        n.Identifier.check(callee.object) &&
        callee.object.name === "_" &&
        n.Identifier.check(callee.property) &&
        callee.property.name === "map" &&
        args.length === 2 &&
        checkExpression(args[0]) &&
        checkExpression(args[1])
      ) {
        const [arr, fn] = args;
        path.replace(
          b.callExpression(b.memberExpression(arr, b.identifier("map")), [
            n.FunctionExpression.check(fn) ||
            n.ArrowFunctionExpression.check(fn)
              ? b.arrowFunctionExpression(
                  fn.params,
                  n.BlockStatement.check(fn.body) &&
                    fn.body.body.length === 1 &&
                    n.ReturnStatement.check(fn.body.body[0])
                    ? fn.body.body[0].argument || b.identifier("undefined")
                    : fn.body
                )
              : fn,
          ])
        );
        changed = true;
      }
      this.traverse(path);
    },
  });

  if (changed) {
    console.log("Writing", file);
    fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
  }
}

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-10 14:08:12 -08:00

221 lines
5.6 KiB
JavaScript

zrequire('unread');
zrequire('util');
zrequire('Filter', 'js/filter');
zrequire('MessageListData', 'js/message_list_data');
set_global('page_params', {});
set_global('blueslip', global.make_zblueslip());
set_global('muting', {});
global.patch_builtin('setTimeout', (f, delay) => {
assert.equal(delay, 0);
return f();
});
function make_msg(msg_id) {
return {
id: msg_id,
unread: true,
topic: 'whatever',
};
}
function make_msgs(msg_ids) {
return msg_ids.map(make_msg);
}
function assert_contents(mld, msg_ids) {
const msgs = mld.all_messages();
assert.deepEqual(msgs, make_msgs(msg_ids));
}
run_test('basics', () => {
const mld = new MessageListData({
muting_enabled: false,
filter: undefined,
});
assert.equal(mld.is_search(), false);
assert(mld.can_mark_messages_read());
mld.add_anywhere(make_msgs([35, 25, 15, 45]));
assert_contents(mld, [15, 25, 35, 45]);
const new_msgs = make_msgs([10, 20, 30, 40, 50, 60, 70]);
const info = mld.add_messages(new_msgs);
assert.deepEqual(info, {
top_messages: make_msgs([10]),
interior_messages: make_msgs([20, 30, 40]),
bottom_messages: make_msgs([50, 60, 70]),
});
assert_contents(mld, [10, 15, 20, 25, 30, 35, 40, 45, 50, 60, 70]);
assert.equal(mld.selected_id(), -1);
assert.equal(mld.closest_id(8), 10);
assert.equal(mld.closest_id(27), 25);
assert.equal(mld.closest_id(72), 70);
mld.set_selected_id(50);
assert.equal(mld.selected_id(), 50);
assert.equal(mld.selected_idx(), 8);
mld.remove([mld.get(50)]);
assert_contents(mld, [10, 15, 20, 25, 30, 35, 40, 45, 60, 70]);
mld.update_items_for_muting();
assert_contents(mld, [10, 15, 20, 25, 30, 35, 40, 45, 60, 70]);
mld.reset_select_to_closest();
assert.equal(mld.selected_id(), 45);
assert.equal(mld.selected_idx(), 7);
assert.equal(mld.first_unread_message_id(), 10);
mld.get(10).unread = false;
assert.equal(mld.first_unread_message_id(), 15);
mld.clear();
assert_contents(mld, []);
assert.equal(mld.closest_id(99), -1);
assert.equal(mld.get_last_message_sent_by_me(), undefined);
mld.add_messages(make_msgs([120, 125.01, 130, 140]));
assert_contents(mld, [120, 125.01, 130, 140]);
mld.set_selected_id(125.01);
assert.equal(mld.selected_id(), 125.01);
mld.get(125.01).id = 145;
mld.change_message_id(125.01, 145, {
re_render: () => {},
});
assert_contents(mld, [120, 130, 140, 145]);
for (const msg of mld.all_messages()) {
msg.unread = false;
}
assert.equal(mld.first_unread_message_id(), 145);
});
run_test('muting enabled', () => {
const mld = new MessageListData({
muting_enabled: true,
filter: undefined,
});
muting.is_topic_muted = function () { return true; };
mld.add_anywhere(make_msgs([35, 25, 15, 45]));
assert_contents(mld, []);
mld.get(35).mentioned = true;
mld.update_items_for_muting();
assert.deepEqual(mld._items, [mld.get(35)]);
mld.remove(make_msgs([35, 15]));
assert_contents(mld, []);
assert.deepEqual(mld._all_items, make_msgs([25, 45]));
const msgs = make_msgs([10, 20]);
msgs[0].mentioned = true;
mld.prepend(msgs);
assert.deepEqual(mld._items, [mld.get(10)]);
assert.deepEqual(mld._all_items, msgs.concat(make_msgs([25, 45])));
mld.clear();
assert.deepEqual(mld._all_items, []);
});
run_test('more muting', () => {
muting.is_topic_muted = function (stream_id, topic) {
return topic === 'muted';
};
const mld = new MessageListData({
muting_enabled: true,
filter: undefined,
});
const orig_messages = [
{id: 3, topic: 'muted'},
{id: 4, topic: 'whatever'},
{id: 7, topic: 'muted'},
{id: 8, topic: 'whatever'},
];
const orig_info = mld.add_messages(orig_messages);
assert.deepEqual(orig_info, {
top_messages: [],
interior_messages: [],
bottom_messages: [
{id: 4, topic: 'whatever'},
{id: 8, topic: 'whatever'},
],
});
assert.deepEqual(
_.pluck(mld._all_items, 'id'),
[3, 4, 7, 8]
);
assert.deepEqual(
_.pluck(mld.all_messages(), 'id'),
[4, 8]
);
const more_messages = [
{id: 1, topic: 'muted'},
{id: 2, topic: 'whatever'},
{id: 3, topic: 'muted'}, // dup
{id: 5, topic: 'muted'},
{id: 6, topic: 'whatever'},
{id: 9, topic: 'muted'},
{id: 10, topic: 'whatever'},
];
const more_info = mld.add_messages(more_messages);
assert.deepEqual(
_.pluck(mld._all_items, 'id'),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
);
assert.deepEqual(
_.pluck(mld.all_messages(), 'id'),
[2, 4, 6, 8, 10]
);
assert.deepEqual(more_info, {
top_messages: [
{id: 2, topic: 'whatever'},
],
interior_messages: [
{id: 6, topic: 'whatever'},
],
bottom_messages: [
{id: 10, topic: 'whatever'},
],
});
});
run_test('errors', () => {
const mld = new MessageListData({
muting_enabled: false,
filter: undefined,
});
assert.equal(mld.get('bogus-id'), undefined);
blueslip.set_test_data('fatal', 'Bad message id');
mld._add_to_hash(['asdf']);
assert.equal(blueslip.get_test_logs('fatal').length, 1);
blueslip.set_test_data('error', 'Duplicate message added to MessageListData');
mld._hash[1] = 'taken';
mld._add_to_hash(make_msgs([1]));
assert.equal(blueslip.get_test_logs('error').length, 1);
});