easel_helpers 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,226 @@
1
+ require 'test_helper'
2
+
3
+ class GridHelperTest < EaselHelpers::ViewTestCase
4
+
5
+ context "advanced grid structures" do
6
+
7
+ should "properly assign classes for a simple column layout" do
8
+ template = %(
9
+ <% container do %>
10
+ <% column do %>
11
+ <% column :half, :id => "primary" do %>
12
+ <% column :one_third do %>
13
+ one third of one half of 24 is 4
14
+ <% end %>
15
+ <% column :one_third, :last, prepend_one_third do %>
16
+ one third of one half of 24 is 4 (but prepended 4 as well)
17
+ <% end %>
18
+ <hr/>
19
+ more text
20
+ <% end %>
21
+ <% column :half, :last, :id => "secondary" do %>
22
+ second column
23
+ <% end %>
24
+ <hr/>
25
+ text
26
+ <% end %>
27
+ <% end %>
28
+ )
29
+
30
+ show_view template do
31
+ assert_select ".container", 1
32
+ assert_select ".container.col-24", 0
33
+ assert_select ".col-24", 1
34
+ assert_select ".col-12", 2
35
+ assert_select ".col-12#primary", 1
36
+ assert_select ".col-12#secondary", 1
37
+ assert_select ".col-4", 2
38
+ assert_select ".prepend-4", 1
39
+ assert_select ".col-24.col-last", 0
40
+ assert_select ".col-12.col-last", 1
41
+ assert_select ".col-4.col-last", 1
42
+ assert_select "hr", 2
43
+ end
44
+ end
45
+
46
+ should "properly assign classes for generic helpers" do
47
+ template = %(
48
+ <% column do %>
49
+ <% fieldset :hform, :half do %>
50
+ <% set :one_third do %>text<% end %>
51
+ <% set :two_thirds, :last do %>more text<% end %>
52
+ <% end %>
53
+ <% recordset :half, :last do %>table<% end %>
54
+ <% end %>
55
+ )
56
+
57
+ show_view template do
58
+ assert_select "div.col-24" do
59
+ assert_select "fieldset.hform.col-12" do
60
+ assert_select "div.col-4", "text"
61
+ assert_select "div.col-8.col-last", "more text"
62
+ end
63
+ assert_select "table.col-12.col-last", "table"
64
+ end
65
+ end
66
+ end
67
+
68
+ should "properly assign classes for generic helpers without column wrappers" do
69
+ template = %(
70
+ <% fieldset :hform, :half do %>
71
+ <% set :one_third do %>text<% end %>
72
+ <% set :two_thirds, :last do %>more text<% end %>
73
+ <% column do %>
74
+ <% column :one_third do %>one third<% end %>
75
+ <% column :two_thirds, :last do %>
76
+ <% column :half do %>half<% end %>
77
+ <% column :half, :last do %>last half<% end %>
78
+ <% end %>
79
+ <% end %>
80
+ <% end %>
81
+ <% column :one_third do %>
82
+ <% column :one_fourth do %>two wide<% end %>
83
+ <% column :half do %>four wide<% end %>
84
+ <% column :one_fourth, :last do %>two more wide<% end %>
85
+ <% end %>
86
+ <% recordset :one_sixth, :last do %>table<% end %>
87
+ )
88
+
89
+ show_view template do
90
+ assert_select "fieldset.hform.col-12" do
91
+ assert_select "div.col-4", "text"
92
+ assert_select "div.col-8.col-last", "more text"
93
+
94
+ assert_select "div.col-12.col-last" do
95
+ assert_select "div.col-4", "one third"
96
+ assert_select "div.col-8.col-last" do
97
+ assert_select "div.col-4", "half"
98
+ assert_select "div.col-4.col-last", "last half"
99
+ end
100
+ end
101
+ end
102
+ assert_select "div.col-8" do
103
+ assert_select "div.col-2", "two wide"
104
+ assert_select "div.col-4", "four wide"
105
+ assert_select "div.col-2.col-last", "two more wide"
106
+ end
107
+ assert_select "table.col-4.col-last", "table"
108
+ end
109
+ end
110
+
111
+ should "properly assign classes for a deeply-nested view" do
112
+ template = %(
113
+ <% container :full do %>
114
+ <% column :half do %>
115
+ <% fieldset :hform, :half do %>
116
+ <% set :one_third do %>text<% end %>
117
+ <% set :two_thirds, :last do %>more text<% end %>
118
+ <% end %>
119
+ <% recordset :half, :last do %>table<% end %>
120
+ <% end %>
121
+ <% column :one_third do %>one third!<% end %>
122
+ <% column :one_sixth, :last do %>
123
+ <% fieldset :vform, :full do %>
124
+ <% set do %>text<% end %>
125
+ <% end %>
126
+ <% end %>
127
+ <% end %>
128
+ )
129
+
130
+ show_view template do
131
+ assert_select "div.container.col-24", 1
132
+ assert_select "div.container" do
133
+ assert_select "div.col-12" do
134
+ assert_select "fieldset.hform.col-6" do
135
+ assert_select "div.col-2", "text"
136
+ assert_select "div.col-4.col-last", "more text"
137
+ end
138
+ assert_select "table.col-6.col-last", "table"
139
+ end
140
+
141
+ assert_select "div.col-8", "one third!"
142
+
143
+ assert_select "div.col-4.col-last" do
144
+ assert_select "fieldset.vform.col-4.col-last" do
145
+ assert_select "div", "text"
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+ should "properly assign classes when using Blueprint grid" do
153
+ template = %(
154
+ <% container do %>
155
+ <% column do %>
156
+ <% column :half, :id => "primary" do %>
157
+ <% column :one_third do %>
158
+ one third of one half of 24 is 4
159
+ <% end %>
160
+ <% column :one_third, :last, prepend_one_third do %>
161
+ one third of one half of 24 is 4 (but prepended 4 as well)
162
+ <% end %>
163
+ <hr/>
164
+ more text
165
+ <% end %>
166
+ <% column :half, :last, :id => "secondary" do %>
167
+ second column
168
+ <% end %>
169
+ <hr/>
170
+ text
171
+ <% end %>
172
+ <% end %>
173
+ )
174
+ EaselHelpers::Helpers::GridHelper.blueprint_grid!
175
+ show_view template do
176
+ assert_select ".container", 1
177
+ assert_select ".span-24", 1
178
+ assert_select ".span-12", 2
179
+ assert_select ".span-12#primary", 1
180
+ assert_select ".span-12#secondary", 1
181
+ assert_select ".span-4", 2
182
+ assert_select ".prepend-4", 1
183
+ assert_select ".span-24.last", 0
184
+ assert_select ".span-12.last", 1
185
+ assert_select ".span-4.last", 1
186
+ assert_select "hr", 2
187
+ end
188
+ EaselHelpers::Helpers::GridHelper.easel_grid!
189
+ end
190
+ end
191
+
192
+ context "column" do
193
+
194
+ should "allow assigning options hash without having to define a width" do
195
+ show_view %(
196
+ <% column :id => "my-custom-id", :class => "content" do %>words<% end %>
197
+ ) do
198
+ assert_select "div.col-24.content#my-custom-id", "words"
199
+ end
200
+ end
201
+
202
+ should "allow explicit column assignment" do
203
+ show_view %(
204
+ <% column 6, :sidebar do %>
205
+ <% column :id => "main" do %>main sidebar<% end %>
206
+ <% column :half do %>three<% end %>
207
+ <% column :one_third do %>two<% end %>
208
+ <% column 1, :last do %>one<% end %>
209
+ <% end %>
210
+ ) do
211
+ assert_select "div.col-6.sidebar" do
212
+ assert_select "div.col-6.col-last#main", "main sidebar"
213
+ assert_select "div.col-3", "three"
214
+ assert_select "div.col-2", "two"
215
+ assert_select "div.col-1.col-last", "one"
216
+ end
217
+ end
218
+ end
219
+
220
+ should "allow tag overriding" do
221
+ show_view %(<% column :tag => :section do %>content<% end %>) do
222
+ assert_select "section.col-24:not([tag=section])", "content"
223
+ end
224
+ end
225
+ end
226
+ end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ class JqueryHelperTest < EaselHelpers::ViewTestCase
4
+
5
+ context "document_ready" do
6
+
7
+ setup do
8
+ @whitespace = '\s+?'
9
+ @anything = '(.|\s)+?'
10
+
11
+ @anon_function_start_regex = Regexp.escape "(function($) {"
12
+ @document_ready_start_regex = Regexp.escape "$(document).ready(function() {"
13
+
14
+ @document_ready_end_regex = Regexp.escape "});"
15
+ @anon_function_end_regex = Regexp.escape "})(jQuery);"
16
+ end
17
+
18
+ should "properly build the document ready script tag" do
19
+ show_view %(
20
+ <% document_ready do %>
21
+ alert("hello!");
22
+ <% end %>
23
+ ) do
24
+ assert_select "script[type=?]", "text/javascript", /#{@anon_function_start_regex}#{@whitespace}#{@document_ready_start_regex}#{@whitespace}alert#{@anything}#{@document_ready_end_regex}#{@whitespace}#{@anon_function_end_regex}/
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,44 @@
1
+ require 'test_helper'
2
+
3
+ class LinkHelperTest < EaselHelpers::ViewTestCase
4
+ context "link_button" do
5
+
6
+ should "default with the correct structure" do
7
+ show_view "<%= link_button 'Link Text', '#' %>" do
8
+ assert_select "a.btn[href=#]" do
9
+ assert_select "span", "Link Text"
10
+ end
11
+ end
12
+ end
13
+
14
+ should "allow the same assignment as link_to" do
15
+ show_view "<%= link_button 'Link Text', '#', :class => 'my-button', :id => 'link-text' %>" do
16
+ assert_select "a.btn.my-button#link-text[href=#]" do
17
+ assert_select "span", "Link Text"
18
+ end
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ context "link_to_email" do
25
+ should "default with the correct structure" do
26
+ show_view "<%= link_to_email 'test@example.com' %>" do
27
+ assert_select "a[href=mailto:test@example.com]", "test@example.com"
28
+ end
29
+ end
30
+
31
+ should "allow override of link text as the first argument after email" do
32
+ show_view "<%= link_to_email 'test@example.com', 'Send an email to Test User' %>" do
33
+ assert_select "a[href=mailto:test@example.com]", "Send an email to Test User"
34
+ end
35
+ end
36
+
37
+ should "allow the same assignment as link_to" do
38
+ show_view "<%= link_to_email 'test@example.com', :class => 'test-user-email', :id => 'user_1_email' %>" do
39
+ assert_select "a.test-user-email#user_1_email[href=mailto:test@example.com]", "test@example.com"
40
+ end
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,50 @@
1
+ require 'test_helper'
2
+
3
+ class MessageHelperTest < EaselHelpers::ViewTestCase
4
+
5
+ context "messages" do
6
+
7
+ should "default with the correct structure" do
8
+ show_view %(<%= messages(:structure => "Flash message") %>) do
9
+ assert_select "p.structure.box.single-line", "Flash message"
10
+ end
11
+ end
12
+
13
+ should "display all flash messages present" do
14
+ show_view %(<%= messages(:structure => "Flash message", :error => "Warning message") %>) do
15
+ assert_select "p.structure", "Flash message"
16
+ assert_select "p.error", "Warning message"
17
+ end
18
+ end
19
+
20
+ should "not display a flash if it is blank" do
21
+ show_view %(<%= messages(:structure => "", :error => nil) %>) do
22
+ assert_select "p.structure", false
23
+ assert_select "p.error", false
24
+ end
25
+ end
26
+
27
+ should "filter by the :only option" do
28
+ show_view %(<%= messages({:structure => "Flash message", :error => "Warning message", :notice => "Notice!"}, {:only => [:structure, :error]}) %>) do
29
+ assert_select "p.structure", "Flash message"
30
+ assert_select "p.error", "Warning message"
31
+ assert_select "p.notice", false
32
+ end
33
+ end
34
+
35
+ should "filter by the :except option" do
36
+ show_view %(<%= messages({:structure => "Flash message", :error => "Warning message", :notice => "Notice!"}, {:except => [:structure, :error]}) %>) do
37
+ assert_select "p.structure", false
38
+ assert_select "p.error", false
39
+ assert_select "p.notice", "Notice!"
40
+ end
41
+ end
42
+
43
+ should "raise an error of :only and :except are both passed" do
44
+ assert_raise ArgumentError, /conflict/ do
45
+ show_view %(<%= messages({:structure => "Flash message"}, {:except => [:structure, :error], :only => :structure}) %>)
46
+ end
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1,130 @@
1
+ require "test_helper"
2
+
3
+ class NavigationHelperTest < ActiveSupport::TestCase
4
+ include EaselHelpers::Helpers::NavigationHelper
5
+
6
+ context "tab" do
7
+ should "parse tab options properly" do
8
+ expects(:parse_tab_options).with("test", {:b => 2}).returns({})
9
+ stubs(:link_to)
10
+ stubs(:content_tag)
11
+ stubs(:clean_css_classes)
12
+ tab("test", "/", {:a => 1}, {:b => 2})
13
+ end
14
+
15
+ should "call link_to properly" do
16
+ stubs(:content_tag)
17
+ stubs(:clean_css_classes)
18
+ stubs(:parse_tab_options).returns({})
19
+ expects(:link_to).with("test", "/path", {:a => 1, :b => 2})
20
+ tab("test", "/path", {:a => 1, :b => 2})
21
+ end
22
+
23
+ context "compiling li classes" do
24
+ setup do
25
+ @result_options = { :active => "1",
26
+ :comparison => "2",
27
+ :compare => false,
28
+ :li_classes => "one two three" }
29
+ stubs(:link_to).returns("link_to")
30
+ stubs(:content_tag)
31
+ end
32
+
33
+ should "properly use classes passed in the options" do
34
+ stubs(:parse_tab_options).returns(@result_options)
35
+ expects(:clean_css_classes).with(["one two three", nil])
36
+ tab("test", "/")
37
+ end
38
+
39
+ should "properly compare information" do
40
+ @result_options[:comparison] = "1"
41
+ stubs(:parse_tab_options).returns(@result_options)
42
+ expects(:clean_css_classes).with(["one two three", "active"])
43
+ tab("test", "/")
44
+ end
45
+
46
+ should "properly use compare value" do
47
+ @result_options[:compare] = true
48
+ stubs(:parse_tab_options).returns(@result_options)
49
+ expects(:clean_css_classes).with(["one two three", "active"])
50
+ tab("test", "/")
51
+ end
52
+
53
+ should "not assign class if css_classes is blank" do
54
+ @result_options[:li_classes] = nil
55
+ stubs(:parse_tab_options).returns({})
56
+ stubs(:clean_css_classes).returns("")
57
+ expects(:content_tag).with(:li, "link_to", {})
58
+ tab("test", "/")
59
+ end
60
+ end
61
+
62
+ context "parse_tab_options" do
63
+ context "calculating :active" do
64
+ setup do
65
+ stubs(:controller).returns(stub_everything)
66
+ end
67
+
68
+ should "be based on option" do
69
+ result = send(:parse_tab_options, "name", {:active => "words"})
70
+ assert_equal "words", result[:active]
71
+ end
72
+
73
+ should "be based on name" do
74
+ name = tableized_name = stub
75
+ name.expects(:gsub).with(/\s/, '').returns(tableized_name)
76
+ tableized_name.expects(:tableize).returns("name")
77
+
78
+ result = send(:parse_tab_options, name, {})
79
+ assert_equal "name", result[:active]
80
+ end
81
+ end
82
+
83
+ context "calculating :comparison" do
84
+ should "be based on option" do
85
+ result = send(:parse_tab_options, "name", {:active_compare => "active"})
86
+ assert_equal "active", result[:comparison]
87
+ end
88
+
89
+ should "be based on controller name" do
90
+ controller = stub(:controller_name => "controller-name")
91
+ expects(:controller).returns(controller)
92
+ result = send(:parse_tab_options, "name", {})
93
+ assert_equal "controller-name", result[:comparison]
94
+ end
95
+ end
96
+
97
+ context "calculating :compare" do
98
+ setup do
99
+ stubs(:controller).returns(stub_everything)
100
+ end
101
+
102
+ should "be based on option" do
103
+ result = send(:parse_tab_options, "name", {:compare => true})
104
+ assert_equal true, result[:compare]
105
+ end
106
+
107
+ should "have a default value" do
108
+ result = send(:parse_tab_options, "name")
109
+ assert_equal false, result[:compare]
110
+ end
111
+ end
112
+
113
+ context "calculating :li_classes" do
114
+ setup do
115
+ stubs(:controller).returns(stub_everything)
116
+ end
117
+
118
+ should "be based on option" do
119
+ result = send(:parse_tab_options, "name", {:class => "custom classes"})
120
+ assert_equal "custom classes", result[:li_classes]
121
+ end
122
+
123
+ should "be nil if no option is passed" do
124
+ result = send(:parse_tab_options, "name")
125
+ assert_nil result[:li_classes]
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end