2020-04-13 13:52:07 +02:00
|
|
|
zrequire('scroll_util');
|
2018-06-21 06:58:19 +02:00
|
|
|
zrequire('list_render');
|
2018-04-17 21:29:21 +02:00
|
|
|
|
|
|
|
// We need these stubs to get by instanceof checks.
|
|
|
|
// The list_render library allows you to insert objects
|
|
|
|
// that are either jQuery, Element, or just raw HTML
|
|
|
|
// strings. We initially test with raw strings.
|
|
|
|
set_global('jQuery', 'stub');
|
|
|
|
set_global('Element', function () {
|
|
|
|
return { };
|
|
|
|
});
|
|
|
|
|
|
|
|
// We only need very simple jQuery wrappers for when the
|
|
|
|
// "real" code wraps html or sets up click handlers.
|
|
|
|
// We'll simulate most other objects ourselves.
|
|
|
|
set_global('$', (arg) => {
|
|
|
|
if (arg.to_jquery) {
|
|
|
|
return arg.to_jquery();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
html: () => arg,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
function make_containers() {
|
|
|
|
// We build objects here that simulate jQuery containers.
|
|
|
|
// The main thing to do at first is simulate that our
|
|
|
|
// parent container is the nearest ancestor to our main
|
|
|
|
// container that has a max-height attribute, and then
|
|
|
|
// the parent container will have a scroll event attached to
|
2020-04-11 14:07:05 +02:00
|
|
|
// it. This is a good time to read set_up_event_handlers
|
|
|
|
// in the real code.
|
2018-04-17 21:29:21 +02:00
|
|
|
const parent_container = {};
|
|
|
|
const container = {};
|
|
|
|
|
|
|
|
container.parent = () => parent_container;
|
|
|
|
container.length = () => 1;
|
|
|
|
container.is = () => false;
|
|
|
|
container.css = (prop) => {
|
|
|
|
assert.equal(prop, 'max-height');
|
|
|
|
return 'none';
|
|
|
|
};
|
|
|
|
|
|
|
|
parent_container.is = () => false;
|
|
|
|
parent_container.length = () => 1;
|
|
|
|
parent_container.css = (prop) => {
|
|
|
|
assert.equal(prop, 'max-height');
|
|
|
|
return 100;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Capture the scroll callback so we can call it in
|
|
|
|
// our tests.
|
2020-04-14 12:56:54 +02:00
|
|
|
parent_container.on = (sel, f) => {
|
|
|
|
assert.equal(sel, 'scroll.list_widget_container');
|
2018-04-17 21:29:21 +02:00
|
|
|
parent_container.call_scroll = () => {
|
|
|
|
f.call(parent_container);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// Make our append function just set a field we can
|
|
|
|
// check in our tests.
|
|
|
|
container.append = (data) => {
|
|
|
|
container.appended_data = data;
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
container: container,
|
|
|
|
parent_container: parent_container,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function make_search_input() {
|
|
|
|
const $element = {};
|
|
|
|
|
|
|
|
// Allow ourselves to be wrapped by $(...) and
|
|
|
|
// return ourselves.
|
|
|
|
$element.to_jquery = () => $element;
|
|
|
|
|
|
|
|
$element.on = (event_name, f) => {
|
2020-04-14 12:56:54 +02:00
|
|
|
assert.equal(event_name, 'input.list_widget_filter');
|
2018-04-17 21:29:21 +02:00
|
|
|
$element.simulate_input_event = () => {
|
|
|
|
const elem = {
|
|
|
|
value: $element.val(),
|
|
|
|
};
|
|
|
|
f.call(elem);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
|
|
|
|
function div(item) {
|
|
|
|
return '<div>' + item + '</div>';
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:45:33 +02:00
|
|
|
run_test('scrolling', () => {
|
2018-04-17 21:29:21 +02:00
|
|
|
const {container, parent_container} = make_containers();
|
|
|
|
|
2020-04-12 19:45:33 +02:00
|
|
|
const items = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < 200; i += 1) {
|
|
|
|
items.push('item ' + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
modifier: (item) => item,
|
|
|
|
};
|
|
|
|
|
|
|
|
container.html = (html) => { assert.equal(html, ''); };
|
2020-04-11 16:23:29 +02:00
|
|
|
list_render.create(container, items, opts);
|
2020-04-12 19:45:33 +02:00
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
container.appended_data.html(),
|
|
|
|
items.slice(0, 80).join('')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Set up our fake geometry so it forces a scroll action.
|
|
|
|
parent_container.scrollTop = 180;
|
|
|
|
parent_container.clientHeight = 100;
|
|
|
|
parent_container.scrollHeight = 260;
|
|
|
|
|
|
|
|
// Scrolling gets the next two elements from the list into
|
|
|
|
// our widget.
|
|
|
|
parent_container.call_scroll();
|
|
|
|
assert.deepEqual(
|
|
|
|
container.appended_data.html(),
|
|
|
|
items.slice(80, 100).join('')
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test('filtering', () => {
|
|
|
|
const {container} = make_containers();
|
|
|
|
|
2018-04-17 21:29:21 +02:00
|
|
|
const search_input = make_search_input();
|
|
|
|
|
|
|
|
const list = [
|
|
|
|
'apple',
|
|
|
|
'banana',
|
|
|
|
'carrot',
|
|
|
|
'dog',
|
|
|
|
'egg',
|
|
|
|
'fence',
|
|
|
|
'grape',
|
|
|
|
];
|
2020-03-10 19:48:03 +01:00
|
|
|
let opts = {
|
2018-04-17 21:29:21 +02:00
|
|
|
filter: {
|
|
|
|
element: search_input,
|
2019-12-30 17:44:24 +01:00
|
|
|
predicate: (item, value) => {
|
2020-02-08 04:04:36 +01:00
|
|
|
return item.includes(value);
|
2019-12-30 16:13:42 +01:00
|
|
|
},
|
2018-04-17 21:29:21 +02:00
|
|
|
},
|
|
|
|
modifier: (item) => div(item),
|
|
|
|
};
|
|
|
|
|
2020-04-12 19:45:33 +02:00
|
|
|
container.html = (html) => { assert.equal(html, ''); };
|
2020-03-10 19:48:03 +01:00
|
|
|
let widget = list_render.create(container, list, opts);
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2020-04-12 19:45:33 +02:00
|
|
|
let expected_html =
|
|
|
|
'<div>apple</div>' +
|
|
|
|
'<div>banana</div>' +
|
|
|
|
'<div>carrot</div>' +
|
|
|
|
'<div>dog</div>' +
|
|
|
|
'<div>egg</div>' +
|
|
|
|
'<div>fence</div>' +
|
|
|
|
'<div>grape</div>';
|
2018-04-17 21:29:21 +02:00
|
|
|
|
|
|
|
assert.deepEqual(container.appended_data.html(), expected_html);
|
|
|
|
|
|
|
|
// Filtering will pick out dog/egg/grape when we put "g"
|
|
|
|
// into our search input. (This uses the default filter, which
|
|
|
|
// is a glorified indexOf call.)
|
|
|
|
search_input.val = () => 'g';
|
|
|
|
search_input.simulate_input_event();
|
|
|
|
expected_html = '<div>dog</div><div>egg</div><div>grape</div>';
|
|
|
|
assert.deepEqual(container.appended_data.html(), expected_html);
|
|
|
|
|
|
|
|
// We can insert new data into the widget.
|
|
|
|
const new_data = [
|
|
|
|
'greta',
|
|
|
|
'faye',
|
|
|
|
'gary',
|
|
|
|
'frank',
|
|
|
|
'giraffe',
|
|
|
|
'fox',
|
|
|
|
];
|
|
|
|
|
2020-04-15 01:29:34 +02:00
|
|
|
widget.replace_list_data(new_data);
|
2020-04-12 19:45:33 +02:00
|
|
|
expected_html =
|
|
|
|
'<div>greta</div>' +
|
|
|
|
'<div>gary</div>' +
|
|
|
|
'<div>giraffe</div>';
|
2018-04-17 21:29:21 +02:00
|
|
|
assert.deepEqual(container.appended_data.html(), expected_html);
|
2020-03-10 19:48:03 +01:00
|
|
|
|
|
|
|
// Opts does not require a filter key.
|
|
|
|
opts = {
|
|
|
|
modifier: (item) => div(item),
|
|
|
|
};
|
|
|
|
list_render.validate_filter(opts);
|
|
|
|
widget = list_render.create(container, ['apple', 'banana'], opts);
|
|
|
|
widget.render();
|
|
|
|
|
2020-04-12 19:45:33 +02:00
|
|
|
expected_html =
|
|
|
|
'<div>apple</div>' +
|
|
|
|
'<div>banana</div>';
|
2020-03-10 19:48:03 +01:00
|
|
|
assert.deepEqual(container.appended_data.html(), expected_html);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-04-17 21:29:21 +02:00
|
|
|
|
|
|
|
function sort_button(opts) {
|
|
|
|
// The complications here are due to needing to find
|
|
|
|
// the list via complicated HTML assumptions. Also, we
|
|
|
|
// don't have any abstraction for the button and its
|
|
|
|
// siblings other than direct jQuery actions.
|
|
|
|
|
|
|
|
function data(sel) {
|
|
|
|
switch (sel) {
|
2018-05-07 03:30:13 +02:00
|
|
|
case "sort": return opts.sort_type;
|
|
|
|
case "sort-prop": return opts.prop_name;
|
|
|
|
default: throw Error('unknown selector: ' + sel);
|
2018-04-17 21:29:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function lookup(sel, value) {
|
|
|
|
return (selector) => {
|
|
|
|
assert.equal(sel, selector);
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-15 12:22:23 +02:00
|
|
|
const button = {
|
2018-04-17 21:29:21 +02:00
|
|
|
data: data,
|
2019-08-22 06:23:41 +02:00
|
|
|
closest: lookup('.progressive-table-wrapper', {
|
|
|
|
data: lookup('list-render', opts.list_name),
|
2018-04-17 21:29:21 +02:00
|
|
|
}),
|
2020-04-13 16:13:06 +02:00
|
|
|
hasClass: (sel) => {
|
|
|
|
if (sel === 'active') {
|
|
|
|
return opts.active;
|
|
|
|
}
|
|
|
|
assert.equal(sel, 'descend');
|
|
|
|
return false;
|
|
|
|
},
|
2018-04-17 21:29:21 +02:00
|
|
|
siblings: lookup('.active', {
|
|
|
|
removeClass: (sel) => {
|
|
|
|
assert.equal(sel, 'active');
|
|
|
|
button.siblings_deactivated = true;
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
addClass: (sel) => {
|
|
|
|
assert.equal(sel, 'active');
|
|
|
|
button.activated = true;
|
|
|
|
},
|
|
|
|
siblings_deactivated: false,
|
|
|
|
activated: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
2019-12-30 16:13:42 +01:00
|
|
|
run_test('filtering', () => {
|
|
|
|
const lst = [
|
|
|
|
'alexander',
|
|
|
|
'alice',
|
|
|
|
'benedict',
|
|
|
|
'JESSE',
|
|
|
|
'scott',
|
|
|
|
'Stephanie',
|
|
|
|
'Xavier',
|
|
|
|
];
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
filter: {
|
2019-12-30 17:44:24 +01:00
|
|
|
predicate: (item, value) => {
|
2019-12-30 16:13:42 +01:00
|
|
|
return item.length === value;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const custom_result = list_render.filter(5, lst, opts);
|
|
|
|
assert.deepEqual(custom_result, [
|
|
|
|
'alice',
|
|
|
|
'JESSE',
|
|
|
|
'scott',
|
|
|
|
]);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('sorting', () => {
|
2018-04-17 21:29:21 +02:00
|
|
|
const {container} = make_containers();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let cleared;
|
2018-04-17 21:29:21 +02:00
|
|
|
container.html = (html) => {
|
|
|
|
assert.equal(html, '');
|
|
|
|
cleared = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const alice = { name: 'alice', salary: 50 };
|
2019-04-22 01:09:02 +02:00
|
|
|
const bob = { name: 'Bob', salary: 40 };
|
2018-04-17 21:29:21 +02:00
|
|
|
const cal = { name: 'cal', salary: 30 };
|
|
|
|
const dave = { name: 'dave', salary: 25 };
|
|
|
|
|
|
|
|
const list = [bob, dave, alice, cal];
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
name: 'my-list',
|
|
|
|
modifier: (item) => {
|
|
|
|
return div(item.name) + div(item.salary);
|
|
|
|
},
|
2019-12-30 16:13:42 +01:00
|
|
|
filter: {
|
2019-12-30 17:44:24 +01:00
|
|
|
predicate: () => true,
|
2019-12-30 16:13:42 +01:00
|
|
|
},
|
2018-04-17 21:29:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function html_for(people) {
|
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-08 02:43:49 +01:00
|
|
|
return people.map(opts.modifier).join('');
|
2018-04-17 21:29:21 +02:00
|
|
|
}
|
|
|
|
|
2020-04-15 12:22:23 +02:00
|
|
|
const widget = list_render.create(container, list, opts);
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let button_opts;
|
|
|
|
let button;
|
|
|
|
let expected_html;
|
2018-04-17 21:29:21 +02:00
|
|
|
|
|
|
|
button_opts = {
|
|
|
|
sort_type: 'alphabetic',
|
|
|
|
prop_name: 'name',
|
|
|
|
list_name: 'my-list',
|
|
|
|
active: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
button = sort_button(button_opts);
|
|
|
|
|
2020-04-15 12:22:23 +02:00
|
|
|
list_render.handle_sort(button, widget);
|
2018-04-17 21:29:21 +02:00
|
|
|
|
|
|
|
assert(cleared);
|
|
|
|
assert(button.siblings_deactivated);
|
|
|
|
|
|
|
|
expected_html = html_for([alice, bob, cal, dave]);
|
|
|
|
assert.deepEqual(container.appended_data.html(), expected_html);
|
|
|
|
|
|
|
|
// Now try a numeric sort.
|
|
|
|
button_opts = {
|
|
|
|
sort_type: 'numeric',
|
|
|
|
prop_name: 'salary',
|
|
|
|
list_name: 'my-list',
|
|
|
|
active: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
button = sort_button(button_opts);
|
|
|
|
|
|
|
|
cleared = false;
|
|
|
|
button.siblings_deactivated = false;
|
|
|
|
|
2020-04-15 12:22:23 +02:00
|
|
|
list_render.handle_sort(button, widget);
|
2018-04-17 21:29:21 +02:00
|
|
|
|
|
|
|
assert(cleared);
|
|
|
|
assert(button.siblings_deactivated);
|
|
|
|
|
|
|
|
expected_html = html_for([dave, cal, bob, alice]);
|
|
|
|
assert.deepEqual(container.appended_data.html(), expected_html);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|