-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_text_field.rb
204 lines (155 loc) · 3.84 KB
/
test_text_field.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# frozen_string_literal: true
require "test_helper"
require "view_component/test_case"
require "active_model"
require "input_group/text_field"
class Game
include ActiveModel::API
attr_accessor :title
end
class Felt::InputGroup::TextFieldTest < ViewComponent::TestCase
include ViewComponent::TestHelpers
def test_renders_a_label
model = Game.new
form = build_form(model)
do_render(form)
assert_selector("label[for=game_title]", text: "Title")
end
def test_renders_provided_label
model = Game.new
form = build_form(model)
do_render(form, label: "This label")
assert_selector("label[for=game_title]", text: "This label")
end
def test_renders_an_input_field
model = Game.new
form = build_form(model)
do_render(form)
assert_selector("input[type='text'][name='game[title]']")
end
def test_uses_the_value_from_the_object
model = Game.new
model.title = "Dead Cells"
form = build_form(model)
do_render(form)
assert(page.has_field?("game[title]", with: "Dead Cells"))
end
def test_uses_configured_classes_for_input
model = Game.new
form = build_form(model)
Felt.configure do |config|
config.classes = {
input_group: {
input: {
default: "input-field"
}
}
}
end
do_render(form)
assert_selector("input[type='text'].input-field")
end
def test_renders_help_from_arguments
model = Game.new
form = build_form(model)
with_translations({
forms: {
game: {
title: {
help: "Help from translations"
}
}
}
}) do
do_render(form, help: "Help is shown below the input")
end
assert_text("Help is shown below the input")
end
def test_renders_help_from_translations
model = Game.new
form = build_form(model)
with_translations({
forms: {
game: {
title: {
help: "Help from translations"
}
}
}
}) do
do_render(form)
end
assert_text("Help from translations")
end
def test_renders_hint_from_translations
model = Game.new
model.title = "Dead Cells"
form = build_form(model)
with_translations({
forms: {
game: {
title: {
hint: "Hint from translations"
}
}
}
}) do
do_render(form)
end
assert_text("Hint from translations")
end
def test_renders_errors_if_any
model = Game.new
model.errors.add(:title, :invalid)
form = build_form(model)
do_render(form)
assert_text("Title is invalid")
end
def test_uses_error_classes_if_model_is_invalid
model = Game.new
model.errors.add(:title, :invalid)
form = build_form(model)
Felt.configure do |config|
config.classes = {
input_group: {
input: {
invalid: "error-class"
}
}
}
end
do_render(form)
assert_css("input[type=text].error-class")
end
def test_options_can_be_added_to_wrapping_element
model = Game.new
form = build_form(model)
do_render(form, id: "game-card")
assert_css("div#game-card")
end
def test_known_options_are_not_added_to_wrapping_element
model = Game.new
form = build_form(model)
do_render(form)
refute_css("div[form]")
refute_css("div[attribute]")
end
def test_uses_configured_classes_for_wrapping_element
model = Game.new
form = build_form(model)
Felt.configure do |config|
config.classes = {
input_group: {
text_field: "my-input-group"
}
}
end
do_render(form)
assert_css("div.my-input-group")
end
private
def do_render(form, **options)
component = Felt::InputGroup::TextField.new(form: form, attribute: :title, **options)
render_inline(component).to_html
end
end