application_helper_spec.rb 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 { helper.extend controller_helpers }
  25. it 'uses the controller body classes in the result' do
  26. expect(helper.body_classes).to match(/modal-layout compose-standalone/)
  27. end
  28. private
  29. def controller_helpers
  30. Module.new do
  31. def body_class_string = 'modal-layout compose-standalone'
  32. def current_account
  33. @current_account ||= Fabricate(:account)
  34. end
  35. def current_theme = 'default'
  36. end
  37. end
  38. end
  39. end
  40. describe 'locale_direction' do
  41. it 'adds rtl body class if locale is Arabic' do
  42. I18n.with_locale(:ar) do
  43. expect(helper.locale_direction).to eq 'rtl'
  44. end
  45. end
  46. it 'adds rtl body class if locale is Farsi' do
  47. I18n.with_locale(:fa) do
  48. expect(helper.locale_direction).to eq 'rtl'
  49. end
  50. end
  51. it 'adds rtl if locale is Hebrew' do
  52. I18n.with_locale(:he) do
  53. expect(helper.locale_direction).to eq 'rtl'
  54. end
  55. end
  56. it 'does not add rtl if locale is Thai' do
  57. I18n.with_locale(:th) do
  58. expect(helper.locale_direction).to_not eq 'rtl'
  59. end
  60. end
  61. end
  62. describe 'fa_icon' do
  63. it 'returns a tag of fixed-width cog' do
  64. expect(helper.fa_icon('cog fw')).to eq '<i class="fa fa-cog fa-fw"></i>'
  65. end
  66. end
  67. describe 'open_registrations?' do
  68. it 'returns true when open for registrations' do
  69. allow(Setting).to receive(:[]).with('registrations_mode').and_return('open')
  70. expect(helper.open_registrations?).to be true
  71. expect(Setting).to have_received(:[]).with('registrations_mode')
  72. end
  73. it 'returns false when closed for registrations' do
  74. allow(Setting).to receive(:[]).with('registrations_mode').and_return('none')
  75. expect(helper.open_registrations?).to be false
  76. expect(Setting).to have_received(:[]).with('registrations_mode')
  77. end
  78. end
  79. describe 'show_landing_strip?', :without_verify_partial_doubles do
  80. describe 'when signed in' do
  81. before do
  82. allow(helper).to receive(:user_signed_in?).and_return(true)
  83. end
  84. it 'does not show landing strip' do
  85. expect(helper.show_landing_strip?).to be false
  86. end
  87. end
  88. describe 'when signed out' do
  89. before do
  90. allow(helper).to receive(:user_signed_in?).and_return(false)
  91. end
  92. it 'does not show landing strip on single user instance' do
  93. allow(helper).to receive(:single_user_mode?).and_return(true)
  94. expect(helper.show_landing_strip?).to be false
  95. end
  96. it 'shows landing strip on multi user instance' do
  97. allow(helper).to receive(:single_user_mode?).and_return(false)
  98. expect(helper.show_landing_strip?).to be true
  99. end
  100. end
  101. end
  102. describe 'available_sign_up_path' do
  103. context 'when registrations are closed' do
  104. before do
  105. allow(Setting).to receive(:[]).with('registrations_mode').and_return 'none'
  106. end
  107. it 'redirects to joinmastodon site' do
  108. expect(helper.available_sign_up_path).to match(/joinmastodon.org/)
  109. end
  110. end
  111. context 'when in omniauth only mode' do
  112. around do |example|
  113. ClimateControl.modify OMNIAUTH_ONLY: 'true' do
  114. example.run
  115. end
  116. end
  117. it 'redirects to joinmastodon site' do
  118. expect(helper.available_sign_up_path).to match(/joinmastodon.org/)
  119. end
  120. end
  121. context 'when registrations are allowed' do
  122. it 'returns a link to the registration page' do
  123. expect(helper.available_sign_up_path).to eq(new_user_registration_path)
  124. end
  125. end
  126. end
  127. describe 'omniauth_only?' do
  128. context 'when env var is set to true' do
  129. around do |example|
  130. ClimateControl.modify OMNIAUTH_ONLY: 'true' do
  131. example.run
  132. end
  133. end
  134. it 'returns true' do
  135. expect(helper).to be_omniauth_only
  136. end
  137. end
  138. context 'when env var is not set' do
  139. around do |example|
  140. ClimateControl.modify OMNIAUTH_ONLY: nil do
  141. example.run
  142. end
  143. end
  144. it 'returns false' do
  145. expect(helper).to_not be_omniauth_only
  146. end
  147. end
  148. end
  149. describe 'quote_wrap' do
  150. it 'indents and quote wraps text' do
  151. text = <<~TEXT
  152. Hello this is a nice message for you to quote.
  153. Be careful because it has two lines.
  154. TEXT
  155. expect(helper.quote_wrap(text)).to eq <<~EXPECTED.strip
  156. > Hello this is a nice message for you to quote.
  157. > Be careful because it has two lines.
  158. EXPECTED
  159. end
  160. end
  161. describe 'storage_host' do
  162. context 'when S3 alias is present' do
  163. around do |example|
  164. ClimateControl.modify S3_ALIAS_HOST: 's3.alias' do
  165. example.run
  166. end
  167. end
  168. it 'returns true' do
  169. expect(helper.storage_host).to eq('https://s3.alias')
  170. end
  171. end
  172. context 'when S3 alias includes a path component' do
  173. around do |example|
  174. ClimateControl.modify S3_ALIAS_HOST: 's3.alias/path' do
  175. example.run
  176. end
  177. end
  178. it 'returns a correct URL' do
  179. expect(helper.storage_host).to eq('https://s3.alias/path')
  180. end
  181. end
  182. context 'when S3 cloudfront is present' do
  183. around do |example|
  184. ClimateControl.modify S3_CLOUDFRONT_HOST: 's3.cloudfront' do
  185. example.run
  186. end
  187. end
  188. it 'returns true' do
  189. expect(helper.storage_host).to eq('https://s3.cloudfront')
  190. end
  191. end
  192. end
  193. describe 'storage_host?' do
  194. context 'when S3 alias is present' do
  195. around do |example|
  196. ClimateControl.modify S3_ALIAS_HOST: 's3.alias' do
  197. example.run
  198. end
  199. end
  200. it 'returns true' do
  201. expect(helper.storage_host?).to be true
  202. end
  203. end
  204. context 'when S3 cloudfront is present' do
  205. around do |example|
  206. ClimateControl.modify S3_CLOUDFRONT_HOST: 's3.cloudfront' do
  207. example.run
  208. end
  209. end
  210. it 'returns true' do
  211. expect(helper.storage_host?).to be true
  212. end
  213. end
  214. context 'when neither env value is present' do
  215. it 'returns false' do
  216. expect(helper.storage_host?).to be false
  217. end
  218. end
  219. end
  220. describe 'visibility_icon' do
  221. it 'returns a globe icon for a public visible status' do
  222. result = helper.visibility_icon Status.new(visibility: 'public')
  223. expect(result).to match(/globe/)
  224. end
  225. it 'returns an unlock icon for a unlisted visible status' do
  226. result = helper.visibility_icon Status.new(visibility: 'unlisted')
  227. expect(result).to match(/unlock/)
  228. end
  229. it 'returns a lock icon for a private visible status' do
  230. result = helper.visibility_icon Status.new(visibility: 'private')
  231. expect(result).to match(/lock/)
  232. end
  233. it 'returns an at icon for a direct visible status' do
  234. result = helper.visibility_icon Status.new(visibility: 'direct')
  235. expect(result).to match(/at/)
  236. end
  237. end
  238. describe 'title' do
  239. around do |example|
  240. site_title = Setting.site_title
  241. example.run
  242. Setting.site_title = site_title
  243. end
  244. it 'returns site title on production environment' do
  245. Setting.site_title = 'site title'
  246. allow(Rails.env).to receive(:production?).and_return(true)
  247. expect(helper.title).to eq 'site title'
  248. expect(Rails.env).to have_received(:production?)
  249. end
  250. it 'returns site title with note on non-production environment' do
  251. Setting.site_title = 'site title'
  252. allow(Rails.env).to receive(:production?).and_return(false)
  253. expect(helper.title).to eq 'site title (Dev)'
  254. expect(Rails.env).to have_received(:production?)
  255. end
  256. end
  257. describe 'html_title' do
  258. before do
  259. allow(Rails.env).to receive(:production?).and_return(true)
  260. end
  261. around do |example|
  262. site_title = Setting.site_title
  263. example.run
  264. Setting.site_title = site_title
  265. end
  266. context 'with a page_title content_for value' do
  267. it 'uses the value in the html title' do
  268. Setting.site_title = 'Site Title'
  269. helper.content_for(:page_title, 'Test Value')
  270. expect(helper.html_title).to eq 'Test Value - Site Title'
  271. expect(helper.html_title).to be_html_safe
  272. end
  273. it 'removes extra new lines' do
  274. Setting.site_title = 'Site Title'
  275. helper.content_for(:page_title, "Test Value\n")
  276. expect(helper.html_title).to eq 'Test Value - Site Title'
  277. expect(helper.html_title).to be_html_safe
  278. end
  279. end
  280. context 'without any page_title content_for value' do
  281. it 'returns the site title' do
  282. Setting.site_title = 'Site Title'
  283. expect(helper.html_title).to eq 'Site Title'
  284. expect(helper.html_title).to be_html_safe
  285. end
  286. end
  287. end
  288. end