table_helper 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,8 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  class CollectionTableByDefaultTest < Test::Unit::TestCase
4
4
  def setup
5
- @table = TableHelper::CollectionTable.new([])
5
+ @collection = []
6
+ @table = TableHelper::CollectionTable.new(@collection)
6
7
  end
7
8
 
8
9
  def test_cellspacing_should_be_zero
@@ -13,242 +14,215 @@ class CollectionTableByDefaultTest < Test::Unit::TestCase
13
14
  assert_equal '0', @table[:cellpadding]
14
15
  end
15
16
 
16
- def test_should_include_header_and_footer
17
- html = @table.build do |header, body|
18
- assert_instance_of TableHelper::Header, header
19
- assert_instance_of TableHelper::Body, body
20
- end
17
+ def test_should_have_a_css_class
18
+ assert_equal 'ui-collection', @table[:class]
19
+ end
20
+
21
+ def test_should_use_custom_collection_css_class_if_specified
22
+ original_collection_class = TableHelper::CollectionTable.collection_class
23
+ TableHelper::CollectionTable.collection_class = 'ui-records'
21
24
 
22
- expected = <<-end_str
23
- <thead style="display: none;">
24
- <tr></tr>
25
- </thead>
26
- <tbody>
27
- <tr class="no_content">
28
- <td>No matches found.</td>
29
- </tr>
30
- </tbody>
31
- end_str
32
- assert_html_equal expected, html
25
+ table = TableHelper::CollectionTable.new(@collection)
26
+ assert_equal 'ui-records', table[:class]
27
+ ensure
28
+ TableHelper::CollectionTable.collection_class = original_collection_class
29
+ end
30
+
31
+ def test_should_not_have_a_klass
32
+ assert_nil @table.klass
33
+ end
34
+
35
+ def test_should_not_have_an_object_name
36
+ assert_nil @table.object_name
37
+ end
38
+
39
+ def test_should_have_a_collection
40
+ assert_equal @collection, @table.collection
41
+ end
42
+
43
+ def test_should_have_a_header
44
+ assert_not_nil @table.header
45
+ assert_instance_of TableHelper::Header, @table.header
46
+ end
47
+
48
+ def test_should_have_a_body
49
+ assert_not_nil @table.rows
50
+ assert_instance_of TableHelper::Body, @table.rows
51
+ end
52
+
53
+ def test_should_have_a_footer
54
+ assert_not_nil @table.footer
55
+ assert_instance_of TableHelper::Footer, @table.footer
56
+ end
57
+
58
+ def test_should_be_empty
59
+ assert @table.empty?
33
60
  end
34
61
  end
35
62
 
36
63
  class CollectionTableTest < Test::Unit::TestCase
37
- class Note
38
- def self.column_names
39
- ['title', 'author_name']
40
- end
41
-
42
- attr_accessor :title
64
+ def test_should_accept_block_with_self_as_argument
65
+ args = nil
66
+ table = TableHelper::CollectionTable.new([]) {|*args|}
43
67
 
44
- def initialize(title)
45
- @title = title
46
- end
68
+ assert_equal [table], args
47
69
  end
48
-
49
- class Post < Note
50
- def self.column_names
51
- ['title']
52
- end
70
+ end
71
+
72
+ class CollectionTableWithClassDetectionTest < Test::Unit::TestCase
73
+ class Post
53
74
  end
54
75
 
55
76
  class Reflection
56
77
  def klass
57
- Note
78
+ Post
58
79
  end
59
80
  end
60
81
 
61
- class NoteCollection < Array
82
+ class PostCollection < Array
62
83
  def proxy_reflection
63
84
  Reflection.new
64
85
  end
65
86
  end
66
87
 
88
+ def test_should_not_detect_class_if_collection_is_empty_vanilla_array
89
+ table = TableHelper::CollectionTable.new([])
90
+ assert_nil table.klass
91
+ end
92
+
93
+ def test_should_detect_class_if_collection_has_objects
94
+ table = TableHelper::CollectionTable.new([Post.new])
95
+ assert_equal Post, table.klass
96
+ end
97
+
98
+ def test_should_detect_class_if_collection_is_model_proxy
99
+ table = TableHelper::CollectionTable.new(PostCollection.new)
100
+ assert_equal Post, table.klass
101
+ end
102
+ end
103
+
104
+ class CollectionTableWithClassTest < Test::Unit::TestCase
105
+ class Post
106
+ end
107
+
67
108
  def setup
