application_helper_spec.rb 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. require 'rails_helper'
  2. describe ApplicationHelper do
  3. describe '#icon_tag' do
  4. it 'returns a Glyphicon icon element' do
  5. icon = icon_tag('glyphicon-help')
  6. expect(icon).to be_html_safe
  7. expect(Nokogiri(icon).at('span.glyphicon.glyphicon-help')).to be_a Nokogiri::XML::Element
  8. end
  9. it 'returns a Glyphicon icon element with an addidional class' do
  10. icon = icon_tag('glyphicon-help', class: 'text-info')
  11. expect(icon).to be_html_safe
  12. expect(Nokogiri(icon).at('span.glyphicon.glyphicon-help.text-info')).to be_a Nokogiri::XML::Element
  13. end
  14. it 'returns a FontAwesome icon element' do
  15. icon = icon_tag('fa-copy')
  16. expect(icon).to be_html_safe
  17. expect(Nokogiri(icon).at('i.fa.fa-copy')).to be_a Nokogiri::XML::Element
  18. end
  19. it 'returns a FontAwesome icon element' do
  20. icon = icon_tag('fa-copy', class: 'text-info')
  21. expect(icon).to be_html_safe
  22. expect(Nokogiri(icon).at('i.fa.fa-copy.text-info')).to be_a Nokogiri::XML::Element
  23. end
  24. end
  25. describe '#nav_link' do
  26. it 'returns a nav link' do
  27. stub(self).current_page?('/things') { false }
  28. nav = nav_link('Things', '/things')
  29. a = Nokogiri(nav).at('li:not(.active) > a[href="/things"]')
  30. expect(a.text.strip).to eq('Things')
  31. end
  32. it 'returns an active nav link' do
  33. stub(self).current_page?('/things') { true }
  34. nav = nav_link('Things', '/things')
  35. expect(nav).to be_html_safe
  36. a = Nokogiri(nav).at('li.active > a[href="/things"]')
  37. expect(a).to be_a Nokogiri::XML::Element
  38. expect(a.text.strip).to eq('Things')
  39. end
  40. describe 'with block' do
  41. it 'returns a nav link with menu' do
  42. stub(self).current_page?('/things') { false }
  43. stub(self).current_page?('/things/stuff') { false }
  44. nav = nav_link('Things', '/things') { nav_link('Stuff', '/things/stuff') }
  45. expect(nav).to be_html_safe
  46. a0 = Nokogiri(nav).at('li.dropdown.dropdown-hover:not(.active) > a[href="/things"]')
  47. expect(a0).to be_a Nokogiri::XML::Element
  48. expect(a0.text.strip).to eq('Things')
  49. a1 = Nokogiri(nav).at('li.dropdown.dropdown-hover:not(.active) > li:not(.active) > a[href="/things/stuff"]')
  50. expect(a1).to be_a Nokogiri::XML::Element
  51. expect(a1.text.strip).to eq('Stuff')
  52. end
  53. it 'returns an active nav link with menu' do
  54. stub(self).current_page?('/things') { true }
  55. stub(self).current_page?('/things/stuff') { false }
  56. nav = nav_link('Things', '/things') { nav_link('Stuff', '/things/stuff') }
  57. expect(nav).to be_html_safe
  58. a0 = Nokogiri(nav).at('li.dropdown.dropdown-hover.active > a[href="/things"]')
  59. expect(a0).to be_a Nokogiri::XML::Element
  60. expect(a0.text.strip).to eq('Things')
  61. a1 = Nokogiri(nav).at('li.dropdown.dropdown-hover.active > li:not(.active) > a[href="/things/stuff"]')
  62. expect(a1).to be_a Nokogiri::XML::Element
  63. expect(a1.text.strip).to eq('Stuff')
  64. end
  65. it 'returns an active nav link with menu when on a child page' do
  66. stub(self).current_page?('/things') { false }
  67. stub(self).current_page?('/things/stuff') { true }
  68. nav = nav_link('Things', '/things') { nav_link('Stuff', '/things/stuff') }
  69. expect(nav).to be_html_safe
  70. a0 = Nokogiri(nav).at('li.dropdown.dropdown-hover.active > a[href="/things"]')
  71. expect(a0).to be_a Nokogiri::XML::Element
  72. expect(a0.text.strip).to eq('Things')
  73. a1 = Nokogiri(nav).at('li.dropdown.dropdown-hover.active > li:not(.active) > a[href="/things/stuff"]')
  74. expect(a1).to be_a Nokogiri::XML::Element
  75. expect(a1.text.strip).to eq('Stuff')
  76. end
  77. end
  78. end
  79. describe '#yes_no' do
  80. it 'returns a label "Yes" if any truthy value is given' do
  81. [true, Object.new].each { |value|
  82. label = yes_no(value)
  83. expect(label).to be_html_safe
  84. expect(Nokogiri(label).text).to eq 'Yes'
  85. }
  86. end
  87. it 'returns a label "No" if any falsy value is given' do
  88. [false, nil].each { |value|
  89. label = yes_no(value)
  90. expect(label).to be_html_safe
  91. expect(Nokogiri(label).text).to eq 'No'
  92. }
  93. end
  94. end
  95. describe '#working' do
  96. before do
  97. @agent = agents(:jane_website_agent)
  98. end
  99. it 'returns a label "Disabled" if a given agent is disabled' do
  100. stub(@agent).disabled? { true }
  101. label = working(@agent)
  102. expect(label).to be_html_safe
  103. expect(Nokogiri(label).text).to eq 'Disabled'
  104. end
  105. it 'returns a label "Missing Gems" if a given agent has dependencies missing' do
  106. stub(@agent).dependencies_missing? { true }
  107. label = working(@agent)
  108. expect(label).to be_html_safe
  109. expect(Nokogiri(label).text).to eq 'Missing Gems'
  110. end
  111. it 'returns a label "Yes" if a given agent is working' do
  112. stub(@agent).working? { true }
  113. label = working(@agent)
  114. expect(label).to be_html_safe
  115. expect(Nokogiri(label).text).to eq 'Yes'
  116. end
  117. it 'returns a label "No" if a given agent is not working' do
  118. stub(@agent).working? { false }
  119. label = working(@agent)
  120. expect(label).to be_html_safe
  121. expect(Nokogiri(label).text).to eq 'No'
  122. end
  123. end
  124. describe '#omniauth_provider_icon' do
  125. it 'returns a correct icon tag for Twitter' do
  126. icon = omniauth_provider_icon(:twitter)
  127. expect(icon).to be_html_safe
  128. elem = Nokogiri(icon).at('i.fa.fa-twitter')
  129. expect(elem).to be_a Nokogiri::XML::Element
  130. end
  131. it 'returns a correct icon tag for GitHub' do
  132. icon = omniauth_provider_icon(:github)
  133. expect(icon).to be_html_safe
  134. elem = Nokogiri(icon).at('i.fa.fa-github')
  135. expect(elem).to be_a Nokogiri::XML::Element
  136. end
  137. it 'returns a correct icon tag for other services' do
  138. icon = omniauth_provider_icon(:'37signals')
  139. expect(icon).to be_html_safe
  140. elem = Nokogiri(icon).at('i.fa.fa-lock')
  141. expect(elem).to be_a Nokogiri::XML::Element
  142. end
  143. end
  144. describe '#highlighted?' do
  145. it 'understands hl=6-8' do
  146. stub(params).[](:hl) { '6-8' }
  147. expect((1..10).select { |i| highlighted?(i) }).to eq [6, 7, 8]
  148. end
  149. it 'understands hl=1,3-4,9' do
  150. stub(params).[](:hl) { '1,3-4,9' }
  151. expect((1..10).select { |i| highlighted?(i) }).to eq [1, 3, 4, 9]
  152. end
  153. it 'understands hl=8-' do
  154. stub(params).[](:hl) { '8-' }
  155. expect((1..10).select { |i| highlighted?(i) }).to eq [8, 9, 10]
  156. end
  157. it 'understands hl=-2' do
  158. stub(params).[](:hl) { '-2' }
  159. expect((1..10).select { |i| highlighted?(i) }).to eq [1, 2]
  160. end
  161. it 'understands hl=-' do
  162. stub(params).[](:hl) { '-' }
  163. expect((1..10).select { |i| highlighted?(i) }).to eq [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  164. end
  165. it 'is OK with no hl' do
  166. stub(params).[](:hl) { nil }
  167. expect((1..10).select { |i| highlighted?(i) }).to be_empty
  168. end
  169. end
  170. end