Files
nairobi/spec/models/legislation/process_spec.rb
Javi Martín 1118c732f1 Bump acts-as-taggable-on to 6.0.0
Rails 5.1 introduced certain changes in the way a record is touched when
the counter cache option is enabled in a belongs to association.

We need to upgrade acts-as-taggable-on so it keeps changing the
`updated_at` attribute when a new tag is added to a record.

Note we now need to reload the records in some cases to get the
`context_tag_list` method to return what we expect. Methods like
`context_tags` however work properly with no need to reload the record.
2020-04-23 18:49:43 +02:00

226 lines
8.3 KiB
Ruby

require "rails_helper"
describe Legislation::Process do
let(:process) { create(:legislation_process) }
it_behaves_like "acts as paranoid", :legislation_process
it_behaves_like "globalizable", :legislation_process
it "is valid" do
expect(process).to be_valid
end
describe "dates validations" do
it "is invalid if debate_start_date is present but debate_end_date is not" do
process = build(:legislation_process, debate_start_date: Date.current, debate_end_date: "")
expect(process).to be_invalid
expect(process.errors.messages[:debate_end_date]).to include("can't be blank")
end
it "is invalid if debate_end_date is present but debate_start_date is not" do
process = build(:legislation_process, debate_start_date: nil, debate_end_date: Date.current)
expect(process).to be_invalid
expect(process.errors.messages[:debate_start_date]).to include("can't be blank")
end
it "is invalid if allegations_start_date is present but debate_end_date is not" do
process = build(:legislation_process, allegations_start_date: Date.current,
allegations_end_date: "")
expect(process).to be_invalid
expect(process.errors.messages[:allegations_end_date]).to include("can't be blank")
end
it "is invalid if debate_end_date is present but allegations_start_date is not" do
process = build(:legislation_process, allegations_start_date: nil,
allegations_end_date: Date.current)
expect(process).to be_invalid
expect(process.errors.messages[:allegations_start_date]).to include("can't be blank")
end
end
describe "date ranges validations" do
it "is invalid if end_date is before start_date" do
process = build(:legislation_process, start_date: Date.current,
end_date: Date.current - 1.day)
expect(process).to be_invalid
expect(process.errors.messages[:end_date]).to include("must be on or after the start date")
end
it "is valid if end_date is the same as start_date" do
process = build(:legislation_process, start_date: Date.current - 1.day,
end_date: Date.current - 1.day)
expect(process).to be_valid
end
it "is valid if debate_end_date is the same as debate_start_date" do
process = build(:legislation_process, debate_start_date: Date.current - 1.day,
debate_end_date: Date.current - 1.day)
expect(process).to be_valid
end
it "is invalid if debate_end_date is before debate_start_date" do
process = build(:legislation_process, debate_start_date: Date.current,
debate_end_date: Date.current - 1.day)
expect(process).to be_invalid
expect(process.errors.messages[:debate_end_date])
.to include("must be on or after the debate start date")
end
it "is valid if draft_end_date is the same as draft_start_date" do
process = build(:legislation_process, draft_start_date: Date.current - 1.day,
draft_end_date: Date.current - 1.day)
expect(process).to be_valid
end
it "is invalid if draft_end_date is before draft_start_date" do
process = build(:legislation_process, draft_start_date: Date.current,
draft_end_date: Date.current - 1.day)
expect(process).to be_invalid
expect(process.errors.messages[:draft_end_date])
.to include("must be on or after the draft start date")
end
it "is invalid if allegations_end_date is before allegations_start_date" do
process = build(:legislation_process, allegations_start_date: Date.current,
allegations_end_date: Date.current - 1.day)
expect(process).to be_invalid
expect(process.errors.messages[:allegations_end_date])
.to include("must be on or after the comments start date")
end
it "is valid if allegations_end_date is the same as allegations_start_date" do
process = build(:legislation_process, allegations_start_date: Date.current - 1.day,
allegations_end_date: Date.current - 1.day)
expect(process).to be_valid
end
end
describe "filter scopes" do
describe "open and past filters" do
let!(:process_1) do
create(:legislation_process, start_date: Date.current - 2.days, end_date: Date.current + 1.day)
end
let!(:process_2) do
create(:legislation_process, start_date: Date.current + 1.day, end_date: Date.current + 3.days)
end
let!(:process_3) do
create(:legislation_process, start_date: Date.current - 4.days, end_date: Date.current - 3.days)
end
it "filters open" do
open_processes = ::Legislation::Process.open
expect(open_processes).to eq [process_1]
expect(open_processes).not_to include [process_2]
end
it "filters past" do
past_processes = ::Legislation::Process.past
expect(past_processes).to eq [process_3]
end
end
it "filters draft phase" do
process_before_draft = create(
:legislation_process,
draft_start_date: Date.current - 3.days,
draft_end_date: Date.current - 2.days
)
process_with_draft_disabled = create(
:legislation_process,
draft_start_date: Date.current - 2.days,
draft_end_date: Date.current + 2.days,
draft_phase_enabled: false
)
process_with_draft_enabled = create(
:legislation_process,
draft_start_date: Date.current - 2.days,
draft_end_date: Date.current + 2.days,
draft_phase_enabled: true
)
process_with_draft_only_today = create(
:legislation_process,
draft_start_date: Date.current,
draft_end_date: Date.current,
draft_phase_enabled: true
)
processes_not_in_draft = ::Legislation::Process.not_in_draft
expect(processes_not_in_draft).to match_array [process_before_draft, process_with_draft_disabled]
expect(processes_not_in_draft).not_to include(process_with_draft_enabled)
expect(processes_not_in_draft).not_to include(process_with_draft_only_today)
end
end
describe "#status" do
it "detects planned phase" do
process.update!(start_date: Date.current + 2.days)
expect(process.status).to eq(:planned)
end
it "detects closed phase" do
process.update!(end_date: Date.current - 2.days)
expect(process.status).to eq(:closed)
end
it "detects open phase" do
expect(process.status).to eq(:open)
end
end
describe "Header colors" do
it "valid format colors" do
process1 = create(:legislation_process, background_color: "123", font_color: "#fff")
process2 = create(:legislation_process, background_color: "#fff", font_color: "123")
process3 = create(:legislation_process, background_color: "", font_color: "")
process4 = create(:legislation_process, background_color: "#abf123", font_color: "fff123")
expect(process1).to be_valid
expect(process2).to be_valid
expect(process3).to be_valid
expect(process4).to be_valid
end
it "invalid format colors" do
expect do
create(:legislation_process, background_color: "#123ghi", font_color: "#fff")
end.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Background color is invalid")
expect do
create(:legislation_process, background_color: "#fff", font_color: "ggg")
end.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Font color is invalid")
end
end
describe "milestone_tags" do
context "without milestone_tags" do
let(:process) { create(:legislation_process) }
it "do not have milestone_tags" do
expect(process.milestone_tag_list).to eq([])
expect(process.milestone_tags).to eq([])
end
it "add a new milestone_tag" do
process.milestone_tag_list = "tag1,tag2"
expect(process.milestone_tag_list).to eq(["tag1", "tag2"])
end
end
context "with milestone_tags" do
let(:process) { create(:legislation_process, :with_milestone_tags) }
it "has milestone_tags" do
expect(process.reload.milestone_tag_list.count).to eq(1)
end
end
end
end