Skip to content

A platform for community discussion. Free, open, simple.

6.7 KB
Raw Blame
# frozen_string_literal: true

module PageObjects
  module Pages
    class ChatSidebar < PageObjects::Pages::Base
      PUBLIC_CHANNELS_SECTION_SELECTOR = ".sidebar-section[data-section-name='chat-channels']"
      DM_CHANNELS_SECTION_SELECTOR = ".sidebar-section[data-section-name='chat-dms']"
      STARRED_CHANNELS_SECTION_SELECTOR =
        ".sidebar-section[data-section-name='chat-starred-channels']"

      def has_no_public_channels_section?
        has_no_css?(PUBLIC_CHANNELS_SECTION_SELECTOR)
      end

      def has_no_dm_channels_section?
        has_no_css?(DM_CHANNELS_SECTION_SELECTOR)
      end

      def has_no_starred_channels_section?
        has_no_css?(STARRED_CHANNELS_SECTION_SELECTOR)
      end

      def channels_section
        find(PUBLIC_CHANNELS_SECTION_SELECTOR)
      end

      def dms_section
        find(DM_CHANNELS_SECTION_SELECTOR)
      end

      def starred_section
        find(STARRED_CHANNELS_SECTION_SELECTOR)
      end

      def open_browse
        open_channel_list_options.option('[data-menu-option-id="browseChannels"]').click
      end

      def open_channel_list_options
        open_list_options(PUBLIC_CHANNELS_SECTION_SELECTOR)
      end

      def open_list_options(selector)
        find(selector).hover
        menu =
          PageObjects::Components::DMenu.new(
            "#{selector} [data-sidebar-action-id='channelListOptions']",
            "chat-channel-list-options-menu",
          )
        menu.expand
        menu
      end

      def set_channel_filter(filter)
        set_list_filter(PUBLIC_CHANNELS_SECTION_SELECTOR, filter)
      end

      def set_starred_filter(filter)
        set_list_filter(STARRED_CHANNELS_SECTION_SELECTOR, filter)
      end

      def set_dm_filter(filter)
        set_list_filter(DM_CHANNELS_SECTION_SELECTOR, filter)
      end

      def set_list_filter(selector, filter)
        menu = open_list_options(selector)
        filter_trigger = menu.option('[data-menu-option-id="filterChannels"]')
        filter_trigger.click
        submenu =
          PageObjects::Components::DMenu.new(filter_trigger, "chat-channel-list-filter-menu")
        submenu.option(%([data-menu-option-id="#{filter}"])).click
      end

      def set_channel_sort(sort)
        menu = open_channel_list_options
        sort_trigger = menu.option('[data-menu-option-id="sortChannels"]')
        sort_trigger.click
        submenu = PageObjects::Components::DMenu.new(sort_trigger, "chat-channel-list-sort-menu")
        submenu.option(%([data-menu-option-id="#{sort}"])).click
      end

      def toggle_channel_filter
        find("[data-sidebar-action-id='toggleChannelFilter']").click
      end

      def open_channel(channel)
        find(".sidebar-section-link.channel-#{channel.id}").click
      end

      def remove_channel(channel)
        menu = open_channel_hover_menu(channel)
        menu.option(".chat-channel-sidebar-link-menu__leave-channel").click
      end

      def open_channel_settings(channel)
        menu = open_channel_hover_menu(channel)
        menu.option(".chat-channel-sidebar-link-menu__channel-settings").click
      end

      def toggle_star_channel(channel)
        menu = open_channel_hover_menu(channel)
        menu.option(".chat-channel-sidebar-link-menu__star-channel").click
      end

      def channel_section_link_selector(channel)
        ".sidebar-section-link.channel-#{channel.id}"
      end

      def open_channel_hover_menu(channel)
        find(channel_section_link_selector(channel)).hover
        first_level_hover_menu =
          PageObjects::Components::DMenu.new(
            "#{channel_section_link_selector(channel)} .sidebar-section-hover-button",
            (
              if channel.direct_message_channel?
                "chat-direct-message-channel-menu"
              else
                "chat-channel-menu"
              end
            ),
          )
        first_level_hover_menu.expand
        first_level_hover_menu
      end

      def find_channel(channel)
        find(".sidebar-section-link.channel-#{channel.id}")
        self
      end

      def has_channel?(channel)
        has_css?(".sidebar-section-link.channel-#{channel.id}")
      end

      def has_no_channel?(channel)
        has_no_css?(".sidebar-section-link.channel-#{channel.id}")
      end

      def channel_names
        within(channels_section) { all(".sidebar-section-link-content-text").map(&:text) }
      end

      def has_user_threads_section?
        has_css?(
          ".sidebar-section-link[data-link-name='user-threads'][href='/chat/threads']",
          text: I18n.t("js.chat.my_threads.title"),
        )
      end

      def has_no_user_threads_section?
        has_no_css?(
          ".sidebar-section-link[data-link-name='user-threads'][href='/chat/threads']",
          text: I18n.t("js.chat.my_threads.title"),
        )
      end

      def has_unread_user_threads?
        has_css?(
          ".sidebar-section-link[data-link-name='user-threads'] .sidebar-section-link-suffix.icon.unread",
        )
      end

      def has_no_unread_user_threads?
        has_no_css?(
          ".sidebar-section-link[data-link-name='user-threads'] .sidebar-section-link-suffix.icon.unread",
        )
      end

      def has_unread_channel?(channel)
        has_css?(
          ".sidebar-section-link.channel-#{channel.id} .sidebar-section-link-content-badge:is(.unread, .urgent)",
        )
      end

      def has_no_unread_channel?(channel)
        has_no_css?(
          ".sidebar-section-link.channel-#{channel.id} .sidebar-section-link-content-badge:is(.unread, .urgent)",
        )
      end

      def has_active_channel?(channel)
        has_css?(".sidebar-section-link.channel-#{channel.id}.active")
      end

      def has_no_active_channel?(channel)
        has_no_css?(".sidebar-section-link.channel-#{channel.id}.active")
      end

      def open_notification_settings(channel)
        menu = open_channel_hover_menu(channel)
        notification_button =
          menu.option(".chat-channel-sidebar-link-menu__open-notification-settings")
        notification_button.click

        PageObjects::Components::DMenu.new(
          notification_button,
          "chat-channel-menu-notification-submenu",
        )
      end

      # Requires open_notification_settings to be called first
      def set_notification_level(level)
        find(".chat-channel-sidebar-link-menu__notification-level-#{level}").click
        has_no_css?(".chat-channel-sidebar-link-menu__notification-level-#{level}")
        self
      end

      # Requires open_notification_settings to be called first
      def toggle_mute_channel
        find(".chat-channel-sidebar-link-menu__mute-channel").click
        has_no_css?(".chat-channel-sidebar-link-menu__mute-channel")
        self
      end
    end
  end
end