tangofoxtrot-table_helper 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,270 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ class HeaderByDefaultTest < Test::Unit::TestCase
4
+ def setup
5
+ @table = TableHelper::CollectionTable.new([])
6
+ @header = TableHelper::Header.new(@table)
7
+ end
8
+
9
+ def test_should_hide_when_empty
10
+ assert @header.hide_when_empty
11
+ end
12
+
13
+ def test_should_have_no_columns
14
+ expected = {}
15
+ assert_equal expected, @header.columns
16
+ end
17
+
18
+ def test_should_have_no_column_names
19
+ assert_equal [], @header.column_names
20
+ end
21
+
22
+ def test_should_be_empty
23
+ assert @header.empty?
24
+ end
25
+
26
+ def test_should_have_a_row
27
+ assert_not_nil @header.row
28
+ end
29
+
30
+ def test_should_have_a_table
31
+ assert_equal @table, @header.table
32
+ end
33
+ end
34
+
35
+ class HeaderWithColumnDetectionTest < Test::Unit::TestCase
36
+ class Post
37
+ def self.column_names
38
+ ['title', 'author_name']
39
+ end
40
+ end
41
+
42
+ def test_should_have_columns_if_class_has_column_names
43
+ table = TableHelper::CollectionTable.new([], Post)
44
+ header = TableHelper::Header.new(table)
45
+
46
+ assert_equal ['title', 'author_name'], header.column_names
47
+ end
48
+
49
+ def test_should_not_have_columns_if_class_has_no_column_names
50
+ table = TableHelper::CollectionTable.new([], Array)
51
+ header = TableHelper::Header.new(table)
52
+
53
+ assert header.columns.empty?
54
+ end
55
+ end
56
+
57
+ class HeaderWithAutomaticColumnsTest < Test::Unit::TestCase
58
+ class Post
59
+ def self.column_names
60
+ ['title', 'author_name']
61
+ end
62
+ end
63
+
64
+ def setup
65
+ table = TableHelper::CollectionTable.new([Post.new], Post)
66
+ @header = TableHelper::Header.new(table)
67
+ end
68
+
69
+ def test_should_use_titleized_name_for_content
70
+ assert_equal 'Title', @header.columns['title'].content
71
+ assert_equal 'Author Name', @header.columns['author_name'].content
72
+ end
73
+
74
+ def test_should_namespace_html_classes
75
+ assert_equal 'post-title', @header.columns['title'][:class]
76
+ assert_equal 'post-author_name', @header.columns['author_name'][:class]
77
+ end
78
+
79
+ def test_should_not_be_empty
80
+ assert !@header.empty?
81
+ end
82
+
83
+ def test_should_build_html
84
+ expected = <<-end_str
85
+ <thead>
86
+ <tr>
87
+ <th class="post-title" scope="col">Title</th>
88
+ <th class="post-author_name" scope="col">Author Name</th>
89
+ </tr>
90
+ </thead>
91
+ end_str
92
+ assert_html_equal expected, @header.html
93
+ end
94
+
95
+ def test_should_clear_existing_columns_when_first_column_is_created
96
+ cell = @header.column :created_on
97
+
98
+ assert_raise(NoMethodError) {@header.builder.title}
99
+ assert_raise(NoMethodError) {@header.builder.author_name}
100
+
101
+ expected = {'created_on' => cell}
102
+ assert_equal expected, @header.columns
103
+ end
104
+ end
105
+
106
+ class HeaderWithCustomColumnsTest < Test::Unit::TestCase
107
+ def setup
108
+ table = TableHelper::CollectionTable.new([])
109
+ @header = TableHelper::Header.new(table)
110
+ @title = @header.column :title
111
+ end
112
+
113
+ def test_should_set_scope
114
+ assert_equal 'col', @title[:scope]
115
+ end
116
+
117
+ def test_should_use_name_for_default_content
118
+ assert_equal 'Title', @title.content
119
+ end
120
+
121
+ def test_should_allow_content_to_be_customized
122
+ title = @header.column :title, 'The Title'
123
+ assert_equal 'The Title', title.content
124
+ end
125
+
126
+ def test_should_allow_html_options_to_be_customized
127
+ title = @header.column :title, :class => 'pretty'
128
+ assert_equal 'pretty title', title[:class]
129
+ end
130
+
131
+ def test_should_not_be_empty
132
+ assert !@header.empty?
133
+ end
134
+ end
135
+
136
+ class HeaderWithMultipleColumnsTest < Test::Unit::TestCase
137
+ def setup
138
+ table = TableHelper::CollectionTable.new([])
139
+ @header = TableHelper::Header.new(table)
140
+ @title, @author_name = @header.column :title, :author_name, :class => 'pretty'
141
+ end
142
+
143
+ def test_should_use_default_content_for_each
144
+ assert_equal 'Title', @title.content
145
+ assert_equal 'Author Name', @author_name.content
146
+ end
147
+
148
+ def test_should_share_html_options
149
+ assert_equal 'pretty title', @title[:class]
150
+ end
151
+ end
152
+
153
+ class HeaderWithEmptyCollectionTest < Test::Unit::TestCase
154
+ def setup
155
+ table = TableHelper::CollectionTable.new([])
156
+ @header = TableHelper::Header.new(table)
157
+ end
158
+
159
+ def test_should_not_display_if_hide_when_empty
160
+ @header.hide_when_empty = true
161
+
162
+ expected = <<-end_str
163
+ <thead style="display: none;">
164
+ <tr>
165
+ </tr>
166
+ </thead>
167
+ end_str
168
+ assert_html_equal expected, @header.html
169
+ end
170
+
171
+ def test_should_display_if_not_hide_when_empty
172
+ @header.hide_when_empty = false
173
+
174
+ expected = <<-end_str
175
+ <thead>
176
+ <tr>
177
+ </tr>
178
+ </thead>
179
+ end_str
180
+ assert_html_equal expected, @header.html
181
+ end
182
+ end
183
+
184
+ class HeaderWithCollectionTest < Test::Unit::TestCase
185
+ def setup
186
+ table = TableHelper::CollectionTable.new([Object.new])
187
+ @header = TableHelper::Header.new(table)
188
+ @header.column :title, :author_name
189
+ end
190
+
191
+ def test_should_display_if_hide_when_empty
192
+ @header.hide_when_empty = true
193
+
194
+ expected = <<-end_str
195
+ <thead>
196
+ <tr>
197
+ <th class="object-title" scope="col">Title</th>
198
+ <th class="object-author_name" scope="col">Author Name</th>
199
+ </tr>
200
+ </thead>
201
+ end_str
202
+ assert_html_equal expected, @header.html
203
+ end
204
+
205
+ def test_should_display_if_not_hide_when_empty
206
+ @header.hide_when_empty = false
207
+
208
+ expected = <<-end_str
209
+ <thead>
210
+ <tr>
211
+ <th class="object-title" scope="col">Title</th>
212
+ <th class="object-author_name" scope="col">Author Name</th>
213
+ </tr>
214
+ </thead>
215
+ end_str
216
+ assert_html_equal expected, @header.html
217
+ end
218
+ end
219
+
220
+ class HeaderWithCustomHtmlOptionsTest < Test::Unit::TestCase
221
+ def setup
222
+ table = TableHelper::CollectionTable.new([Object.new])
223
+ @header = TableHelper::Header.new(table)
224
+ @header.column :title
225
+ end
226
+
227
+ def test_should_include_html_options
228
+ @header[:class] = 'pretty'
229
+
230
+ expected = <<-end_str
231
+ <thead class="pretty">
232
+ <tr>
233
+ <th class="object-title" scope="col">Title</th>
234
+ </tr>
235
+ </thead>
236
+ end_str
237
+ assert_html_equal expected, @header.html
238
+ end
239
+
240
+ def test_should_include_html_options_for_header_row
241
+ @header.row[:class] = 'pretty'
242
+
243
+ expected = <<-end_str
244
+ <thead>
245
+ <tr class="pretty">
246
+ <th class="object-title" scope="col">Title</th>
247
+ </tr>
248
+ </thead>
249
+ end_str
250
+ assert_html_equal expected, @header.html
251
+ end
252
+ end
253
+
254
+ class HeaderWithModelsTest < ActiveRecord::TestCase
255
+ def setup
256
+ Person.create(:first_name => 'John', :last_name => 'Smith')
257
+ end
258
+
259
+ def test_should_include_all_columns_if_not_selecting_columns
260
+ table = TableHelper::CollectionTable.new(Person.all)
261
+ @header = TableHelper::Header.new(table)
262
+ assert_equal %w(first_name id last_name), @header.column_names.sort
263
+ end
264
+
265
+ def test_should_only_include_selected_columns_if_specified_in_query
266
+ table = TableHelper::CollectionTable.new(Person.all(:select => 'first_name'))
267
+ @header = TableHelper::Header.new(table)
268
+ assert_equal %w(first_name), @header.column_names.sort
269
+ end
270
+ end
@@ -0,0 +1,74 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ class HtmlElementByDefaultTest < Test::Unit::TestCase
4
+ def setup
5
+ @element = TableHelper::HtmlElement.new
6
+ end
7
+
8
+ def test_should_generate_an_empty_tag
9
+ assert_equal '<></>', @element.html
10
+ end
11
+
12
+ def test_not_have_any_html_options
13
+ element = TableHelper::HtmlElement.new
14
+ assert_nil element[:class]
15
+ end
16
+ end
17
+
18
+ class HtmlElementTest < Test::Unit::TestCase
19
+ class DivElement < TableHelper::HtmlElement
20
+ def tag_name
21
+ 'div'
22
+ end
23
+ end
24
+
25
+ def test_should_set_html_options_on_initialization
26
+ element = TableHelper::HtmlElement.new(:class => 'fancy')
27
+ assert_equal 'fancy', element[:class]
28
+ end
29
+
30
+ def test_should_symbolize_html_options
31
+ element = TableHelper::HtmlElement.new('class' => 'fancy')
32
+ assert_equal 'fancy', element[:class]
33
+ end
34
+
35
+ def test_should_generate_entire_element_if_content_and_tag_name_specified
36
+ element = DivElement.new
37
+ element.instance_eval do
38
+ def content
39
+ 'hello world'
40
+ end
41
+ end
42
+
43
+ assert_equal '<div>hello world</div>', element.html
44
+ end
45
+
46
+ def test_should_include_html_options_in_element_tag
47
+ element = DivElement.new
48
+ element[:class] = 'fancy'
49
+
50
+ assert_equal '<div class="fancy"></div>', element.html
51
+ end
52
+
53
+ def test_should_save_changes_in_html_options
54
+ element = TableHelper::HtmlElement.new
55
+ element[:float] = 'left'
56
+ assert_equal 'left', element[:float]
57
+ end
58
+ end
59
+
60
+ class HtmlElementWithNoContentTest < Test::Unit::TestCase
61
+ class DivElement < TableHelper::HtmlElement
62
+ def tag_name
63
+ 'div'
64
+ end
65
+ end
66
+
67
+ def setup
68
+ @element = DivElement.new
69
+ end
70
+
71
+ def test_should_generate_empty_tags
72
+ assert_equal '<div></div>', @element.html
73
+ end
74
+ end
@@ -0,0 +1,55 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ class RowBuilderParent < TableHelper::HtmlElement
4
+ attr_reader :table
5
+
6
+ def initialize(table = TableHelper::CollectionTable.new([]))
7
+ @table = table
8
+ end
9
+ end
10
+
11
+ class RowBuilderByDefaultTest < Test::Unit::TestCase
12
+ def setup
13
+ @row = TableHelper::Row.new(RowBuilderParent.new)
14
+ @builder = TableHelper::RowBuilder.new(@row)
15
+ end
16
+
17
+ def test_should_forward_missing_calls_to_row
18
+ assert_equal '<tr></tr>', @builder.html
19
+ end
20
+ end
21
+
22
+ class RowBuilderWithCellsTest < Test::Unit::TestCase
23
+ def setup
24
+ @row = TableHelper::Row.new(RowBuilderParent.new)
25
+ @builder = TableHelper::RowBuilder.new(@row)
26
+ @builder.define_cell('first-name')
27
+ end
28
+
29
+ def test_should_create_cell_reader
30
+ assert @builder.respond_to?(:first_name)
31
+ end
32
+
33
+ def test_should_read_cell_without_arguments
34
+ cell = @row.cells['first-name'] = TableHelper::Cell.new('first-name')
35
+ assert_equal cell, @builder.first_name
36
+ end
37
+
38
+ def test_should_write_cell_with_arguments
39
+ cell = @builder.first_name 'Your Name'
40
+ assert_equal 'Your Name', cell.content
41
+ end
42
+ end
43
+
44
+ class RowBuilderAfterUndefiningACellTest < Test::Unit::TestCase
45
+ def setup
46
+ @row = TableHelper::Row.new(RowBuilderParent.new)
47
+ @builder = TableHelper::RowBuilder.new(@row)
48
+ @builder.define_cell('first-name')
49
+ @builder.undef_cell('first-name')
50
+ end
51
+
52
+ def test_should_not_have_cell_reader
53
+ assert_raise(NoMethodError) {@builder.first_name}
54
+ end
55
+ end
@@ -0,0 +1,204 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ class RowParent < TableHelper::HtmlElement
4
+ attr_reader :table
5
+
6
+ def initialize(table = TableHelper::CollectionTable.new([]))
7
+ @table = table
8
+ end
9
+ end
10
+
11
+ class RowByDefaultTest < Test::Unit::TestCase
12
+ def setup
13
+ @parent = RowParent.new
14
+ @row = TableHelper::Row.new(@parent)
15
+ end
16
+
17
+ def test_should_not_have_class
18
+ assert_nil @row[:class]
19
+ end
20
+
21
+ def test_should_not_have_any_cells
22
+ assert @row.cells.empty?
23
+ end
24
+
25
+ def test_should_not_have_any_cell_names
26
+ assert_equal [], @row.cell_names
27
+ end
28
+
29
+ def test_should_have_a_builder
30
+ assert_not_nil @row.builder
31
+ end
32
+
33
+ def test_should_have_a_parent
34
+ assert_equal @parent, @row.parent
35
+ end
36
+
37
+ def test_should_have_a_table
38
+ assert_equal @parent.table, @row.table
39
+ end
40
+
41
+ def test_should_be_empty
42
+ assert @row.empty?
43
+ end
44
+ end
45
+
46
+ class RowWithoutCellsTest < Test::Unit::TestCase
47
+ def setup
48
+ @row = TableHelper::Row.new(RowParent.new)
49
+ end
50
+
51
+ def test_should_be_able_to_clear_cells
52
+ @row.clear
53
+ assert_equal [], @row.cell_names
54
+ end
55
+
56
+ def test_should_be_empty
57
+ assert @row.empty?
58
+ end
59
+
60
+ def test_should_build_html
61
+ assert_equal '<tr></tr>', @row.html
62
+ end
63
+ end
64
+
65
+ class RowWithCellsTest < Test::Unit::TestCase
66
+ def setup
67
+ @row = TableHelper::Row.new(RowParent.new)
68
+ @cell = @row.cell :name
69
+ end
70
+
71
+ def test_should_create_cell_reader
72
+ assert_nothing_raised {@row.builder.name}
73
+ assert_equal @cell, @row.builder.name
74
+ end
75
+
76
+ def test_should_use_cell_name_for_class_name
77
+ assert_equal 'name', @cell[:class]
78
+ end
79
+
80
+ def test_should_have_cells
81
+ expected = {'name' => @cell}
82
+ assert_equal expected, @row.cells
83
+ end
84
+
85
+ def test_should_have_cell_names
86
+ assert_equal ['name'], @row.cell_names
87
+ end
88
+
89
+ def test_should_not_be_empty
90
+ assert !@row.empty?
91
+ end
92
+
93
+ def test_should_be_able_to_clear_existing_cells
94
+ @row.clear
95
+
96
+ assert_equal [], @row.cell_names
97
+ end
98
+
99
+ def test_should_build_html
100
+ assert_equal '<tr><td class="name">Name</td></tr>', @row.html
101
+ end
102
+
103
+ def test_should_create_new_cell_if_cell_already_exists
104
+ new_cell = @row.cell :name
105
+ assert_not_same new_cell, @cell
106
+ end
107
+
108
+ def test_should_be_able_to_recreate_cell_after_clearing
109
+ @row.clear
110
+ @row.cell :name
111
+
112
+ assert_equal ['name'], @row.cell_names
113
+ assert_nothing_raised {@row.builder.name}
114
+ end
115
+
116
+ def test_should_allow_html_options
117
+ @row.clear
118
+ cell = @row.cell :name, 'Name', :class => 'pretty'
119
+
120
+ assert_equal 'pretty name', cell[:class]
121
+ end
122
+ end
123
+
124
+ class RowWithMultipleCellsTest < Test::Unit::TestCase
125
+ def setup
126
+ @row = TableHelper::Row.new(RowParent.new)
127
+ @name = @row.cell :name
128
+ @location = @row.cell :location
129
+ end
130
+
131
+ def test_should_have_cells
132
+ expected = {'name' => @name, 'location' => @location}
133
+ assert_equal expected, @row.cells
134
+ end
135
+
136
+ def test_should_have_cell_names
137
+ assert_equal ['location', 'name'], @row.cell_names.sort
138
+ end
139
+
140
+ def test_should_build_html
141
+ assert_equal '<tr><td class="name">Name</td><td class="location">Location</td></tr>', @row.html
142
+ end
143
+ end
144
+
145
+ class RowWithUnconventionalCellNamesTest < Test::Unit::TestCase
146
+ def setup
147
+ @row = TableHelper::Row.new(RowParent.new)
148
+ @cell = @row.cell 'the-name'
149
+ end
150
+
151
+ def test_should_have_cells
152
+ expected = {'the-name' => @cell}
153
+ assert_equal expected, @row.cells
154
+ end
155
+
156
+ def test_should_have_cell_names
157
+ assert_equal ['the-name'], @row.cell_names
158
+ end
159
+
160
+ def test_should_use_sanitized_name_for_reader_method
161
+ assert_nothing_raised {@row.builder.the_name}
162
+ assert_equal @cell, @row.builder.the_name
163
+ end
164
+
165
+ def test_should_use_unsanitized_name_for_class
166
+ assert_equal 'the-name', @cell[:class]
167
+ end
168
+ end
169
+
170
+ class RowWithConflictingCellNamesTest < Test::Unit::TestCase
171
+ def setup
172
+ @row = TableHelper::Row.new(RowParent.new)
173
+ @row.cell :id
174
+ end
175
+
176
+ def test_should_be_able_to_read_cell
177
+ assert_instance_of TableHelper::Cell, @row.cells['id']
178
+ end
179
+
180
+ def test_should_be_able_to_write_to_cell
181
+ @row.builder.id '1'
182
+ assert_instance_of TableHelper::Cell, @row.cells['id']
183
+ end
184
+
185
+ def test_should_be_able_to_clear
186
+ assert_nothing_raised {@row.clear}
187
+ end
188
+ end
189
+
190
+ class RowWithTableObjectNameTest < Test::Unit::TestCase
191
+ def setup
192
+ table = TableHelper::CollectionTable.new([], Object)
193
+ @row = TableHelper::Row.new(RowParent.new(table))
194
+ @cell = @row.cell :id
195
+ end
196
+
197
+ def test_should_not_have_class
198
+ assert_nil @row[:class]
199
+ end
200
+
201
+ def test_should_namespace_cell
202
+ assert_equal 'object-id', @cell[:class]
203
+ end
204
+ end