Skip to content

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

DEV: Add system tests for tag filtering in discourse-templates (#37305)

Alan Guo Xiang Tan9dad3615c1bb
diff --git a/plugins/discourse-templates/spec/system/page_objects/components/d_templates_panel.rb b/plugins/discourse-templates/spec/system/page_objects/components/d_templates_panel.rb
new file mode 100644
index 00000000000..b4bcb1f4d03
--- /dev/null
+++ b/plugins/discourse-templates/spec/system/page_objects/components/d_templates_panel.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+module PageObjects
+ module Components
+ class DTemplatesPanel < PageObjects::Components::Base
+ PANEL_SELECTOR = ".d-templates-container"
+
+ def open
+ find(".toolbar-menu__options-trigger").click
+ find("button[title='#{I18n.t("js.templates.insert_template")}']").click
+ self
+ end
+
+ def tag_drop
+ PageObjects::Components::SelectKit.new("#{PANEL_SELECTOR} .tag-drop")
+ end
+
+ def has_templates?(*templates)
+ templates.all? { |template| has_css?("#template-item-#{template.id}") } &&
+ template_count == templates.size
+ end
+
+ def template_count
+ all("#{PANEL_SELECTOR} .template-item").count
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-templates/spec/system/tag_filtering_spec.rb b/plugins/discourse-templates/spec/system/tag_filtering_spec.rb
new file mode 100644
index 00000000000..91e8e02be0c
--- /dev/null
+++ b/plugins/discourse-templates/spec/system/tag_filtering_spec.rb
@@ -0,0 +1,56 @@
+# frozen_string_literal: true
+
+RSpec.describe "Filtering templates by tags", type: :system do
+ fab!(:current_user, :user)
+ fab!(:templates_category, :category)
+ fab!(:tag_1, :tag)
+ fab!(:tag_2, :tag)
+ fab!(:template_with_tag_1) do
+ Fabricate(:template_item, category: templates_category, tags: [tag_1])
+ end
+ fab!(:template_with_tag_2) do
+ Fabricate(:template_item, category: templates_category, tags: [tag_2])
+ end
+ fab!(:template_both_tags) do
+ Fabricate(:template_item, category: templates_category, tags: [tag_1, tag_2])
+ end
+ fab!(:template_no_tags) { Fabricate(:template_item, category: templates_category) }
+ fab!(:topic) { Fabricate(:post).topic }
+
+ let(:templates_panel) { PageObjects::Components::DTemplatesPanel.new }
+ let(:topic_page) { PageObjects::Pages::Topic.new }
+
+ before do
+ SiteSetting.discourse_templates_enabled = true
+ SiteSetting.discourse_templates_categories = templates_category.id.to_s
+ SiteSetting.tagging_enabled = true
+ sign_in(current_user)
+ end
+
+ context "when filtering templates" do
+ it "filters by tag and shows correct templates" do
+ topic_page.visit_topic(topic)
+ topic_page.click_reply_button
+
+ templates_panel.open
+ expect(templates_panel).to have_templates(
+ template_with_tag_1,
+ template_with_tag_2,
+ template_both_tags,
+ template_no_tags,
+ )
+
+ templates_panel.tag_drop.expand
+ templates_panel.tag_drop.select_row_by_name(tag_1.name)
+ expect(templates_panel).to have_templates(template_with_tag_1, template_both_tags)
+
+ templates_panel.tag_drop.expand
+ templates_panel.tag_drop.select_row_by_name(tag_2.name)
+ expect(templates_panel).to have_templates(template_with_tag_2, template_both_tags)
+
+ templates_panel.tag_drop.expand
+ templates_panel.tag_drop.select_row_by_value("no-tags")
+ expect(templates_panel).to have_templates(template_no_tags)
+ end
+ end
+end