123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- # frozen_string_literal: true
- require 'rails_helper'
- RSpec.describe ApplicationHelper do
- describe 'body_classes' do
- context 'with a body class string from a controller' do
- before { helper.extend controller_helpers }
- it 'uses the controller body classes in the result' do
- expect(helper.body_classes)
- .to match(/modal-layout compose-standalone/)
- .and match(/theme-default/)
- end
- it 'includes values set via content_for' do
- helper.content_for(:body_classes) { 'admin' }
- expect(helper.body_classes)
- .to match(/admin/)
- end
- private
- def controller_helpers
- Module.new do
- def body_class_string = 'modal-layout compose-standalone'
- def current_account
- @current_account ||= Fabricate(:account)
- end
- def current_theme = 'default'
- end
- end
- end
- end
- describe 'locale_direction' do
- it 'adds rtl body class if locale is Arabic' do
- I18n.with_locale(:ar) do
- expect(helper.locale_direction).to eq 'rtl'
- end
- end
- it 'adds rtl body class if locale is Farsi' do
- I18n.with_locale(:fa) do
- expect(helper.locale_direction).to eq 'rtl'
- end
- end
- it 'adds rtl if locale is Hebrew' do
- I18n.with_locale(:he) do
- expect(helper.locale_direction).to eq 'rtl'
- end
- end
- it 'does not add rtl if locale is Thai' do
- I18n.with_locale(:th) do
- expect(helper.locale_direction).to_not eq 'rtl'
- end
- end
- end
- describe '#material_symbol' do
- it 'returns an svg with the icon and options' do
- expect(helper.material_symbol('lock', class: :test, data: { hidden: true }))
- .to match('<svg.*/svg>')
- .and match('class="icon material-lock test"')
- .and match('data-hidden="true"')
- end
- end
- describe 'open_registrations?' do
- it 'returns true when open for registrations' do
- allow(Setting).to receive(:[]).with('registrations_mode').and_return('open')
- expect(helper.open_registrations?).to be true
- expect(Setting).to have_received(:[]).with('registrations_mode')
- end
- it 'returns false when closed for registrations' do
- allow(Setting).to receive(:[]).with('registrations_mode').and_return('none')
- expect(helper.open_registrations?).to be false
- expect(Setting).to have_received(:[]).with('registrations_mode')
- end
- end
- describe 'available_sign_up_path' do
- context 'when registrations are closed' do
- before do
- allow(Setting).to receive(:[]).with('registrations_mode').and_return 'none'
- end
- it 'redirects to joinmastodon site' do
- expect(helper.available_sign_up_path).to match(/joinmastodon.org/)
- end
- end
- context 'when in omniauth only mode' do
- around do |example|
- ClimateControl.modify OMNIAUTH_ONLY: 'true' do
- example.run
- end
- end
- it 'redirects to joinmastodon site' do
- expect(helper.available_sign_up_path).to match(/joinmastodon.org/)
- end
- end
- context 'when registrations are allowed' do
- it 'returns a link to the registration page' do
- expect(helper.available_sign_up_path).to eq(new_user_registration_path)
- end
- end
- end
- describe 'omniauth_only?' do
- context 'when env var is set to true' do
- around do |example|
- ClimateControl.modify OMNIAUTH_ONLY: 'true' do
- example.run
- end
- end
- it 'returns true' do
- expect(helper).to be_omniauth_only
- end
- end
- context 'when env var is not set' do
- around do |example|
- ClimateControl.modify OMNIAUTH_ONLY: nil do
- example.run
- end
- end
- it 'returns false' do
- expect(helper).to_not be_omniauth_only
- end
- end
- end
- describe 'quote_wrap' do
- it 'indents and quote wraps text' do
- text = <<~TEXT
- Hello this is a nice message for you to quote.
- Be careful because it has two lines.
- TEXT
- expect(helper.quote_wrap(text)).to eq <<~EXPECTED.strip
- > Hello this is a nice message for you to quote.
- > Be careful because it has two lines.
- EXPECTED
- end
- end
- describe 'storage_host' do
- context 'when S3 alias is present' do
- around do |example|
- ClimateControl.modify S3_ALIAS_HOST: 's3.alias' do
- example.run
- end
- end
- it 'returns true' do
- expect(helper.storage_host).to eq('https://s3.alias')
- end
- end
- context 'when S3 alias includes a path component' do
- around do |example|
- ClimateControl.modify S3_ALIAS_HOST: 's3.alias/path' do
- example.run
- end
- end
- it 'returns a correct URL' do
- expect(helper.storage_host).to eq('https://s3.alias/path')
- end
- end
- context 'when S3 cloudfront is present' do
- around do |example|
- ClimateControl.modify S3_CLOUDFRONT_HOST: 's3.cloudfront' do
- example.run
- end
- end
- it 'returns true' do
- expect(helper.storage_host).to eq('https://s3.cloudfront')
- end
- end
- end
- describe 'storage_host?' do
- context 'when S3 alias is present' do
- around do |example|
- ClimateControl.modify S3_ALIAS_HOST: 's3.alias' do
- example.run
- end
- end
- it 'returns true' do
- expect(helper.storage_host?).to be true
- end
- end
- context 'when S3 cloudfront is present' do
- around do |example|
- ClimateControl.modify S3_CLOUDFRONT_HOST: 's3.cloudfront' do
- example.run
- end
- end
- it 'returns true' do
- expect(helper.storage_host?).to be true
- end
- end
- context 'when neither env value is present' do
- it 'returns false' do
- expect(helper.storage_host?).to be false
- end
- end
- end
- describe 'title' do
- it 'returns site title on production environment' do
- Setting.site_title = 'site title'
- allow(Rails.env).to receive(:production?).and_return(true)
- expect(helper.title).to eq 'site title'
- expect(Rails.env).to have_received(:production?)
- end
- it 'returns site title with note on non-production environment' do
- Setting.site_title = 'site title'
- allow(Rails.env).to receive(:production?).and_return(false)
- expect(helper.title).to eq 'site title (Dev)'
- expect(Rails.env).to have_received(:production?)
- end
- end
- describe 'html_title' do
- before do
- allow(Rails.env).to receive(:production?).and_return(true)
- end
- context 'with a page_title content_for value' do
- it 'uses the value in the html title' do
- Setting.site_title = 'Site Title'
- helper.content_for(:page_title, 'Test Value')
- expect(helper.html_title).to eq 'Test Value - Site Title'
- expect(helper.html_title).to be_html_safe
- end
- it 'does not escape twice' do
- Setting.site_title = 'Site Title'
- helper.content_for(:page_title, '"Test Value"'.html_safe)
- expect(helper.html_title).to eq '"Test Value" - Site Title'
- expect(helper.html_title).to be_html_safe
- end
- end
- context 'without any page_title content_for value' do
- it 'returns the site title' do
- Setting.site_title = 'Site Title'
- expect(helper.html_title).to eq 'Site Title'
- expect(helper.html_title).to be_html_safe
- end
- end
- end
- end
|