68
- @collection = [
69
- Post.new('first'),
70
- Post.new('second'),
71
- Post.new('last')
72
- ]
109
+ @table = TableHelper::CollectionTable.new([], Post)
73
110
  end
74
111
 
75
- def test_should_raise_exception_if_invalid_option_specified
76
- assert_raise(ArgumentError) {TableHelper::CollectionTable.new([], :invalid => true)}
112
+ def test_should_have_klass
113
+ assert_equal Post, @table.klass
77
114
  end
78
115
 
79
- def test_should_only_include_body_if_no_header
80
- table = TableHelper::CollectionTable.new(@collection, :header => false)
81
- html = table.build do |body|
82
- assert_instance_of TableHelper::Body, body
83
- end
84
-
85
- expected = <<-end_str
86
- <tbody>
87
- <tr class="row">
88
- <td class="title">first</td>
89
- </tr>
90
- <tr class="row">
91
- <td class="title">second</td>
92
- </tr>
93
- <tr class="row">
94
- <td class="title">last</td>
95
- </tr>
96
- </tbody>
97
- end_str
98
- assert_html_equal expected, html
116
+ def test_should_have_object_name
117
+ assert_equal 'post', @table.object_name
99
118
  end
100
119
 
101
- def test_should_include_body_and_footer_if_no_header_and_footer
102
- table = TableHelper::CollectionTable.new(@collection, :header => false, :footer => true)
103
- html = table.build do |body, footer|
104
- assert_instance_of TableHelper::Body, body
105
- assert_instance_of TableHelper::Footer, footer
106
- end
107
-
120
+ def test_should_include_pluralized_object_name_in_css_class
121
+ assert_equal 'posts ui-collection', @table[:class]
122
+ end
123
+ end
124
+
125
+ class CollectionTableWithEmptyCollectionTest < Test::Unit::TestCase
126
+ def setup
127
+ @table = TableHelper::CollectionTable.new([])
128
+ end
129
+
130
+ def test_should_be_empty
131
+ assert @table.empty?
132
+ end
133
+
134
+ def test_should_build_html
108
135
  expected = <<-end_str
109
- <tbody>
110
- <tr class="row">
111
- <td class="title">first</td>
112
- </tr>
113
- <tr class="row">
114
- <td class="title">second</td>
115
- </tr>
116
- <tr class="row">
117
- <td class="title">last</td>
118
- </tr>
119
- </tbody>
120
- <tfoot>
121
- <tr>
122
- </tr>
123
- </tfoot>
136
+ <table cellpadding="0" cellspacing="0" class="ui-collection">
137
+ <tbody>
138
+ <tr class="ui-collection-empty"><td>No matches found.</td></tr>
139
+ </tbody>
140
+ </table>
124
141
  end_str
125
- assert_html_equal expected, html
142
+ assert_html_equal expected, @table.html
143
+ end
144
+ end
145
+
146
+ class CollectionTableWithObjectsTest < Test::Unit::TestCase
147
+ def setup
148
+ @table = TableHelper::CollectionTable.new([Object.new])
126
149
  end
127
150
 
128
- def test_should_include_header_body_and_footer_if_footer
129
- table = TableHelper::CollectionTable.new(@collection, :footer => true)
130
- html = table.build do |header, body, footer|
131
- assert_instance_of TableHelper::Header, header
132
- assert_instance_of TableHelper::Body, body
133
- assert_instance_of TableHelper::Footer, footer
134
- end
135
-
151
+ def test_should_not_be_empty
152
+ assert !@table.empty?
153
+ end
154
+ end
155
+
156
+ class CollectionTableHeaderTest < Test::Unit::TestCase
157
+ def setup
158
+ @table = TableHelper::CollectionTable.new([Object.new])
159
+ end
160
+
161
+ def test_should_not_include_in_html_if_none_specified
136
162
  expected = <<-end_str
