|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe BetterTogether::NavigationItem, type: :model do |
| 6 | + let(:navigation_area) { create(:navigation_area) } |
| 7 | + |
| 8 | + context 'title fallbacks' do |
| 9 | + it 'returns nav item translation when present' do |
| 10 | + nav = described_class.build(navigation_area:, title: 'Nav Title', slug: 'nav-title', visible: true) |
| 11 | + |
| 12 | + expect(nav.title).to eq('Nav Title') |
| 13 | + end |
| 14 | + |
| 15 | + it 'falls back to linkable title when nav item title blank and linkable present' do |
| 16 | + page = create(:page, title: 'Page Title') |
| 17 | + nav = described_class.build(navigation_area:, title: '', slug: 'nav-title', visible: true, linkable: page) |
| 18 | + |
| 19 | + expect(nav.title).to eq('Page Title') |
| 20 | + end |
| 21 | + |
| 22 | + it 'returns blank when nav item title blank and no linkable' do |
| 23 | + nav = described_class.build(navigation_area:, title: '', slug: 'nav-title', visible: true) |
| 24 | + |
| 25 | + expect(nav.title).to be_blank |
| 26 | + end |
| 27 | + |
| 28 | + it 'prefers linkable title when set' do |
| 29 | + page = create(:page, title: 'Page Title') |
| 30 | + nav = described_class.build(navigation_area:, title: 'Nav Title', slug: 'nav-title', visible: true, |
| 31 | + linkable: page) |
| 32 | + |
| 33 | + expect(nav.title).to eq('Page Title') |
| 34 | + end |
| 35 | + |
| 36 | + it 'returns translation for requested locale when available' do |
| 37 | + I18n.with_locale(:es) do |
| 38 | + nav = described_class.build(navigation_area:, title: 'Título Nav', slug: 'nav-title', visible: true) |
| 39 | + |
| 40 | + expect(nav.title(locale: :es)).to eq('Título Nav') |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + it 'falls back to linkable translation for a missing nav translation' do |
| 45 | + page = create(:page) |
| 46 | + # set page spanish title |
| 47 | + page.public_send(:title=, 'Título Página', locale: :es) |
| 48 | + |
| 49 | + nav = described_class.build(navigation_area:, title: '', slug: 'nav-title', visible: true, linkable: page) |
| 50 | + |
| 51 | + I18n.with_locale(:es) do |
| 52 | + expect(nav.title(locale: :es)).to eq('Título Página') |
| 53 | + end |
| 54 | + end |
| 55 | + end |
| 56 | +end |
| 57 | +# frozen_string_literal: true |
| 58 | + |
3 | 59 | # spec/models/better_together/navigation_item_spec.rb |
4 | 60 |
|
5 | 61 | require 'rails_helper' |
|
0 commit comments