pill_typeahead: Simplify fake this objects.

This commit is contained in:
evykassirer 2024-03-29 17:38:36 -07:00 committed by Tim Abbott
parent fc3ab45b46
commit 5cb32fe733
1 changed files with 26 additions and 47 deletions

View File

@ -146,56 +146,35 @@ run_test("set_up", ({mock_template, override}) => {
assert.equal(typeof config.updater, "function"); assert.equal(typeof config.updater, "function");
// test queries // test queries
const fake_stream_this = { const stream_query = "#Denmark";
query: "#Denmark", const person_query = "me";
}; const group_query = "test";
const fake_person_this = {
query: "me",
};
const fake_group_this = {
query: "test",
};
(function test_highlighter() { (function test_highlighter() {
if (opts.stream) { if (opts.stream) {
// Test stream highlighter_html for widgets that allow stream pills. // Test stream highlighter_html for widgets that allow stream pills.
assert.equal( assert.equal(config.highlighter_html(denmark, stream_query), $fake_rendered_stream);
config.highlighter_html(denmark, fake_stream_this.query),
$fake_rendered_stream,
);
} }
if (opts.user_group && opts.user) { if (opts.user_group && opts.user) {
// If user is also allowed along with user_group // If user is also allowed along with user_group
// then we should check that each of them rendered correctly. // then we should check that each of them rendered correctly.
assert.equal( assert.equal(config.highlighter_html(testers, group_query), $fake_rendered_group);
config.highlighter_html(testers, fake_group_this.query), assert.equal(config.highlighter_html(me, person_query), $fake_rendered_person);
$fake_rendered_group,
);
assert.equal(
config.highlighter_html(me, fake_person_this.query),
$fake_rendered_person,
);
} }
if (opts.user && !opts.user_group) { if (opts.user && !opts.user_group) {
assert.equal( assert.equal(config.highlighter_html(me, person_query), $fake_rendered_person);
config.highlighter_html(me, fake_person_this.query),
$fake_rendered_person,
);
} }
if (!opts.user && opts.user_group) { if (!opts.user && opts.user_group) {
assert.equal( assert.equal(config.highlighter_html(testers, group_query), $fake_rendered_group);
config.highlighter_html(testers, fake_group_this.query),
$fake_rendered_group,
);
} }
})(); })();
(function test_matcher() { (function test_matcher() {
let result; let result;
if (opts.stream) { if (opts.stream) {
result = config.matcher(denmark, fake_stream_this.query); result = config.matcher(denmark, stream_query);
assert.ok(result); assert.ok(result);
result = config.matcher(sweden, fake_stream_this.query); result = config.matcher(sweden, stream_query);
assert.ok(!result); assert.ok(!result);
} }
if (opts.user_group && opts.user) { if (opts.user_group && opts.user) {
@ -204,28 +183,28 @@ run_test("set_up", ({mock_template, override}) => {
or group is returned. */ or group is returned. */
// group query, with correct item. // group query, with correct item.
result = config.matcher(testers, fake_group_this.query); result = config.matcher(testers, group_query);
assert.ok(result); assert.ok(result);
// group query, with wrong item. // group query, with wrong item.
result = config.matcher(admins, fake_group_this.query); result = config.matcher(admins, group_query);
assert.ok(!result); assert.ok(!result);
// person query with correct item. // person query with correct item.
result = config.matcher(me, fake_person_this.query); result = config.matcher(me, person_query);
assert.ok(result); assert.ok(result);
// person query with wrong item. // person query with wrong item.
result = config.matcher(jill, fake_person_this.query); result = config.matcher(jill, person_query);
assert.ok(!result); assert.ok(!result);
} }
if (opts.user_group && !opts.user) { if (opts.user_group && !opts.user) {
result = config.matcher(testers, fake_group_this.query); result = config.matcher(testers, group_query);
assert.ok(result); assert.ok(result);
result = config.matcher(admins, fake_group_this.query); result = config.matcher(admins, group_query);
assert.ok(!result); assert.ok(!result);
} }
if (opts.user && !opts.user_group) { if (opts.user && !opts.user_group) {
result = config.matcher(me, fake_person_this.query); result = config.matcher(me, person_query);
assert.ok(result); assert.ok(result);
result = config.matcher(jill, fake_person_this.query); result = config.matcher(jill, person_query);
assert.ok(!result); assert.ok(!result);
} }
})(); })();
@ -233,17 +212,17 @@ run_test("set_up", ({mock_template, override}) => {
(function test_sorter() { (function test_sorter() {
if (opts.stream) { if (opts.stream) {
sort_streams_called = false; sort_streams_called = false;
config.sorter([], fake_stream_this.query); config.sorter([], stream_query);
assert.ok(sort_streams_called); assert.ok(sort_streams_called);
} }
if (opts.user_group) { if (opts.user_group) {
sort_recipients_called = false; sort_recipients_called = false;
config.sorter([testers], fake_group_this.query); config.sorter([testers], group_query);
assert.ok(sort_recipients_called); assert.ok(sort_recipients_called);
} }
if (opts.user) { if (opts.user) {
sort_recipients_called = false; sort_recipients_called = false;
config.sorter([me], fake_person_this.query); config.sorter([me], person_query);
assert.ok(sort_recipients_called); assert.ok(sort_recipients_called);
} }
})(); })();
@ -251,7 +230,7 @@ run_test("set_up", ({mock_template, override}) => {
(function test_source() { (function test_source() {
let result; let result;
if (opts.stream) { if (opts.stream) {
result = config.source(fake_stream_this.query); result = config.source(stream_query);
const stream_ids = result.map((stream) => stream.stream_id); const stream_ids = result.map((stream) => stream.stream_id);
const expected_stream_ids = [denmark.stream_id, sweden.stream_id]; const expected_stream_ids = [denmark.stream_id, sweden.stream_id];
assert.deepEqual(stream_ids, expected_stream_ids); assert.deepEqual(stream_ids, expected_stream_ids);
@ -262,7 +241,7 @@ run_test("set_up", ({mock_template, override}) => {
function is_group(item) { function is_group(item) {
return item.members; return item.members;
} }
result = config.source(fake_person_this.query); result = config.source(person_query);
actual_result = result actual_result = result
.map((item) => { .map((item) => {
if (is_group(item)) { if (is_group(item)) {
@ -305,11 +284,11 @@ run_test("set_up", ({mock_template, override}) => {
return pills.length; return pills.length;
} }
assert.equal(number_of_pills(), 0); assert.equal(number_of_pills(), 0);
config.updater(denmark, fake_stream_this.query); config.updater(denmark, stream_query);
assert.equal(number_of_pills(), 1); assert.equal(number_of_pills(), 1);
config.updater(me, fake_person_this.query); config.updater(me, person_query);
assert.equal(number_of_pills(), 2); assert.equal(number_of_pills(), 2);
config.updater(testers, fake_group_this.query); config.updater(testers, group_query);
assert.equal(number_of_pills(), 3); assert.equal(number_of_pills(), 3);
assert.ok(update_func_called); assert.ok(update_func_called);