137
- <thead>
138
- <tr>
139
- <th class="title" scope="col">Title</th>
140
- </tr>
141
- </thead>
142
- <tbody>
143
- <tr class="row">
144
- <td class="title">first</td>
145
- </tr>
146
- <tr class="row">
147
- <td class="title">second</td>
148
- </tr>
149
- <tr class="row">
150
- <td class="title">last</td>
151
- </tr>
152
- </tbody>
153
- <tfoot>
154
- <tr>
155
- </tr>
156
- </tfoot>
163
+ <table cellpadding="0" cellspacing="0" class="objects ui-collection">
164
+ <tbody>
165
+ <tr class="object ui-collection-result"></tr>
166
+ </tbody>
167
+ </table>
157
168
  end_str
158
- assert_html_equal expected, html
169
+ assert_html_equal expected, @table.html
159
170
  end
160
171
 
161
- def test_should_use_custom_class_to_generate_columns
162
- table = TableHelper::CollectionTable.new(@collection, :class => Note)
163
- html = table.build
172
+ def test_should_include_in_html_if_specified
173
+ @table.header :name, :title
164
174
 
165
175
  expected = <<-end_str
166
- <thead>
167
- <tr>
168
- <th class="title" scope="col">Title</th>
169
- <th class="author_name" scope="col">Author Name</th>
170
- </tr>
171
- </thead>
172
- <tbody>
173
- <tr class="row">
174
- <td class="title">first</td>
175
- <td class="author_name empty"></td>
176
- </tr>
177
- <tr class="row">
178
- <td class="title">second</td>
179
- <td class="author_name empty"></td>
180
- </tr>
181
- <tr class="row">
182
- <td class="title">last</td>
183
- <td class="author_name empty"></td>
184
- </tr>
185
- </tbody>
176
+ <table cellpadding="0" cellspacing="0" class="objects ui-collection">
177
+ <thead>
178
+ <tr>
179
+ <th class="object-name" scope="col">Name</th>
180
+ <th class="object-title" scope="col">Title</th>
181
+ </tr>
182
+ </thead>
183
+ <tbody>
184
+ <tr class="object ui-collection-result">
185
+ <td class="object-name ui-state-empty"></td>
186
+ <td class="object-title ui-state-empty"></td>
187
+ </tr>
188
+ </tbody>
189
+ </table>
186
190
  end_str
187
- assert_html_equal expected, html
191
+ assert_html_equal expected, @table.html
192
+ end
193
+ end
194
+
195
+ class CollectionTableFooterTest < Test::Unit::TestCase
196
+ def setup
197
+ @table = TableHelper::CollectionTable.new([Object.new])
188
198
  end
189
199
 
190
- def test_should_use_class_from_first_element_to_generate_columns
191
- @collection.insert(0, Note.new('zeroth'))
192
- table = TableHelper::CollectionTable.new(@collection)
193
- html = table.build
194
-
200
+ def test_should_not_include_in_html_if_none_specified
195
201
  expected = <<-end_str
196
- <thead>
197
- <tr>
198
- <th class="title" scope="col">Title</th>
199
- <th class="author_name" scope="col">Author Name</th>
200
- </tr>
201
- </thead>
202
- <tbody>
203
- <tr class="row">
204
- <td class="title">zeroth</td>
205
- <td class="author_name empty"></td>
206
- </tr>
207
- <tr class="row">
208
- <td class="title">first</td>
209
- <td class="author_name empty"></td>
210
- </tr>
211
- <tr class="row">
212
- <td class="title">second</td>
213
- <td class="author_name empty"></td>
214
- </tr>
215
- <tr class="row">
216
- <td class="title">last</td>
217
- <td class="author_name empty"></td>
218
- </tr>
219
- </tbody>
202
+ <table cellpadding="0" cellspacing="0" class="objects ui-collection">
203
+ <tbody>
204
+ <tr class="object ui-collection-result"></tr>
205
+ </tbody>
206
+ </table>
220
207
  end_str
221
- assert_html_equal expected, html
208
+ assert_html_equal expected, @table.html
222
209
  end
