docbrown/spec/models/navigation_link_spec.rb
Michael Kohl 5d0fe12528
Update NavigationLink handling (#12316)
* Update copy

* Allow relative URLs and normalize on save

* Update specs

* Move clarification from index to form

* Add data update script

* Update spec
2021-01-20 13:57:59 -05:00

55 lines
1.5 KiB
Ruby

require "rails_helper"
RSpec.describe NavigationLink, type: :model do
let(:navigation_link) { create(:navigation_link) }
describe "validations" do
describe "presence validations" do
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_presence_of(:url) }
it { is_expected.to validate_presence_of(:icon) }
end
it "validates the icon" do
navigation_link.icon = "test.png"
expect(navigation_link).not_to be_valid
end
context "when validating the URL" do
it "does not allow invalid URLs" do
navigation_link.url = "test"
expect(navigation_link).not_to be_valid
end
it "does allow relative URLs" do
navigation_link.url = "/test"
expect(navigation_link).to be_valid
end
end
end
describe "callbacks" do
let(:base_url) { "https://testforem.com" }
before { allow(URL).to receive(:url).and_return(base_url) }
it "normalizes local URLs to relative URLs on save" do
navigation_link.url = "#{base_url}/test"
navigation_link.save
expect(navigation_link.url).to eq "/test"
end
it "persists relative URLs unchanged" do
navigation_link.url = "/test"
navigation_link.save
expect(navigation_link.url).to eq "/test"
end
it "persists external URLs unchanged" do
url = "https://example.com/test"
navigation_link.url = url
navigation_link.save
expect(navigation_link.url).to eq url
end
end
end