From 23047612238b41df6bb0dfa86b6e305c3ec6e59a Mon Sep 17 00:00:00 2001
From: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Date: Wed, 21 Dec 2022 17:01:20 +0100
Subject: [PATCH] FIX: correctly sorts public channels (#19555)

---
 .../discourse/services/chat-channels-manager.js | 10 ++++++----
 .../spec/system/list_channels/mobile_spec.rb    | 17 +++++++++++++++++
 .../system/list_channels/no_sidebar_spec.rb     | 17 +++++++++++++++++
 .../spec/system/list_channels/sidebar_spec.rb   | 17 +++++++++++++++++
 4 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js b/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js
index e755624b1f3..ab556c1cf6e 100644
--- a/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js
+++ b/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js
@@ -86,10 +86,12 @@ export default class ChatChannelsManager extends Service {
   }
 
   get publicMessageChannels() {
-    return this.channels.filter(
-      (channel) =>
-        channel.isCategoryChannel && channel.currentUserMembership.following
-    );
+    return this.channels
+      .filter(
+        (channel) =>
+          channel.isCategoryChannel && channel.currentUserMembership.following
+      )
+      .sort((a, b) => a.title.localeCompare(b.title));
   }
 
   get directMessageChannels() {
diff --git a/plugins/chat/spec/system/list_channels/mobile_spec.rb b/plugins/chat/spec/system/list_channels/mobile_spec.rb
index 025adc640ff..073ca8533df 100644
--- a/plugins/chat/spec/system/list_channels/mobile_spec.rb
+++ b/plugins/chat/spec/system/list_channels/mobile_spec.rb
@@ -31,6 +31,23 @@ RSpec.describe "List channels | mobile", type: :system, js: true, mobile: true d
       end
     end
 
+    context "when multiple category channels are present" do
+      fab!(:channel_1) { Fabricate(:category_channel, name: "b channel") }
+      fab!(:channel_2) { Fabricate(:category_channel, name: "a channel") }
+
+      before do
+        channel_1.add(current_user)
+        channel_2.add(current_user)
+      end
+
+      it "sorts them alphabetically" do
+        visit("/chat")
+
+        expect(page.find("#public-channels a:nth-child(1)")["data-chat-channel-id"]).to eq(channel_2.id.to_s)
+        expect(page.find("#public-channels a:nth-child(2)")["data-chat-channel-id"]).to eq(channel_1.id.to_s)
+      end
+    end
+
     context "when direct message channels" do
       fab!(:dm_channel_1) { Fabricate(:direct_message_channel, users: [current_user]) }
       fab!(:inaccessible_dm_channel_1) { Fabricate(:direct_message_channel) }
diff --git a/plugins/chat/spec/system/list_channels/no_sidebar_spec.rb b/plugins/chat/spec/system/list_channels/no_sidebar_spec.rb
index 8f23c39413f..37266c51a2d 100644
--- a/plugins/chat/spec/system/list_channels/no_sidebar_spec.rb
+++ b/plugins/chat/spec/system/list_channels/no_sidebar_spec.rb
@@ -32,6 +32,23 @@ RSpec.describe "List channels | no sidebar", type: :system, js: true do
       end
     end
 
+    context "when multiple category channels are present" do
+      fab!(:channel_1) { Fabricate(:category_channel, name: "b channel") }
+      fab!(:channel_2) { Fabricate(:category_channel, name: "a channel") }
+
+      before do
+        channel_1.add(current_user)
+        channel_2.add(current_user)
+      end
+
+      it "sorts them alphabetically" do
+        visit("/chat")
+
+        expect(page.find("#public-channels a:nth-child(1)")["data-chat-channel-id"]).to eq(channel_2.id.to_s)
+        expect(page.find("#public-channels a:nth-child(2)")["data-chat-channel-id"]).to eq(channel_1.id.to_s)
+      end
+    end
+
     context "when direct message channels" do
       fab!(:dm_channel_1) { Fabricate(:direct_message_channel, users: [current_user]) }
       fab!(:inaccessible_dm_channel_1) { Fabricate(:direct_message_channel) }
diff --git a/plugins/chat/spec/system/list_channels/sidebar_spec.rb b/plugins/chat/spec/system/list_channels/sidebar_spec.rb
index 3ebc9da89b2..aae03c9c44a 100644
--- a/plugins/chat/spec/system/list_channels/sidebar_spec.rb
+++ b/plugins/chat/spec/system/list_channels/sidebar_spec.rb
@@ -41,6 +41,23 @@ RSpec.describe "List channels | sidebar", type: :system, js: true do
       end
     end
 
+    context "when multiple category channels are present" do
+      fab!(:channel_1) { Fabricate(:category_channel, name: "b channel") }
+      fab!(:channel_2) { Fabricate(:category_channel, name: "a channel") }
+
+      before do
+        channel_1.add(current_user)
+        channel_2.add(current_user)
+      end
+
+      it "sorts them alphabetically" do
+        visit("/")
+
+        expect(page.find("#sidebar-section-content-chat-channels li:nth-child(1)")).to have_css(".channel-#{channel_2.id}")
+        expect(page.find("#sidebar-section-content-chat-channels li:nth-child(2)")).to have_css(".channel-#{channel_1.id}")
+      end
+    end
+
     context "when direct message channels" do
       fab!(:dm_channel_1) { Fabricate(:direct_message_channel, users: [current_user]) }
       fab!(:inaccessible_dm_channel_1) { Fabricate(:direct_message_channel) }