Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

started to add validation specs

Jonas Jabari83dc6e58acdc
diff --git a/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.html.erb b/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.html.erb
index 14ba839a647..83cb4ae632b 100644
--- a/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.html.erb
+++ b/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.html.erb
@@ -26,7 +26,13 @@
)) do
t("button_cancel")
end
- footer_collection.with_component(Primer::ButtonComponent.new(scheme: :primary, type: :submit)) do
+ footer_collection.with_component(Primer::ButtonComponent.new(
+ scheme: :primary,
+ type: :submit,
+ data: {
+ qa_selector: 'save-project-attributes-button'
+ }
+ )) do
t("button_save")
end
end
diff --git a/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/multi_value_input.rb b/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/multi_value_input.rb
index a9d165138ee..9a97e4040d3 100644
--- a/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/multi_value_input.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/multi_value_input.rb
@@ -38,8 +38,6 @@ class Project::CustomValueForm::Base::Autocomplete::MultiValueInput < Applicatio
def input_attributes
base_input_attributes.merge(
autocomplete_options:,
- invalid: invalid?,
- validation_message:,
wrapper_data_attributes: {
'qa-field-name': qa_field_name
}
diff --git a/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/single_value_input.rb b/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/single_value_input.rb
index 83a1fe61828..f681142c79f 100644
--- a/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/single_value_input.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/single_value_input.rb
@@ -38,8 +38,6 @@ class Project::CustomValueForm::Base::Autocomplete::SingleValueInput < Applicati
def input_attributes
base_input_attributes.merge(
autocomplete_options:,
- invalid: invalid?,
- validation_message:,
wrapper_data_attributes: {
'qa-field-name': qa_field_name
}
diff --git a/modules/overviews/app/forms/project/custom_value_form/base/input.rb b/modules/overviews/app/forms/project/custom_value_form/base/input.rb
index b5b9809ee29..3dc03b52b1e 100644
--- a/modules/overviews/app/forms/project/custom_value_form/base/input.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/base/input.rb
@@ -47,4 +47,12 @@ class Project::CustomValueForm::Base::Input < ApplicationForm
def value
@custom_field_value.value || @custom_field.default_value
end
+
+ def invalid?
+ @custom_field_value.errors.any?
+ end
+
+ def validation_message
+ @custom_field_value.errors.full_messages.join(', ') if invalid?
+ end
end
diff --git a/modules/overviews/app/forms/project/custom_value_form/base/utils.rb b/modules/overviews/app/forms/project/custom_value_form/base/utils.rb
index 16ab8e4b3a6..df2c9fb3989 100644
--- a/modules/overviews/app/forms/project/custom_value_form/base/utils.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/base/utils.rb
@@ -35,7 +35,9 @@ module Project::CustomValueForm::Base::Utils
scope_id_to_model: false,
placeholder: @custom_field.name,
label: @custom_field.name,
- required: @custom_field.is_required?
+ required: @custom_field.is_required?,
+ invalid: invalid?,
+ validation_message:
}
end
diff --git a/spec/features/projects/project_custom_fields/overview_page/dialog_spec.rb b/spec/features/projects/project_custom_fields/overview_page/dialog_spec.rb
index 977d7fddef1..1e54e7685ae 100644
--- a/spec/features/projects/project_custom_fields/overview_page/dialog_spec.rb
+++ b/spec/features/projects/project_custom_fields/overview_page/dialog_spec.rb
@@ -706,6 +706,175 @@ RSpec.describe 'Edit project custom fields on project overview page', :js do
end
end
end
+
+ describe 'with correct validation behaviour' do
+ describe 'with input fields' do
+ let(:section) { section_for_input_fields }
+ let(:dialog) { Components::Projects::ProjectCustomFields::EditDialog.new(project, section) }
+
+ shared_examples 'a custom field input' do
+ it 'shows an error if the value is invalid' do
+ custom_field.update!(is_required: true)
+ custom_field.custom_values.destroy_all
+
+ overview_page.open_edit_dialog_for_section(section)
+
+ dialog.submit(wait_until_done: true)
+
+ field.expect_error(I18n.t('activerecord.errors.messages.blank'))
+ end
+ end
+
+ # boolean CFs can not be validated
+
+ describe 'with string CF' do
+ let(:custom_field) { string_project_custom_field }
+ let(:field) { FormFields::Primerized::InputField.new(custom_field) }
+
+ it_behaves_like 'a custom field input'
+
+ it 'shows an error if the value is too long' do
+ custom_field.update!(max_length: 3)
+
+ overview_page.open_edit_dialog_for_section(section)
+
+ field.fill_in(with: 'Foooo')
+
+ dialog.submit
+
+ field.expect_error(I18n.t('activerecord.errors.messages.too_long', count: 3))
+ end
+
+ it 'shows an error if the value is too short' do
+ custom_field.update!(min_length: 3, max_length: 5)
+
+ overview_page.open_edit_dialog_for_section(section)
+
+ field.fill_in(with: 'Fo')
+
+ dialog.submit
+
+ field.expect_error(I18n.t('activerecord.errors.messages.too_short', count: 3))
+ end
+
+ it 'shows an error if the value does not match the regex' do
+ custom_field.update!(regexp: '^[A-Z]+$')
+
+ overview_page.open_edit_dialog_for_section(section)
+
+ field.fill_in(with: 'foo')
+
+ dialog.submit
+
+ field.expect_error(I18n.t('activerecord.errors.messages.invalid'))
+ end
+ end
+
+ describe 'with integer CF' do
+ let(:custom_field) { integer_project_custom_field }
+ let(:field) { FormFields::Primerized::InputField.new(custom_field) }
+
+ it_behaves_like 'a custom field input'
+
+ it 'shows an error if the value is too long' do
+ custom_field.update!(max_length: 2)
+
+ overview_page.open_edit_dialog_for_section(section)
+
+ field.fill_in(with: '111')
+
+ dialog.submit
+
+ field.expect_error(I18n.t('activerecord.errors.messages.too_long', count: 2))
+ end
+
+ it 'shows an error if the value is too short' do
+ custom_field.update!(min_length: 2, max_length: 5)
+
+ overview_page.open_edit_dialog_for_section(section)
+
+ field.fill_in(with: '1')
+
+ dialog.submit
+
+ field.expect_error(I18n.t('activerecord.errors.messages.too_short', count: 2))
+ end
+ end
+
+ describe 'with float CF' do
+ let(:custom_field) { float_project_custom_field }
+ let(:field) { FormFields::Primerized::InputField.new(custom_field) }
+
+ it_behaves_like 'a custom field input'
+
+ it 'shows an error if the value is too long' do
+ custom_field.update!(max_length: 4)
+
+ overview_page.open_edit_dialog_for_section(section)
+
+ field.fill_in(with: '1111.1')
+
+ dialog.submit
+
+ field.expect_error(I18n.t('activerecord.errors.messages.too_long', count: 4))
+ end
+
+ it 'shows an error if the value is too short' do
+ custom_field.update!(min_length: 4, max_length: 5)
+
+ overview_page.open_edit_dialog_for_section(section)
+
+ field.fill_in(with: '1.1')
+
+ dialog.submit
+
+ field.expect_error(I18n.t('activerecord.errors.messages.too_short', count: 4))
+ end
+ end
+
+ describe 'with date CF' do
+ let(:custom_field) { date_project_custom_field }
+ let(:field) { FormFields::Primerized::InputField.new(custom_field) }
+
+ it_behaves_like 'a custom field input'
+ end
+
+ describe 'with text CF' do
+ let(:custom_field) { text_project_custom_field }
+ let(:field) { FormFields::Primerized::InputField.new(custom_field) }
+
+ it_behaves_like 'a custom field input'
+
+ it 'shows an error if the value is too long' do
+ custom_field.update!(max_length: 3)
+
+ overview_page.open_edit_dialog_for_section(section)
+
+ field.fill_in(with: 'Foo')
+
+ dialog.submit
+
+ field.expect_error(I18n.t('activerecord.errors.messages.too_long', count: 3))
+ end
+
+ it 'shows an error if the value is too short' do
+ custom_field.update!(min_length: 3, max_length: 5)
+
+ overview_page.open_edit_dialog_for_section(section)
+
+ field.fill_in(with: 'Fo')
+
+ dialog.submit
+
+ field.expect_error(I18n.t('activerecord.errors.messages.too_short', count: 3))
+ end
+ end
+ end
+ end
+
+ describe 'with correct updating behaviour' do
+ # TODO
+ end
end
end
end
diff --git a/spec/support/components/projects/project_custom_fields/edit_dialog.rb b/spec/support/components/projects/project_custom_fields/edit_dialog.rb
index 3b47651eea4..297a69f1966 100644
--- a/spec/support/components/projects/project_custom_fields/edit_dialog.rb
+++ b/spec/support/components/projects/project_custom_fields/edit_dialog.rb
@@ -75,6 +75,12 @@ module Components
end
end
+ def submit
+ within(dialog_css_selector) do
+ page.find("[data-qa-selector='save-project-attributes-button']").click
+ end
+ end
+
def expect_open
expect(page).to have_css(dialog_css_selector)
end
diff --git a/spec/support/form_fields/primerized/input_field.rb b/spec/support/form_fields/primerized/input_field.rb
new file mode 100644
index 00000000000..c5dd77f3b77
--- /dev/null
+++ b/spec/support/form_fields/primerized/input_field.rb
@@ -0,0 +1,28 @@
+require_relative 'form_field'
+
+module FormFields
+ module Primerized
+ class InputField < FormField
+ delegate :fill_in, to: :input_element
+
+ def field_container
+ page.find(selector).first(:xpath, ".//..").first(:xpath, ".//..")
+ end
+
+ def input_element
+ field_container
+ end
+
+ def send_keys(*)
+ input_element.send_keys(*)
+ end
+
+ # expectations
+
+ def expect_error(string = nil)
+ expect(page).to have_css("#{selector}[invalid='true']")
+ expect(field_container).to have_content(string) if string
+ end
+ end
+ end
+end