2019-09-30 08:06:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-29 03:56:52 -04:00
|
|
|
require 'spec_helper'
|
2016-04-04 21:25:38 -04:00
|
|
|
|
|
|
|
describe FormHelper do
|
|
|
|
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
|
|
|
|
|
|
|
|
it 'renders an alert div' do
|
|
|
|
model = double(errors: errors_stub('Error 1'))
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(helper.form_errors(model))
|
|
|
|
.to include('<div class="alert alert-danger" id="error_explanation">')
|
2016-04-04 21:25:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains a summary message' do
|
|
|
|
single_error = double(errors: errors_stub('A'))
|
|
|
|
multi_errors = double(errors: errors_stub('A', 'B', 'C'))
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(helper.form_errors(single_error))
|
|
|
|
.to include('<h4>The form contains the following error:')
|
|
|
|
expect(helper.form_errors(multi_errors))
|
|
|
|
.to include('<h4>The form contains the following errors:')
|
2016-04-04 21:25:38 -04:00
|
|
|
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-03-02 13:07:42 -05:00
|
|
|
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
|
|
|
|
|
2016-04-04 21:25:38 -04:00
|
|
|
def errors_stub(*messages)
|
|
|
|
ActiveModel::Errors.new(double).tap do |errors|
|
|
|
|
messages.each { |msg| errors.add(:base, msg) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|