2019-12-21 20:55:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
require 'spec_helper'
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe FormHelper do
|
2022-08-27 11:52:29 +05:30
|
|
|
include Devise::Test::ControllerHelpers
|
|
|
|
|
|
|
|
describe '#dropdown_max_select' do
|
2022-11-25 23:54:43 +05:30
|
|
|
let(:feature_flag) { :limit_reviewer_and_assignee_size }
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
context "with the :limit_reviewer_and_assignee_size feature flag on" do
|
2022-11-25 23:54:43 +05:30
|
|
|
before do
|
|
|
|
stub_feature_flags(feature_flag => true)
|
|
|
|
end
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
it 'correctly returns the max amount of reviewers or assignees to allow' do
|
2022-11-25 23:54:43 +05:30
|
|
|
max = Issuable::MAX_NUMBER_OF_ASSIGNEES_OR_REVIEWERS
|
2022-08-27 11:52:29 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(helper.dropdown_max_select({}, feature_flag))
|
2022-08-27 11:52:29 +05:30
|
|
|
.to eq(max)
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(helper.dropdown_max_select({ 'max-select'.to_sym => 5 }, feature_flag))
|
2022-08-27 11:52:29 +05:30
|
|
|
.to eq(5)
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(helper.dropdown_max_select({ 'max-select'.to_sym => max + 5 }, feature_flag))
|
2022-08-27 11:52:29 +05:30
|
|
|
.to eq(max)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with the :limit_reviewer_and_assignee_size feature flag off" do
|
|
|
|
before do
|
2022-11-25 23:54:43 +05:30
|
|
|
stub_feature_flags(feature_flag => false)
|
2022-08-27 11:52:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'correctly returns the max amount of reviewers or assignees to allow' do
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(helper.dropdown_max_select({}, feature_flag))
|
2022-08-27 11:52:29 +05:30
|
|
|
.to eq(nil)
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(helper.dropdown_max_select({ 'max-select'.to_sym => 5 }, feature_flag))
|
2022-08-27 11:52:29 +05:30
|
|
|
.to eq(5)
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(helper.dropdown_max_select({ 'max-select'.to_sym => 120 }, feature_flag))
|
2022-08-27 11:52:29 +05:30
|
|
|
.to eq(120)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
describe '#assignees_dropdown_options' do
|
|
|
|
let(:merge_request) { build(:merge_request) }
|
|
|
|
|
|
|
|
context "with the :limit_assignees_per_issuable feature flag on" do
|
|
|
|
context "with multiple assignees" do
|
|
|
|
it 'correctly returns the max amount of assignees to allow' do
|
|
|
|
allow(helper).to receive(:merge_request_supports_multiple_assignees?).and_return(true)
|
|
|
|
|
|
|
|
expect(helper.assignees_dropdown_options(:merge_request)[:data][:'max-select'])
|
|
|
|
.to eq(Issuable::MAX_NUMBER_OF_ASSIGNEES_OR_REVIEWERS)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with only 1 assignee" do
|
|
|
|
it 'correctly returns the max amount of assignees to allow' do
|
|
|
|
expect(helper.assignees_dropdown_options(:merge_request)[:data][:'max-select'])
|
|
|
|
.to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with the :limit_assignees_per_issuable feature flag off" do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(limit_assignees_per_issuable: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with multiple assignees" do
|
|
|
|
it 'correctly returns the max amount of assignees to allow' do
|
|
|
|
allow(helper).to receive(:merge_request_supports_multiple_assignees?).and_return(true)
|
|
|
|
|
|
|
|
expect(helper.assignees_dropdown_options(:merge_request)[:data][:'max-select'])
|
|
|
|
.to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with only 1 assignee" do
|
|
|
|
it 'correctly returns the max amount of assignees to allow' do
|
|
|
|
expect(helper.assignees_dropdown_options(:merge_request)[:data][:'max-select'])
|
|
|
|
.to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
describe '#reviewers_dropdown_options' do
|
|
|
|
let(:merge_request) { build(:merge_request) }
|
|
|
|
|
|
|
|
context "with the :limit_reviewer_and_assignee_size feature flag on" do
|
|
|
|
context "with multiple reviewers" do
|
|
|
|
it 'correctly returns the max amount of reviewers or assignees to allow' do
|
|
|
|
allow(helper).to receive(:merge_request_supports_multiple_reviewers?).and_return(true)
|
|
|
|
|
|
|
|
expect(helper.reviewers_dropdown_options(merge_request)[:data][:'max-select'])
|
2022-11-25 23:54:43 +05:30
|
|
|
.to eq(Issuable::MAX_NUMBER_OF_ASSIGNEES_OR_REVIEWERS)
|
2022-08-27 11:52:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with only 1 reviewer" do
|
|
|
|
it 'correctly returns the max amount of reviewers or assignees to allow' do
|
|
|
|
expect(helper.reviewers_dropdown_options(merge_request)[:data][:'max-select'])
|
|
|
|
.to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with the :limit_reviewer_and_assignee_size feature flag off" do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(limit_reviewer_and_assignee_size: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with multiple reviewers" do
|
|
|
|
it 'correctly returns the max amount of reviewers or assignees to allow' do
|
|
|
|
allow(helper).to receive(:merge_request_supports_multiple_reviewers?).and_return(true)
|
|
|
|
|
|
|
|
expect(helper.reviewers_dropdown_options(merge_request)[:data][:'max-select'])
|
|
|
|
.to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with only 1 reviewer" do
|
|
|
|
it 'correctly returns the max amount of reviewers or assignees to allow' do
|
|
|
|
expect(helper.reviewers_dropdown_options(merge_request)[:data][:'max-select'])
|
|
|
|
.to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
describe 'form_errors' do
|
|
|
|
it 'returns nil when model has no errors' do
|
|
|
|
model = double(errors: [])
|
|
|
|
|
|
|
|
expect(helper.form_errors(model)).to be_nil
|
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
it 'renders an appropriately styled alert div' do
|
2016-06-02 11:05:42 +05:30
|
|
|
model = double(errors: errors_stub('Error 1'))
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
expect(helper.form_errors(model))
|
2022-07-23 23:45:48 +05:30
|
|
|
.to include(
|
|
|
|
'<div class="gl-alert gl-mb-5 gl-alert-danger gl-alert-not-dismissible" id="error_explanation" role="alert">'
|
|
|
|
)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains a summary message' do
|
|
|
|
single_error = double(errors: errors_stub('A'))
|
|
|
|
multi_errors = double(errors: errors_stub('A', 'B', 'C'))
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(helper.form_errors(single_error))
|
2022-07-23 23:45:48 +05:30
|
|
|
.to include('The form contains the following error:')
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(helper.form_errors(multi_errors))
|
2022-07-23 23:45:48 +05:30
|
|
|
.to include('The form contains the following errors:')
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders each message' do
|
|
|
|
model = double(errors: errors_stub('Error 1', 'Error 2', 'Error 3'))
|
|
|
|
|
|
|
|
errors = helper.form_errors(model)
|
|
|
|
|
|
|
|
aggregate_failures do
|
|
|
|
expect(errors).to include('<li>Error 1</li>')
|
|
|
|
expect(errors).to include('<li>Error 2</li>')
|
|
|
|
expect(errors).to include('<li>Error 3</li>')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it 'renders messages truncated if requested' do
|
|
|
|
model = double(errors: errors_stub('Error 1', 'Error 2'))
|
|
|
|
model.errors.add(:title, 'is truncated')
|
|
|
|
model.errors.add(:base, 'Error 3')
|
|
|
|
|
|
|
|
expect(model.class).to receive(:human_attribute_name) do |attribute|
|
|
|
|
attribute.to_s.capitalize
|
|
|
|
end
|
|
|
|
|
|
|
|
errors = helper.form_errors(model, truncate: :title)
|
|
|
|
|
|
|
|
aggregate_failures do
|
|
|
|
expect(errors).to include('<li>Error 1</li>')
|
|
|
|
expect(errors).to include('<li>Error 2</li>')
|
|
|
|
expect(errors).to include('<li><span class="str-truncated-100">Title is truncated</span></li>')
|
|
|
|
expect(errors).to include('<li>Error 3</li>')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
it 'renders help page links' do
|
|
|
|
stubbed_errors = ActiveModel::Errors.new(double).tap do |errors|
|
|
|
|
errors.add(:base, 'No text.', help_page_url: 'http://localhost/doc/user/index.html')
|
|
|
|
errors.add(
|
|
|
|
:base,
|
|
|
|
'With text.',
|
|
|
|
help_link_text: 'Documentation page title.',
|
|
|
|
help_page_url: 'http://localhost/doc/administration/index.html'
|
|
|
|
)
|
|
|
|
errors.add(
|
|
|
|
:base,
|
|
|
|
'With HTML text.',
|
|
|
|
help_link_text: '<foo>',
|
|
|
|
help_page_url: 'http://localhost/doc/security/index.html'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
model = double(errors: stubbed_errors)
|
|
|
|
|
|
|
|
errors = helper.form_errors(model)
|
|
|
|
|
|
|
|
aggregate_failures do
|
|
|
|
expect(errors).to include(
|
|
|
|
'<li>No text. <a target="_blank" rel="noopener noreferrer" ' \
|
|
|
|
'href="http://localhost/doc/user/index.html">Learn more.</a></li>'
|
|
|
|
)
|
|
|
|
expect(errors).to include(
|
|
|
|
'<li>With text. <a target="_blank" rel="noopener noreferrer" ' \
|
|
|
|
'href="http://localhost/doc/administration/index.html">Documentation page title.</a></li>'
|
|
|
|
)
|
|
|
|
expect(errors).to include(
|
|
|
|
'<li>With HTML text. <a target="_blank" rel="noopener noreferrer" ' \
|
|
|
|
'href="http://localhost/doc/security/index.html"><foo></a></li>'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def errors_stub(*messages)
|
|
|
|
ActiveModel::Errors.new(double).tap do |errors|
|
|
|
|
messages.each { |msg| errors.add(:base, msg) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|