1
0

application_helper_spec.rb 7.4 KB

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