223
210
 
224
- def test_should_use_class_from_proxy_reflection_to_generate_columns
225
- collection = NoteCollection.new
226
- collection.concat(@collection)
227
- table = TableHelper::CollectionTable.new(collection)
228
- html = table.build
211
+ def test_should_include_in_html_if_specified
212
+ @table.footer :total, 1
229
213
 
230
214
  expected = <<-end_str
231
- <thead>
232
- <tr>
233
- <th class="title" scope="col">Title</th>
234
- <th class="author_name" scope="col">Author Name</th>
235
- </tr>
236
- </thead>
237
- <tbody>
238
- <tr class="row">
239
- <td class="title">first</td>
240
- <td class="author_name empty"></td>
241
- </tr>
242
- <tr class="row">
243
- <td class="title">second</td>
244
- <td class="author_name empty"></td>
245
- </tr>
246
- <tr class="row">
247
- <td class="title">last</td>
248
- <td class="author_name empty"></td>
249
- </tr>
250
- </tbody>
215
+ <table cellpadding="0" cellspacing="0" class="objects ui-collection">
216
+ <tbody>
217
+ <tr class="object ui-collection-result"></tr>
218
+ </tbody>
219
+ <tfoot>
220
+ <tr>
221
+ <td class="object-total">1</td>
222
+ </tr>
223
+ </tfoot>
224
+ </table>
251
225
  end_str
252
- assert_html_equal expected, html
226
+ assert_html_equal expected, @table.html
253
227
  end
254
228
  end
@@ -2,17 +2,31 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  class FooterByDefaultTest < Test::Unit::TestCase
4
4
  def setup
5
- @footer = TableHelper::Footer.new([])
5
+ @table = TableHelper::CollectionTable.new([])
6
+ @footer = TableHelper::Footer.new(@table)
6
7
  end
7
8
 
8
- def test_should_hdie_when_empty
9
+ def test_should_hide_when_empty
9
10
  assert @footer.hide_when_empty
10
11
  end
12
+
13
+ def test_should_be_empty
14
+ assert @footer.empty?
15
+ end
16
+
17
+ def test_should_have_a_row
18
+ assert_not_nil @footer.row
19
+ end
20
+
21
+ def test_should_have_a_table
22
+ assert_equal @table, @footer.table
23
+ end
11
24
  end
12
25
 
13
26
  class FooterTest < Test::Unit::TestCase
14
27
  def setup
15
- @footer = TableHelper::Footer.new([1])
28
+ table = TableHelper::CollectionTable.new([Object.new])
29
+ @footer = TableHelper::Footer.new(table)
16
30
  end
17
31
 
18
32
  def test_should_include_custom_attributes
@@ -26,14 +40,24 @@ class FooterTest < Test::Unit::TestCase
26
40
  end_str
27
41
  assert_html_equal expected, @footer.html
28
42
  end
43
+ end
44
+
45
+ class FooterWithCellsTest < Test::Unit::TestCase
46
+ def setup
47
+ table = TableHelper::CollectionTable.new([Object.new])
48
+ @footer = TableHelper::Footer.new(table)
49
+ @cell = @footer.cell :total, 20
50
+ end
29
51
 
30
- def test_should_include_created_cells_when_built
31
- @footer.cell :total, 20
32
-
52
+ def test_should_namespace_cell_classes
53
+ assert_equal 'object-total', @cell[:class]
54
+ end
55
+
56
+ def test_should_build_html
33
57
  expected = <<-end_str
34
58
  <tfoot>
35
59
  <tr>
36
- <td class="total">20</td>
60
+ <td class="object-total">20</td>
37
61
  </tr>
38
62
  </tfoot>
39
63
  end_str
@@ -43,7 +67,8 @@ end
43
67
 
44
68
  class FooterWithEmptyCollectionTest < Test::Unit::TestCase
45
69
  def setup
46
- @footer = TableHelper::Footer.new([])
70
+ table = TableHelper::CollectionTable.new([])
71
+ @footer = TableHelper::Footer.new(table)
47
72
  end
48
73
 
49
74
  def test_should_not_display_if_hide_when_empty