require 'rails_helper'
RSpec.describe ClosePetitionsEarlyJob, type: :job do
let(:dissolution_at) { "2017-05-02T23:00:01Z".in_time_zone }
let(:open_at) { dissolution_at - 4.weeks }
let(:scheduled_at) { dissolution_at - 2.weeks }
let(:before_dissolution) { dissolution_at - 1.week }
let(:job) { Delayed::Job.last }
let(:jobs) { Delayed::Job.all.to_a }
let!(:petition) { FactoryBot.create(:open_petition, open_at: open_at) }
before do
ActiveJob::Base.queue_adapter = :delayed_job
travel_to(scheduled_at) {
described_class.schedule_for(dissolution_at)
}
end
after do
ActiveJob::Base.queue_adapter = :test
end
it "enqueues the job" do
expect(jobs).to eq([job])
end
context "before the scheduled date" do -
it "doesn't perform the enqueued job" do
expect {
travel_to(before_dissolution) {
Delayed::Worker.new.work_off
}
}.not_to change {
petition.reload.state
}
end
end
context "after the scheduled date" do -
it "closes the petition" do - context(after the scheduled date)::it#closes the petition has a flog score of 25
expect {
travel_to(dissolution_at) {
Delayed::Worker.new.work_off
}
}.to change {
petition.reload.state
}.from("open").to("closed")
end
it "sets the closed_at to the correct timestamp" do - context(after the scheduled date)::it#sets the closed_at to the correct timestamp has a flog score of 26
expect {
travel_to(dissolution_at) {
Delayed::Worker.new.work_off
}
}.to change {
petition.reload.closed_at
}.from(nil).to(dissolution_at)
end
end
end