Skip to content

Commit cb476df

Browse files
committed
Add spec coverage for base filter input form
Covers BaseFilterForm's shared row contract directly via a throwaway concrete subclass: row data attributes, the operator select with its data-no-value branch, label targeting, hidden-when-inactive, the delete button, hiding the operator select for a boolean filter, and the abstract add_operand raising SubclassResponsibilityError.
1 parent 153d9de commit cb476df

1 file changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# frozen_string_literal: true
2+
3+
#-- copyright
4+
# OpenProject is an open source project management software.
5+
# Copyright (C) the OpenProject GmbH
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2013 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See COPYRIGHT and LICENSE files for more details.
29+
#++
30+
31+
require "spec_helper"
32+
33+
RSpec.describe Filters::Inputs::BaseFilterForm, type: :forms do
34+
include ViewComponent::TestHelpers
35+
36+
let!(:date_field) { create(:user_custom_field, field_format: "date") }
37+
let(:query) { UserQuery.new }
38+
let(:filter) { query.available_advanced_filters.find { |af| af.name == :"cf_#{date_field.id}" } }
39+
let(:active) { true }
40+
41+
# Concrete subclass so the abstract base can be rendered: BaseFilterForm#add_operand
42+
# raises SubclassResponsibilityError, so every real subclass supplies an operand.
43+
let(:form_class) do
44+
Class.new(described_class) do
45+
def add_operand(group)
46+
group.text_field(name: operand_name, label: @filter.human_name)
47+
end
48+
end
49+
end
50+
51+
def render_base_form(form_class: self.form_class, filter: self.filter, active: self.active)
52+
render_in_view_context(form_class, filter, active) do |form_class, filter, active|
53+
primer_form_with(url: "/test", method: :post) do |f|
54+
render(form_class.new(f, filter:, additional_attributes: {}, active:))
55+
end
56+
end
57+
page
58+
end
59+
60+
subject(:rendered_form) { render_base_form }
61+
62+
it "renders the row group with filter data attributes" do
63+
expect(rendered_form).to have_element "data-filter--filters-form-target": "filter",
64+
"data-filter-name": filter.name.to_s,
65+
"data-filter-type": filter.type.to_s
66+
end
67+
68+
it "renders the operator select with the filter's operators" do
69+
expect(rendered_form).to have_select "operator_#{filter.name}" do |select|
70+
filter.available_operators.each do |op|
71+
expect(select).to have_selector :option, text: op.human_name
72+
end
73+
end
74+
end
75+
76+
it "marks value-less operators with data-no-value and leaves the rest untouched" do
77+
select = rendered_form.find(:select, "operator_#{filter.name}", visible: :all)
78+
79+
filter.available_operators.each do |op|
80+
option = select.find(:element, :option, value: op.symbol, visible: :all)
81+
82+
if op.requires_value?
83+
expect(option["data-no-value"]).to be_nil
84+
else
85+
expect(option["data-no-value"]).to eq("true")
86+
end
87+
end
88+
end
89+
90+
it "points the label at the operator select for a non-boolean filter" do
91+
expect(rendered_form).to have_element :label,
92+
text: filter.human_name,
93+
for: "operator_#{filter.name}"
94+
end
95+
96+
it "renders a delete button within the filter row" do
97+
expect(rendered_form).to have_element "data-filter--filters-form-target": "filter" do |row|
98+
expect(row).to have_element "tool-tip", text: I18n.t("button_delete")
99+
end
100+
end
101+
102+
context "when inactive" do
103+
let(:active) { false }
104+
105+
it "hides the row" do
106+
expect(rendered_form).to have_element "data-filter--filters-form-target": "filter",
107+
"data-filter-name": filter.name.to_s,
108+
hidden: "hidden",
109+
visible: :all
110+
end
111+
end
112+
113+
context "with a boolean filter" do
114+
let!(:bool_field) { create(:user_custom_field, field_format: "bool") }
115+
let(:filter) { query.available_advanced_filters.find { |af| af.name == :"cf_#{bool_field.id}" } }
116+
117+
it "hides the operator select" do
118+
expect(rendered_form).to have_select "operator_#{filter.name}", visible: :hidden
119+
end
120+
end
121+
122+
context "when add_operand is not overridden" do
123+
let(:form_class) { Class.new(described_class) }
124+
125+
it "raises SubclassResponsibilityError when rendered" do
126+
expect { render_base_form }.to raise_error(SubclassResponsibilityError)
127+
end
128+
end
129+
end

0 commit comments

Comments
 (0)