Files
grecia/spec/models/sdg/goal_spec.rb
Javi Martín c025fef50b Add short titles to SDG targets
Since targets didn't have a title but only a long description, every
form allowing to select targets was pretty much unusable: we either
displayed just the code or the whole description.

Now, with a concise title, it's easier to find and select the desired
target.

The titles have been copied from The Global Goals page [1].

Note we're using the `short_title` I18n key for the `title` method and
the `long_title` I18n key for the `long_title` method. We can't use
`title` as I18n key instead of `short_title` because it would affect
existing translations.

[1] https://www.globalgoals.org/
2021-10-01 16:19:10 +02:00

88 lines
2.2 KiB
Ruby

require "rails_helper"
describe SDG::Goal do
describe "validations" do
it "is valid with an existent code" do
goal = SDG::Goal[1]
expect(goal).to be_valid
end
it "is not valid without a code" do
expect(build(:sdg_goal, code: nil)).not_to be_valid
end
it "is not valid with a nonexistent code" do
[0, 18].each do |code|
goal = SDG::Goal.where(code: code).first_or_initialize
expect(goal).not_to be_valid
end
end
end
describe "#<=>" do
let(:goal) { SDG::Goal[10] }
it "can be compared against goals" do
lesser_goal = SDG::Goal[9]
greater_goal = SDG::Goal[11]
expect(goal).to be > lesser_goal
expect(goal).to be < greater_goal
end
it "can be compared against global targets" do
lesser_target = build(:sdg_target, code: "9.A", goal: SDG::Goal[9])
greater_target = build(:sdg_target, code: "10.1", goal: SDG::Goal[10])
expect(goal).to be > lesser_target
expect(goal).to be < greater_target
end
it "can be compared against local targets" do
lesser_local_target = build(:sdg_local_target, code: "9.B.12")
greater_local_target = build(:sdg_local_target, code: "10.1.4")
expect(goal).to be > lesser_local_target
expect(goal).to be < greater_local_target
end
end
describe ".[]" do
it "finds existing goals by code" do
expect(SDG::Goal[1].code).to be 1
end
it "raises an exception for non-existing codes" do
expect { SDG::Goal[100] }.to raise_exception ActiveRecord::RecordNotFound
end
end
it "translates title" do
goal = SDG::Goal[1]
expect(goal.title).to eq "No Poverty"
I18n.with_locale(:es) do
expect(goal.title).to eq "Fin de la pobreza"
end
end
it "translates description" do
goal = SDG::Goal[1]
expect(goal.description).to eq "End poverty in all its forms, everywhere."
I18n.with_locale(:es) do
expect(goal.description).to eq "Poner fin a la pobreza en todas sus formas en todo el mundo."
end
end
describe "#long_title" do
it "returns the title" do
expect(SDG::Goal[1].long_title).to eq "No Poverty"
end
end
end