-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathbase_component_test.rb
More file actions
269 lines (201 loc) · 7.72 KB
/
Copy pathbase_component_test.rb
File metadata and controls
269 lines (201 loc) · 7.72 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# frozen_string_literal: true
require "components/test_helper"
class PrimerBaseComponentTest < Minitest::Test
include Primer::ComponentTestHelpers
def test_renders_data_view_component
render_inline(Primer::BaseComponent.new(tag: :div))
assert_selector("div[data-view-component]")
end
def test_renders_title
render_inline(Primer::BaseComponent.new(tag: :div, title: "title"))
assert_selector("[title='title']")
end
def test_renders_content
render_inline(Primer::BaseComponent.new(tag: :div)) do
"content"
end
assert_text("content")
end
def test_renders_self_closing
img = Primer::BaseComponent.new(tag: :img)
img.expects(:tag)
img.call
end
def test_does_not_render_self_closing
img = Primer::BaseComponent.new(tag: :div)
img.expects(:content_tag)
img.call
end
def test_skips_rendering_primer_class_if_value_is_nil
result = render_inline(Primer::BaseComponent.new(tag: :div, my: nil))
assert result.css("div").first.attribute("class").nil?
end
def test_renders_arbitrary_attributes
render_inline(Primer::BaseComponent.new(tag: :div, itemtype: "http://schema.org/Code"))
assert_selector("[itemtype='http://schema.org/Code']")
end
def test_renders_arbitrary_class_names
render_inline(Primer::BaseComponent.new(tag: :div, classes: "foobar"))
assert_selector(".foobar")
end
def test_renders_arbitrary_blank_attributes
render_inline(Primer::BaseComponent.new(tag: :div, itemscope: true))
assert_selector("[itemscope]")
end
def test_conditionally_renders_arbitrary_blank_attributes
render_inline(Primer::BaseComponent.new(tag: :div, itemscope: false))
refute_selector("[itemscope]")
end
def test_does_not_render_class_attribute_if_none_is_set
render_inline(Primer::BaseComponent.new(tag: :div, title: "title"))
refute_selector("div[class='']")
end
def test_renders_system_argument_class_with_no_whitespace
render_inline(Primer::BaseComponent.new(tag: :div, ml: 3))
# Asserts exact class attribute value with no leading/trailing/extra whitespace
assert_selector("div[class='ml-3 tmp-ml-3']")
end
def test_does_not_render_primer_layout_classes_as_attributes
render_inline(Primer::BaseComponent.new(tag: :div, my: 4))
refute_selector("[my='4']")
end
def test_renders_as_a_link
render_inline(Primer::BaseComponent.new(tag: :a, href: "http://google.com"))
assert_selector("a[href='http://google.com']")
end
def test_rejects_javascript_href_in_non_production
err = assert_raises(ArgumentError) do
render_inline(Primer::BaseComponent.new(tag: :a, href: "javascript:alert(1)"))
end
assert_match(/Rejected dangerous URI scheme/, err.message)
end
def test_neutralizes_javascript_href_in_production
with_raise_on_invalid_options(false) do
render_inline(Primer::BaseComponent.new(tag: :a, href: "javascript:alert(1)")) { "x" }
assert_selector("a")
refute_selector("a[href]")
end
end
def test_neutralizes_vbscript_href_in_production
with_raise_on_invalid_options(false) do
render_inline(Primer::BaseComponent.new(tag: :a, href: "vbscript:msgbox(1)")) { "x" }
refute_selector("a[href]")
end
end
def test_neutralizes_javascript_href_with_whitespace_bypass_attempt
with_raise_on_invalid_options(false) do
render_inline(Primer::BaseComponent.new(tag: :a, href: "\tJaVaScRiPt:alert(1)")) { "x" }
refute_selector("a[href]")
end
end
def test_allows_safe_hrefs
render_inline(Primer::BaseComponent.new(tag: :a, href: "/foo/bar"))
assert_selector("a[href='/foo/bar']")
render_inline(Primer::BaseComponent.new(tag: :a, href: "mailto:hello@example.com"))
assert_selector("a[href='mailto:hello@example.com']")
end
# We were calling tag.send(as), passing in :p ended up calling `p`, aka `puts`
# Due to how Rails uses method_missing in TagHelper. See Slack convo:
# https://github.slack.com/archives/C0HV3F37A/p1556216733019500
def test_renders_as_a_paragraph
render_inline(Primer::BaseComponent.new(tag: :p))
refute_text("[")
end
def test_renders_data_attributes
render_inline(Primer::BaseComponent.new(tag: :div, data: { foo: "bar" }))
assert_selector("[data-foo='bar']")
end
def test_renders_test_selector
render_inline(Primer::BaseComponent.new(tag: :div, test_selector: "bar"))
refute_selector("[test_selector='bar']")
assert_selector("[data-test-selector='bar']")
end
def test_renders_height_attribute
render_inline(Primer::BaseComponent.new(tag: :div, height: 30))
assert_selector("div[height=30]")
end
def test_renders_width_attribute
render_inline(Primer::BaseComponent.new(tag: :div, width: 30))
assert_selector("div[width=30]")
end
def test_does_not_render_height_as_attribute_if_value_is_class
render_inline(Primer::BaseComponent.new(tag: :div, h: :fit))
refute_selector("div[height='fit']")
assert_selector("div.height-fit")
end
def test_does_not_render_width_as_attribute_if_value_is_class
render_inline(Primer::BaseComponent.new(tag: :div, w: :fit))
refute_selector("div[width='fit']")
assert_selector("div.width-fit")
end
def test_renders_content_with_raise_on_invalid_options
with_raise_on_invalid_options(true) do
render_inline(Primer::BaseComponent.new(tag: :div)) do
"content"
end
assert_text("content")
end
end
def test_restricts_allowed_system_arguments
with_raise_on_invalid_options(true) do
error = assert_raises(ArgumentError) do
render_inline(
Primer::BaseComponent.new(
tag: :div,
p: 4,
system_arguments_denylist: {
[:p] => "Perhaps you could consider using :padding options of :foo, :bar, or :baz?"
}
)
)
end
assert_includes(error.message, "Perhaps you could consider using")
end
end
def test_strips_denied_system_arguments
with_raise_on_invalid_options(false) do
render_inline(
Primer::BaseComponent.new(
tag: :div,
p: 4,
system_arguments_denylist: {
[:p] => "Perhaps you could consider using :padding options of :foo, :bar, or :baz?"
}
)
)
end
refute_selector("div[system_arguments_denylist]")
refute_selector(".p-4")
end
def test_raises_when_using_aria_label_for_invalid_tags_and_raise_on_invalid_aria
with_raise_on_invalid_aria(true) do
Primer::Component::INVALID_ARIA_LABEL_TAGS.each do |tag|
err = assert_raises ArgumentError do
render_inline(Primer::BaseComponent.new(tag: tag, "aria-label": "label"))
end
assert_equal "Don't use `aria-label` on `#{tag}` elements. See https://www.tpgi.com/short-note-on-aria-label-aria-labelledby-and-aria-describedby/", err.message
end
end
end
def test_does_not_raise_when_tag_has_role
with_raise_on_invalid_aria(true) do
Primer::Component::INVALID_ARIA_LABEL_TAGS.each do |tag|
render_inline(Primer::BaseComponent.new(tag: tag, role: :role, aria: { label: "label" }))
assert_selector("#{tag}[aria-label='label']")
render_inline(Primer::BaseComponent.new(tag: tag, role: :role, "aria-label": "label"))
assert_selector("#{tag}[aria-label='label']")
end
end
end
def test_renders_aria_label_with_valid_tag
[:a, :img, :button].each do |tag|
render_inline(Primer::BaseComponent.new(tag: tag, aria: { label: "label" }))
assert_selector("#{tag}[aria-label='label']")
render_inline(Primer::BaseComponent.new(tag: tag, "aria-label": "label"))
assert_selector("#{tag}[aria-label='label']")
end
end
def test_status
assert_component_state(Primer::BaseComponent, :beta)
end
end