1
+ require 'test_helper'
2
+ require 'rails/dom/testing/assertions/tag_assertions'
3
+
4
+ HTML_TEST_OUTPUT = <<HTML
5
+ < html >
6
+ < body >
7
+ < a href ="/ "> < img src ="/images/button.png " /> </ a >
8
+ < div id ="foo ">
9
+ < ul >
10
+ < li class ="item "> hello</ li >
11
+ < li class ="item "> goodbye</ li >
12
+ </ ul >
13
+ </ div >
14
+ < div id ="bar ">
15
+ < form action ="/somewhere ">
16
+ Name: < input type ="text " name ="person[name] " id ="person_name " />
17
+ </ form >
18
+ </ div >
19
+ </ body >
20
+ </ html >
21
+ HTML
22
+
23
+ class AssertTagTest < ActiveSupport ::TestCase
24
+ include Rails ::Dom ::Testing ::Assertions ::TagAssertions
25
+
26
+ class FakeResponse
27
+ attr_accessor :content_type , :body
28
+
29
+ def initialize ( content_type , body )
30
+ @content_type , @body = content_type , body
31
+ end
32
+ end
33
+
34
+ setup do
35
+ @response = FakeResponse . new 'html' , HTML_TEST_OUTPUT
36
+ end
37
+
38
+ def test_assert_tag_tag
39
+ # there is a 'form' tag
40
+ assert_tag tag : 'form'
41
+ # there is not an 'hr' tag
42
+ assert_no_tag tag : 'hr'
43
+ end
44
+
45
+ def test_assert_tag_attributes
46
+ # there is a tag with an 'id' of 'bar'
47
+ assert_tag attributes : { id : "bar" }
48
+ # there is no tag with a 'name' of 'baz'
49
+ assert_no_tag attributes : { name : "baz" }
50
+ end
51
+
52
+ def test_assert_tag_parent
53
+ # there is a tag with a parent 'form' tag
54
+ assert_tag parent : { tag : "form" }
55
+ # there is no tag with a parent of 'input'
56
+ assert_no_tag parent : { tag : "input" }
57
+ end
58
+
59
+ def test_assert_tag_child
60
+ # there is a tag with a child 'input' tag
61
+ assert_tag child : { tag : "input" }
62
+ # there is no tag with a child 'strong' tag
63
+ assert_no_tag child : { tag : "strong" }
64
+ end
65
+
66
+ def test_assert_tag_ancestor
67
+ # there is a 'li' tag with an ancestor having an id of 'foo'
68
+ assert_tag ancestor : { attributes : { id : "foo" } } , tag : "li"
69
+ # there is no tag of any kind with an ancestor having an href matching 'foo'
70
+ assert_no_tag ancestor : { attributes : { href : /foo/ } }
71
+ end
72
+
73
+ def test_assert_tag_descendant
74
+ # there is a tag with a descendant 'li' tag
75
+ assert_tag descendant : { tag : "li" }
76
+ # there is no tag with a descendant 'html' tag
77
+ assert_no_tag descendant : { tag : "html" }
78
+ end
79
+
80
+ def test_assert_tag_sibling
81
+ # there is a tag with a sibling of class 'item'
82
+ assert_tag sibling : { attributes : { class : "item" } }
83
+ # there is no tag with a sibling 'ul' tag
84
+ assert_no_tag sibling : { tag : "ul" }
85
+ end
86
+
87
+ def test_assert_tag_after
88
+ # there is a tag following a sibling 'div' tag
89
+ assert_tag after : { tag : "div" }
90
+ # there is no tag following a sibling tag with id 'bar'
91
+ assert_no_tag after : { attributes : { id : "bar" } }
92
+ end
93
+
94
+ def test_assert_tag_before
95
+ # there is a tag preceding a tag with id 'bar'
96
+ assert_tag before : { attributes : { id : "bar" } }
97
+ # there is no tag preceding a 'form' tag
98
+ assert_no_tag before : { tag : "form" }
99
+ end
100
+
101
+ def test_assert_tag_children_count
102
+ # there is a tag with 2 children
103
+ assert_tag children : { count : 2 }
104
+ # in particular, there is a <ul> tag with two children (a nameless pair of <li>s)
105
+ assert_tag tag : 'ul' , children : { count : 2 }
106
+ # there is no tag with 4 children
107
+ assert_no_tag children : { count : 4 }
108
+ end
109
+
110
+ def test_assert_tag_children_less_than
111
+ # there is a tag with less than 5 children
112
+ assert_tag children : { less_than : 5 }
113
+ # there is no 'ul' tag with less than 2 children
114
+ assert_no_tag children : { less_than : 2 } , tag : "ul"
115
+ end
116
+
117
+ def test_assert_tag_children_greater_than
118
+ # there is a 'body' tag with more than 1 children
119
+ assert_tag children : { greater_than : 1 } , tag : "body"
120
+ # there is no tag with more than 10 children
121
+ assert_no_tag children : { greater_than : 10 }
122
+ end
123
+
124
+ def test_assert_tag_children_only
125
+ # there is a tag containing only one child with an id of 'foo'
126
+ assert_tag children : { count : 1 ,
127
+ only : { attributes : { id : "foo" } } }
128
+ # there is no tag containing only one 'li' child
129
+ assert_no_tag children : { count : 1 , only : { tag : "li" } }
130
+ end
131
+
132
+ def test_assert_tag_content
133
+ # the output contains the string "Name"
134
+ assert_tag content : /Name/
135
+ # the output does not contain the string "test"
136
+ assert_no_tag content : /test/
137
+ end
138
+
139
+ def test_assert_tag_multiple
140
+ # there is a 'div', id='bar', with an immediate child whose 'action'
141
+ # attribute matches the regexp /somewhere/.
142
+ assert_tag tag : "div" , attributes : { id : "bar" } ,
143
+ child : { attributes : { action : /somewhere/ } }
144
+
145
+ # there is no 'div', id='foo', with a 'ul' child with more than
146
+ # 2 "li" children.
147
+ assert_no_tag tag : "div" , attributes : { id : "foo" } ,
148
+ child : { tag : "ul" ,
149
+ children : { greater_than : 2 , only : { tag : "li" } } }
150
+ end
151
+
152
+ def test_assert_tag_children_without_content
153
+ # there is a form tag with an 'input' child which is a self closing tag
154
+ assert_tag tag : "form" ,
155
+ children : { count : 1 ,
156
+ only : { tag : "input" } }
157
+
158
+ # the body tag has an 'a' child which in turn has an 'img' child
159
+ assert_tag tag : "body" ,
160
+ children : { count : 1 ,
161
+ only : { tag : "a" ,
162
+ children : { count : 1 ,
163
+ only : { tag : "img" } } } }
164
+ end
165
+
166
+ def test_assert_tag_attribute_matching
167
+ @response . body = '<input type="text" name="my_name">'
168
+ assert_tag tag : 'input' ,
169
+ attributes : { name : /my/ , type : 'text' }
170
+ assert_no_tag tag : 'input' ,
171
+ attributes : { name : 'my' , type : 'text' }
172
+ assert_no_tag tag : 'input' ,
173
+ attributes : { name : /^my$/ , type : 'text' }
174
+ end
175
+
176
+ def test_assert_tag_content_matching
177
+ @response . body = "<p>hello world</p>"
178
+ assert_tag tag : "p" , content : "hello world"
179
+ assert_tag tag : "p" , content : /hello/
180
+ assert_no_tag tag : "p" , content : "hello"
181
+ end
182
+ end
0 commit comments