/
error_test.rb
107 lines (88 loc) · 3.46 KB
/
error_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
require 'test_helper'
# Tests for f.error and f.full_error
class ErrorTest < ActionView::TestCase
def with_error_for(object, *args)
with_concat_form_for(object) do |f|
f.error(*args)
end
end
def with_full_error_for(object, *args)
with_concat_form_for(object) do |f|
f.full_error(*args)
end
end
test 'error should not generate content for attribute without errors' do
with_error_for @user, :active
assert_no_select 'span.error'
end
test 'error should not generate messages when object is not present' do
with_error_for :project, :name
assert_no_select 'span.error'
end
test "error should not generate messages when object doesn't respond to errors method" do
@user.instance_eval { undef errors }
with_error_for @user, :name
assert_no_select 'span.error'
end
test 'error should generate messages for attribute with single error' do
with_error_for @user, :name
assert_select 'span.error', "can't be blank"
end
test 'error should generate messages for attribute with one error when using first' do
swap SimpleForm, :error_method => :first do
with_error_for @user, :age
assert_select 'span.error', 'is not a number'
end
end
test 'error should generate messages for attribute with several errors when using to_sentence' do
swap SimpleForm, :error_method => :to_sentence do
with_error_for @user, :age
assert_select 'span.error', 'is not a number and must be greater than 18'
end
end
test 'error should be able to pass html options' do
with_error_for @user, :name, :id => 'error', :class => 'yay'
assert_select 'span#error.error.yay'
end
test 'error should find errors on attribute and association' do
with_error_for @user, :company_id, :as => :select,
:error_method => :to_sentence, :reflection => Association.new(Company, :company, {})
assert_select 'span.error', 'must be valid and company must be present'
end
test 'error should generate an error tag with a clean HTML' do
with_error_for @user, :name
assert_no_select 'span.error[error_html]'
end
test 'error should generate an error tag with a clean HTML when errors options are present' do
with_error_for @user, :name, :error_tag => :p, :error_prefix => 'Name', :error_method => :first
assert_no_select 'p.error[error_html]'
assert_no_select 'p.error[error_tag]'
assert_no_select 'p.error[error_prefix]'
assert_no_select 'p.error[error_method]'
end
test 'error should generate an error message with raw HTML tags' do
with_error_for @user, :name, :error_prefix => '<b>Name</b>'
assert_select 'span.error', "Name can't be blank"
assert_select 'span.error b', "Name"
end
# FULL ERRORS
test 'full error should generate an full error tag for the attribute' do
with_full_error_for @user, :name
assert_select 'span.error', "Super User Name! can't be blank"
end
test 'full error should generate an full error tag with a clean HTML' do
with_full_error_for @user, :name
assert_no_select 'span.error[error_html]'
end
test 'full error should allow passing options to full error tag' do
with_full_error_for @user, :name, :id => 'name_error', :error_prefix => "Your name"
assert_select 'span.error#name_error', "Your name can't be blank"
end
# CUSTOM WRAPPERS
test 'error with custom wrappers works' do
swap_wrapper do
with_error_for @user, :name
assert_select 'span.omg_error', "can't be blank"
end
end
end