/
tabs_test.rb
122 lines (93 loc) · 3.42 KB
/
tabs_test.rb
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
require 'test_helper'
class TabsTest < ActionView::TestCase
OpenZeroArgsBuilder = Class.new(TabsOnRails::Tabs::Builder) do
def open_tabs
'<ul>'
end
end
CloseZeroArgsBuilder = Class.new(TabsOnRails::Tabs::Builder) do
def close_tabs
'</ul>'
end
end
BlockBuilder = Class.new(TabsOnRails::Tabs::Builder) do
def tab_for(tab, name, options, item_options = {}, &block)
item_options[:class] = item_options[:class].to_s.split(" ").push("current").join(" ") if current_tab?(tab)
content = @context.link_to(name, options)
content += @context.capture(&block) if block_given?
@context.content_tag(:li, content, item_options)
end
end
def setup
@template = self
@tabs = TabsOnRails::Tabs.new(@template)
end
def test_initialize
@tabs = TabsOnRails::Tabs.new(@template)
assert_equal @template, @tabs.instance_variable_get(:"@context")
assert_instance_of TabsOnRails::Tabs::TabsBuilder, @tabs.instance_variable_get(:"@builder")
end
def test_initialize_with_option_builder
builder = Class.new(TabsOnRails::Tabs::TabsBuilder)
@tabs = TabsOnRails::Tabs.new(@template, :builder => builder)
assert_equal @template, @tabs.instance_variable_get(:"@context")
assert_instance_of builder, @tabs.instance_variable_get(:"@builder")
end
def test_initialize_with_default_builder
default_builder = TabsOnRails::Tabs.default_builder
TabsOnRails::Tabs.default_builder = CustomBuilder
custom_tab = TabsOnRails::Tabs.new(@template) # doesnt need argument :builder => CustomBuilder
assert_instance_of CustomBuilder, custom_tab.instance_variable_get(:"@builder")
TabsOnRails::Tabs.default_builder = default_builder
end
def test_open_tabs
assert_equal '<ul>', @tabs.open_tabs
end
def test_open_tabs_with_options
assert_equal '<ul class="foo">', @tabs.open_tabs(:class => "foo")
end
def test_open_tabs_should_ignore_options_if_arity_is_zero
@tabs = TabsOnRails::Tabs.new(@template, :builder => OpenZeroArgsBuilder)
assert_nothing_raised do
assert_equal '<ul>', @tabs.open_tabs(:class => "foo")
end
end
def test_close_tabs
assert_equal '</ul>', @tabs.close_tabs
end
def test_close_tabs_with_options
assert_equal '</ul>', @tabs.close_tabs(:class => "foo")
end
def test_open_tabs_should_ignore_options_if_arity_is_zero
@tabs = TabsOnRails::Tabs.new(@template, :builder => CloseZeroArgsBuilder)
assert_nothing_raised do
assert_equal '</ul>', @tabs.close_tabs(:class => "foo")
end
end
def test_tab_for
assert_equal %Q{<li><a href="#">Welcome</a></li>},
@tabs.welcome('Welcome', '#')
end
def test_tab_for_with_block
@tabs = TabsOnRails::Tabs.new(@template, :builder => BlockBuilder)
expected = %Q{<li class="current"><a href="http://dashboard.com/">Foo Bar</a>More Content</li>}
result = @tabs.dashboard('Foo Bar', 'http://dashboard.com/') do
"More Content"
end
assert_dom_equal expected, result
end
class CustomBuilder < TabsOnRails::Tabs::Builder
def open_tabs(options = {})
@context.tag("ul", options, open = true)
end
def close_tabs(options = {})
"</ul>".html_safe
end
def tab_for(tab, name, options, item_options = {})
item_options[:class] = (current_tab?(tab) ? 'active' : '')
@context.content_tag(:li, item_options) do
@context.link_to(name, options)
end
end
end
end