application_helper_spec.rb 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe ApplicationHelper do
  4. describe 'active_nav_class' do
  5. it 'returns active when on the current page' do
  6. allow(helper).to receive(:current_page?).and_return(true)
  7. result = helper.active_nav_class('/test')
  8. expect(result).to eq 'active'
  9. end
  10. it 'returns active when on a current page' do
  11. allow(helper).to receive(:current_page?).with('/foo').and_return(false)
  12. allow(helper).to receive(:current_page?).with('/test').and_return(true)
  13. result = helper.active_nav_class('/foo', '/test')
  14. expect(result).to eq 'active'
  15. end
  16. it 'returns empty string when not on current page' do
  17. allow(helper).to receive(:current_page?).and_return(false)
  18. result = helper.active_nav_class('/test')
  19. expect(result).to eq ''
  20. end
  21. end
  22. describe 'body_classes' do
  23. context 'with a body class string from a controller' do
  24. before do
  25. without_partial_double_verification do
  26. allow(helper).to receive_messages(body_class_string: 'modal-layout compose-standalone', current_theme: 'default', current_account: Fabricate(:account))
  27. end
  28. end
  29. it 'uses the controller body classes in the result' do
  30. expect(helper.body_classes).to match(/modal-layout compose-standalone/)
  31. end
  32. end
  33. end
  34. describe 'locale_direction' do
  35. it 'adds rtl body class if locale is Arabic' do
  36. I18n.with_locale(:ar) do
  37. expect(helper.locale_direction).to eq 'rtl'
  38. end
  39. end
  40. it 'adds rtl body class if locale is Farsi' do
  41. I18n.with_locale(:fa) do
  42. expect(helper.locale_direction).to eq 'rtl'
  43. end
  44. end
  45. it 'adds rtl if locale is Hebrew' do
  46. I18n.with_locale(:he) do
  47. expect(helper.locale_direction).to eq 'rtl'
  48. end
  49. end
  50. it 'does not add rtl if locale is Thai' do
  51. I18n.with_locale(:th) do
  52. expect(helper.locale_direction).to_not eq 'rtl'
  53. end
  54. end
  55. end
  56. describe 'fa_icon' do
  57. it 'returns a tag of fixed-width cog' do
  58. expect(helper.fa_icon('cog fw')).to eq '<i class="fa fa-cog fa-fw"></i>'
  59. end
  60. end
  61. describe 'open_registrations?' do
  62. it 'returns true when open for registrations' do
  63. allow(Setting).to receive(:[]).with('registrations_mode').and_return('open')
  64. expect(helper.open_registrations?).to be true
  65. expect(Setting).to have_received(:[]).with('registrations_mode')
  66. end
  67. it 'returns false when closed for registrations' do
  68. allow(Setting).to receive(:[]).with('registrations_mode').and_return('none')
  69. expect(helper.open_registrations?).to be false
  70. expect(Setting).to have_received(:[]).with('registrations_mode')
  71. end
  72. end
  73. describe 'show_landing_strip?', without_verify_partial_doubles: true do
  74. describe 'when signed in' do
  75. before do
  76. allow(helper).to receive(:user_signed_in?).and_return(true)
  77. end
  78. it 'does not show landing strip' do
  79. expect(helper.show_landing_strip?).to be false
  80. end
  81. end
  82. describe 'when signed out' do
  83. before do
  84. allow(helper).to receive(:user_signed_in?).and_return(false)
  85. end
  86. it 'does not show landing strip on single user instance' do
  87. allow(helper).to receive(:single_user_mode?).and_return(true)
  88. expect(helper.show_landing_strip?).to be false
  89. end
  90. it 'shows landing strip on multi user instance' do
  91. allow(helper).to receive(:single_user_mode?).and_return(false)
  92. expect(helper.show_landing_strip?).to be true
  93. end
  94. end
  95. end
  96. describe 'available_sign_up_path' do
  97. context 'when registrations are closed' do
  98. before do
  99. without_partial_double_verification do
  100. allow(Setting).to receive(:registrations_mode).and_return('none')
  101. end
  102. end
  103. it 'redirects to joinmastodon site' do
  104. expect(helper.available_sign_up_path).to match(/joinmastodon.org/)
  105. end
  106. end
  107. context 'when in omniauth only mode' do
  108. around do |example|
  109. ClimateControl.modify OMNIAUTH_ONLY: 'true' do
  110. example.run
  111. end
  112. end
  113. it 'redirects to joinmastodon site' do
  114. expect(helper.available_sign_up_path).to match(/joinmastodon.org/)
  115. end
  116. end
  117. context 'when registrations are allowed' do
  118. it 'returns a link to the registration page' do
  119. expect(helper.available_sign_up_path).to eq(new_user_registration_path)
  120. end
  121. end
  122. end
  123. describe 'omniauth_only?' do
  124. context 'when env var is set to true' do
  125. around do |example|
  126. ClimateControl.modify OMNIAUTH_ONLY: 'true' do
  127. example.run
  128. end
  129. end
  130. it 'returns true' do
  131. expect(helper).to be_omniauth_only
  132. end
  133. end
  134. context 'when env var is not set' do
  135. around do |example|
  136. ClimateControl.modify OMNIAUTH_ONLY: nil do
  137. example.run
  138. end
  139. end
  140. it 'returns false' do
  141. expect(helper).to_not be_omniauth_only
  142. end
  143. end
  144. end
  145. describe 'quote_wrap' do
  146. it 'indents and quote wraps text' do
  147. text = <<~TEXT
  148. Hello this is a nice message for you to quote.
  149. Be careful because it has two lines.
  150. TEXT
  151. expect(helper.quote_wrap(text)).to eq <<~EXPECTED.strip
  152. > Hello this is a nice message for you to quote.
  153. > Be careful because it has two lines.
  154. EXPECTED
  155. end
  156. end
  157. describe 'storage_host' do
  158. context 'when S3 alias is present' do
  159. around do |example|
  160. ClimateControl.modify S3_ALIAS_HOST: 's3.alias' do
  161. example.run
  162. end
  163. end
  164. it 'returns true' do
  165. expect(helper.storage_host).to eq('https://s3.alias')
  166. end
  167. end
  168. context 'when S3 alias includes a path component' do
  169. around do |example|
  170. ClimateControl.modify S3_ALIAS_HOST: 's3.alias/path' do
  171. example.run
  172. end
  173. end
  174. it 'returns a correct URL' do
  175. expect(helper.storage_host).to eq('https://s3.alias/path')
  176. end
  177. end
  178. context 'when S3 cloudfront is present' do
  179. around do |example|
  180. ClimateControl.modify S3_CLOUDFRONT_HOST: 's3.cloudfront' do
  181. example.run
  182. end
  183. end
  184. it 'returns true' do
  185. expect(helper.storage_host).to eq('https://s3.cloudfront')
  186. end
  187. end
  188. end
  189. describe 'storage_host?' do
  190. context 'when S3 alias is present' do
  191. around do |example|
  192. ClimateControl.modify S3_ALIAS_HOST: 's3.alias' do
  193. example.run
  194. end
  195. end
  196. it 'returns true' do
  197. expect(helper.storage_host?).to be true
  198. end
  199. end
  200. context 'when S3 cloudfront is present' do
  201. around do |example|
  202. ClimateControl.modify S3_CLOUDFRONT_HOST: 's3.cloudfront' do
  203. example.run
  204. end
  205. end
  206. it 'returns true' do
  207. expect(helper.storage_host?).to be true
  208. end
  209. end
  210. context 'when neither env value is present' do
  211. it 'returns false' do
  212. expect(helper.storage_host?).to be false
  213. end
  214. end
  215. end
  216. describe 'visibility_icon' do
  217. it 'returns a globe icon for a public visible status' do
  218. result = helper.visibility_icon Status.new(visibility: 'public')
  219. expect(result).to match(/globe/)
  220. end
  221. it 'returns an unlock icon for a unlisted visible status' do
  222. result = helper.visibility_icon Status.new(visibility: 'unlisted')
  223. expect(result).to match(/unlock/)
  224. end
  225. it 'returns a lock icon for a private visible status' do
  226. result = helper.visibility_icon Status.new(visibility: 'private')
  227. expect(result).to match(/lock/)
  228. end
  229. it 'returns an at icon for a direct visible status' do
  230. result = helper.visibility_icon Status.new(visibility: 'direct')
  231. expect(result).to match(/at/)
  232. end
  233. end
  234. describe 'title' do
  235. around do |example|
  236. site_title = Setting.site_title
  237. example.run
  238. Setting.site_title = site_title
  239. end
  240. it 'returns site title on production environment' do
  241. Setting.site_title = 'site title'
  242. allow(Rails.env).to receive(:production?).and_return(true)
  243. expect(helper.title).to eq 'site title'
  244. expect(Rails.env).to have_received(:production?)
  245. end
  246. end
  247. end