node tests: Avoid people stubs for poll test.

We also have Alice vote on the current user's poll.
This commit is contained in:
Steve Howell 2021-04-16 12:43:17 +00:00 committed by Steve Howell
parent e71f906eba
commit 8ffb828030
1 changed files with 48 additions and 19 deletions

View File

@ -14,13 +14,24 @@ const poll_widget = zrequire("poll_widget");
const people = zrequire("people"); const people = zrequire("people");
const me = {
email: "me@zulip.com",
full_name: "Me Myself",
user_id: 99,
};
const alice = {
email: "alice@zulip.com",
full_name: "Alice Lee",
user_id: 100,
};
people.add_active_user(me);
people.add_active_user(alice);
people.initialize_current_user(me.user_id);
run_test("PollData my question", () => { run_test("PollData my question", () => {
const is_my_poll = true; const is_my_poll = true;
const question = "Favorite color?"; const question = "Favorite color?";
const sender_id = 99;
people.my_current_user_id = () => sender_id;
const data_holder = new poll_widget.PollData(is_my_poll, question, []); const data_holder = new poll_widget.PollData(is_my_poll, question, []);
let data = data_holder.get_widget_data(); let data = data_holder.get_widget_data();
@ -35,7 +46,7 @@ run_test("PollData my question", () => {
question: "best plan?", question: "best plan?",
}; };
data_holder.handle_event(sender_id, question_event); data_holder.handle_event(me.user_id, question_event);
data = data_holder.get_widget_data(); data = data_holder.get_widget_data();
assert.deepEqual(data, { assert.deepEqual(data, {
@ -49,9 +60,7 @@ run_test("PollData my question", () => {
option: "release now", option: "release now",
}; };
people.safe_full_names = () => ""; data_holder.handle_event(me.user_id, option_event);
data_holder.handle_event(sender_id, option_event);
data = data_holder.get_widget_data(); data = data_holder.get_widget_data();
assert.deepEqual(data, { assert.deepEqual(data, {
@ -73,14 +82,14 @@ run_test("PollData my question", () => {
vote: 1, vote: 1,
}; };
data_holder.handle_event(sender_id, vote_event); data_holder.handle_event(me.user_id, vote_event);
data = data_holder.get_widget_data(); data = data_holder.get_widget_data();
assert.deepEqual(data, { assert.deepEqual(data, {
options: [ options: [
{ {
option: "release now", option: "release now",
names: "", names: "Me Myself",
count: 1, count: 1,
key: "99,1", key: "99,1",
current_user_vote: true, current_user_vote: true,
@ -89,6 +98,28 @@ run_test("PollData my question", () => {
question: "best plan?", question: "best plan?",
}); });
vote_event = {
type: "vote",
key: "99,1",
vote: 1,
};
data_holder.handle_event(alice.user_id, vote_event);
data = data_holder.get_widget_data();
assert.deepEqual(data, {
options: [
{
option: "release now",
names: "Me Myself, Alice Lee",
count: 2,
key: "99,1",
current_user_vote: true,
},
],
question: "best plan?",
});
const invalid_vote_event = { const invalid_vote_event = {
type: "vote", type: "vote",
key: "98,1", key: "98,1",
@ -96,7 +127,7 @@ run_test("PollData my question", () => {
}; };
blueslip.expect("warn", `unknown key for poll: ${invalid_vote_event.key}`); blueslip.expect("warn", `unknown key for poll: ${invalid_vote_event.key}`);
data_holder.handle_event(sender_id, invalid_vote_event); data_holder.handle_event(me.user_id, invalid_vote_event);
data = data_holder.get_widget_data(); data = data_holder.get_widget_data();
const option_outbound_event = data_holder.handle.new_option.outbound("new option"); const option_outbound_event = data_holder.handle.new_option.outbound("new option");
@ -122,15 +153,15 @@ run_test("PollData my question", () => {
vote: -1, vote: -1,
}; };
data_holder.handle_event(sender_id, vote_event); data_holder.handle_event(me.user_id, vote_event);
data = data_holder.get_widget_data(); data = data_holder.get_widget_data();
assert.deepEqual(data, { assert.deepEqual(data, {
options: [ options: [
{ {
option: "release now", option: "release now",
names: "", names: "Alice Lee",
count: 0, count: 1,
key: "99,1", key: "99,1",
current_user_vote: false, current_user_vote: false,
}, },
@ -140,7 +171,6 @@ run_test("PollData my question", () => {
}); });
run_test("activate another person poll", () => { run_test("activate another person poll", () => {
people.is_my_user_id = () => false;
stub_templates((template_name) => { stub_templates((template_name) => {
if (template_name === "widgets/poll_widget") { if (template_name === "widgets/poll_widget") {
return "widgets/poll_widget"; return "widgets/poll_widget";
@ -162,7 +192,7 @@ run_test("activate another person poll", () => {
elem: widget_elem, elem: widget_elem,
callback, callback,
message: { message: {
sender_id: 100, sender_id: alice.user_id,
}, },
extra_data: { extra_data: {
question: "What do you want?", question: "What do you want?",
@ -222,7 +252,7 @@ run_test("activate another person poll", () => {
const vote_events = [ const vote_events = [
{ {
sender_id: 100, sender_id: alice.user_id,
data: { data: {
type: "new_option", type: "new_option",
idx: 1, idx: 1,
@ -230,7 +260,7 @@ run_test("activate another person poll", () => {
}, },
}, },
{ {
sender_id: 100, sender_id: alice.user_id,
data: { data: {
type: "vote", type: "vote",
key: "100,1", key: "100,1",
@ -263,7 +293,6 @@ run_test("activate another person poll", () => {
}); });
run_test("activate own poll", () => { run_test("activate own poll", () => {
people.is_my_user_id = () => true;
stub_templates((template_name) => { stub_templates((template_name) => {
if (template_name === "widgets/poll_widget") { if (template_name === "widgets/poll_widget") {
return "widgets/poll_widget"; return "widgets/poll_widget";
@ -283,7 +312,7 @@ run_test("activate own poll", () => {
elem: widget_elem, elem: widget_elem,
callback, callback,
message: { message: {
sender_id: 100, sender_id: me.user_id,
}, },
extra_data: { extra_data: {
question: "Where to go?", question: "Where to go?",