Repository: rails/rails
File: activestorage/test/jobs/create_variants_job_test.rb (Ruby)

Code before:
    @transformations = [{ resize_to_limit: [ 100, 100 ] }, { resize_to_limit: [ 200, 200 ] }]
  end

  test "creates preview when previewable" do
    blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")

    assert_changes -> { blob.preview_image.attached? }, from: false, to: true do
      ActiveStorage::CreateVariantsJob.perform_now blob, generate: :delayed, transformations: @transformations
    end
  end

  test "enqueues individual transform jobs for each transformation when :delayed" do
    blob = create_file_blob
    ActiveStorage::CreateVariantsJob.perform_now blob, generate: :delayed, transformations: @transformations

    @transformations.each do |transformation|
      assert_enqueued_with job: ActiveStorage::TransformJob, args: [ blob, transformation ]
    end
  end

  test "does not transform when :delayed" do
    blob = create_file_blob
    ActiveStorage::CreateVariantsJob.perform_now blob, generate: :delayed, transformations: @transformations

    @transformations.each do |transformation|
      assert_not blob.variant(transformation).send(:processed?)
    end
  end

  test "performs transformations immediately when :immediate" do
    blob = create_file_blob
    ActiveStorage::CreateVariantsJob.perform_now blob, generate: :immediate, transformations: @transformations

    @transformations.each do |transformation|
      assert blob.variant(transformation).send(:processed?)
    end
  end
end


Review comment (suggestion):
I didn't include in this PR, but I think we should consider moving `processed?` to a public method. Not only is it used in testing, but our app uses it as a conditional to determine whether to display a variant on highly requested pages.

Code after:
    @variants = [{ resize_to_limit: [ 100, 100 ] }, { resize_to_limit: [ 200, 200 ] }]
  end

  test "creates preview when previewable" do
    blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")

    assert_changes -> { blob.preview_image.attached? }, from: false, to: true do
      ActiveStorage::CreateVariantsJob.perform_now blob, variants: @variants, process: :later
    end
  end

  test "enqueues individual transform jobs for each transformation when process: :later" do
    blob = create_file_blob
    ActiveStorage::CreateVariantsJob.perform_now blob, variants: @variants, process: :later

    @variants.each do |transformations|
      assert_enqueued_with job: ActiveStorage::TransformJob, args: [ blob, transformations ]
    end
  end

  test "does not transform when process: :later" do
    blob = create_file_blob
    ActiveStorage::CreateVariantsJob.perform_now blob, variants: @variants, process: :later

    @variants.each do |transformations|
      assert_not blob.variant(transformations).processed?
    end
  end

  test "performs transformations immediately when process: :immediately" do
    blob = create_file_blob
    ActiveStorage::CreateVariantsJob.perform_now blob, variants: @variants, process: :immediately

    @variants.each do |transformations|
      assert blob.variant(transformations).processed?
    end
  end
end