main
1# frozen_string_literal: true
2
3require 'rails_helper'
4
5RSpec.describe ApplicationHelper do
6 describe "#alert_class_for" do
7 specify { expect(helper.alert_class_for(:notice)).to eql('is-success') }
8 specify { expect(helper.alert_class_for('notice')).to eql('is-success') }
9 specify { expect(helper.alert_class_for(:warning)).to eql('is-warning') }
10 specify { expect(helper.alert_class_for(:error)).to eql('is-danger') }
11 specify { expect(helper.alert_class_for(:info)).to eql('is-info') }
12 end
13
14 describe "#flash_error_messages_for" do
15 context "when the item is an array of strings" do
16 it 'returns the array of strings' do
17 expect(helper.flash_error_messages_for(['error'])).to match_array(['error'])
18 end
19
20 context "when the item is a single string" do
21 it 'returns an array of strings when' do
22 expect(helper.flash_error_messages_for('error')).to match_array(['error'])
23 end
24 end
25
26 context "when the item is an instance of ActiveModel::Errors" do
27 let(:user_class) do
28 Class.new do
29 extend ActiveModel::Naming
30 attr_reader :email, :password
31
32 def read_attribute_for_validation(attr)
33 send(attr)
34 end
35
36 def self.human_attribute_name(attr, _options = {})
37 attr.to_s.titleize
38 end
39
40 def self.lookup_ancestors
41 [self]
42 end
43 end
44 end
45 let(:user) { user_class.new }
46 let(:errors) do
47 errors = ActiveModel::Errors.new(user)
48 errors.add(:email, 'has already been taken.')
49 errors.add(:password, 'must contain at least one upper case character.')
50 errors.add(:password, 'must contain at least one numeric character.')
51 errors
52 end
53
54 specify do
55 expect(helper.flash_error_messages_for(errors)).to match_array([
56 'Email has already been taken.',
57 'Password must contain at least one upper case character. Password must contain at least one numeric character.',
58 ])
59 end
60 end
61 end
62 end
63end