diff --git a/web/src/compose_recipient.js b/web/src/compose_recipient.js index 1754ce564c..bbc3979e7e 100644 --- a/web/src/compose_recipient.js +++ b/web/src/compose_recipient.js @@ -246,22 +246,7 @@ function item_click_callback(event, dropdown) { } function get_options_for_recipient_widget() { - const options = stream_data - .subscribed_subs() - .map((stream) => ({ - name: stream.name, - unique_id: stream.stream_id, - stream, - })) - .sort((a, b) => { - if (a.name.toLowerCase() < b.name.toLowerCase()) { - return -1; - } - if (a.name.toLowerCase() > b.name.toLowerCase()) { - return 1; - } - return 0; - }); + const options = stream_data.get_options_for_dropdown_widget(); const direct_messages_option = { is_direct_message: true, diff --git a/web/src/stream_data.js b/web/src/stream_data.js index 2f1d7884d4..c1ab54a61a 100644 --- a/web/src/stream_data.js +++ b/web/src/stream_data.js @@ -849,3 +849,25 @@ export function initialize(params) { export function remove_default_stream(stream_id) { default_stream_ids.delete(stream_id); } + +export function get_options_for_dropdown_widget() { + return ( + subscribed_subs() + .map((stream) => ({ + name: stream.name, + unique_id: stream.stream_id, + stream, + })) + // eslint-disable-next-line consistent-return, array-callback-return + .sort((a, b) => { + if (a.name.toLowerCase() < b.name.toLowerCase()) { + return -1; + } + if (a.name.toLowerCase() > b.name.toLowerCase()) { + return 1; + } + // Not possible since two streams cannot have same name. + // return 0; + }) + ); +} diff --git a/web/tests/stream_data.test.js b/web/tests/stream_data.test.js index a862f45174..af1dd98a1c 100644 --- a/web/tests/stream_data.test.js +++ b/web/tests/stream_data.test.js @@ -169,6 +169,35 @@ test("basics", () => { assert.equal(sub_store.get(-3), undefined); assert.equal(sub_store.get(undefined), undefined); assert.equal(sub_store.get(1), denmark); + + assert.deepEqual(stream_data.get_options_for_dropdown_widget(), [ + { + name: "social", + stream: { + color: "red", + history_public_to_subscribers: false, + invite_only: true, + is_muted: false, + name: "social", + stream_id: 2, + stream_post_policy: 2, + subscribed: true, + }, + unique_id: 2, + }, + { + name: "test", + stream: { + color: "yellow", + invite_only: false, + is_muted: true, + name: "test", + stream_id: 3, + subscribed: true, + }, + unique_id: 3, + }, + ]); }); test("get_subscribed_streams_for_user", () => { @@ -981,3 +1010,103 @@ test("can_unsubscribe_others", () => { page_params.is_admin = false; assert.equal(stream_data.can_unsubscribe_others(sub), false); }); + +test("options for dropdown widget", () => { + const denmark = { + subscribed: true, + color: "blue", + name: "Denmark", + stream_id: 1, + is_muted: true, + invite_only: true, + history_public_to_subscribers: true, + }; + const social = { + subscribed: true, + color: "red", + name: "social", + stream_id: 2, + is_muted: false, + invite_only: true, + history_public_to_subscribers: false, + stream_post_policy: stream_data.stream_post_policy_values.admins.code, + }; + const test = { + subscribed: true, + color: "yellow", + name: "test", + stream_id: 3, + is_muted: true, + invite_only: false, + }; + const web_public_stream = { + subscribed: true, + color: "yellow", + name: "web_public_stream", + stream_id: 4, + is_muted: false, + invite_only: false, + history_public_to_subscribers: true, + is_web_public: true, + }; + stream_data.add_sub(denmark); + stream_data.add_sub(social); + stream_data.add_sub(web_public_stream); + stream_data.add_sub(test); + + assert.deepEqual(stream_data.get_options_for_dropdown_widget(), [ + { + name: "Denmark", + stream: { + subscribed: true, + color: "blue", + name: "Denmark", + stream_id: 1, + is_muted: true, + invite_only: true, + history_public_to_subscribers: true, + }, + unique_id: 1, + }, + { + name: "social", + stream: { + color: "red", + history_public_to_subscribers: false, + invite_only: true, + is_muted: false, + name: "social", + stream_id: 2, + stream_post_policy: 2, + subscribed: true, + }, + unique_id: 2, + }, + { + name: "test", + stream: { + color: "yellow", + invite_only: false, + is_muted: true, + name: "test", + stream_id: 3, + subscribed: true, + }, + unique_id: 3, + }, + { + name: "web_public_stream", + stream: { + subscribed: true, + color: "yellow", + name: "web_public_stream", + stream_id: 4, + is_muted: false, + invite_only: false, + history_public_to_subscribers: true, + is_web_public: true, + }, + unique_id: 4, + }, + ]); +});