# frozen_string_literal: true

require "helper"
require "jobs/timezone_dependent_job"

class TimezonesTest < ActiveSupport::TestCase
  setup do
    JobBuffer.clear
  end

  test "it performs the job in the given timezone" do
    job = TimezoneDependentJob.new("2018-01-01T00:00:00Z")
    job.timezone = "London"
    job.perform_now

    assert_equal "Happy New Year!", JobBuffer.last_value

    job = TimezoneDependentJob.new("2018-01-01T00:00:00Z")
    job.timezone = "Eastern Time (US & Canada)"
    job.perform_now

    assert_equal "Just 5 hours to go", JobBuffer.last_value
  end

  test "perform_now passes down current timezone to the job" do
    Time.zone = "America/New_York"

    Time.use_zone("London") do
      job = TimezoneDependentJob.new("2018-01-01T00:00:00Z")
      assert_equal "London", job.timezone
      job.perform_now
    end

    assert_equal "Happy New Year!", JobBuffer.last_value
  end
end

# frozen_string_literal: true

require "helper"
require "jobs/timezone_dependent_job"

class TimezonesTest < ActiveSupport::TestCase
  setup do
    JobBuffer.clear
  end

  test "it performs the job in the given timezone" do
    job = TimezoneDependentJob.new("2018-01-01T00:00:00Z")
    job.timezone = "London"
    job.perform_now

    assert_equal "Happy New Year!", JobBuffer.last_value

    job = TimezoneDependentJob.new("2018-01-01T00:00:00Z")
    job.timezone = "Eastern Time (US & Canada)"
    job.perform_now

    assert_equal "Just 5 hours to go", JobBuffer.last_value
  end

  test "perform_now passes down current timezone to the job" do
    Time.zone = "America/New_York"

    Time.use_zone("London") do
      job = TimezoneDependentJob.new("2018-01-01T00:00:00Z")
      assert_equal "London", job.timezone
      job.perform_now
    end

    assert_equal "Happy New Year!", JobBuffer.last_value
  ensure
    Time.zone = nil
  end
end

Reset Time.zone to avoid leaking into other tests
Reset Time.zone to avoid leaking into other tests

https://buildkite.com/rails/rails/builds/72478#93358e11-6e26-4588-a791-26f9512157c2/1074-1087
https://buildkite.com/rails/rails/builds/72747#10657eba-2359-47ca-9914-49a48b3f2d3c/967-976
https://buildkite.com/rails/rails/builds/72795#3f4bff27-3f42-4678-b643-08cc811c8954/999-1012
