Skip to content

Commit

Permalink
improve test coverage and fix rubocop violations
Browse files Browse the repository at this point in the history
  • Loading branch information
markburns committed Jul 3, 2024
1 parent 0c9f96d commit c2c6303
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 41 deletions.
3 changes: 1 addition & 2 deletions lib/interactify/async/job_maker.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "interactify/async/job_klass"
require "interactify/async/null_job"

module Interactify
module Async
Expand All @@ -23,7 +22,7 @@ def job_klass
private

def define_job_klass
return NullJob if Interactify.sidekiq_missing?
return if Interactify.sidekiq_missing?

this = self

Expand Down
2 changes: 2 additions & 0 deletions lib/interactify/async/jobable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module Jobable
next if Interactify.sidekiq_missing?

def base.inherited(klass)
return if Interactify.sidekiq_missing?

super_klass = klass.superclass
super_job = super_klass::Job # really spiffing

Expand Down
23 changes: 0 additions & 23 deletions lib/interactify/async/null_job.rb

This file was deleted.

2 changes: 2 additions & 0 deletions lib/interactify/configure.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Interactify
module Configure
def validate_app(ignore: [])
Expand Down
2 changes: 2 additions & 0 deletions lib/interactify/contracts/breaches.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Interactify
module Breaches
def self.handle_with_failure(context, breaches)
Expand Down
10 changes: 2 additions & 8 deletions lib/interactify/dependency_inference.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Interactify
class << self
delegate :on_definition_error, :trigger_definition_error, to: :configuration
Expand All @@ -10,10 +12,6 @@ def railties_missing!
@railties_missing = true
end

def railties
railties?
end

def railties?
!railties_missing?
end
Expand All @@ -26,10 +24,6 @@ def sidekiq_missing!
@sidekiq_missing = true
end

def sidekiq
sidekiq?
end

def sidekiq?
!sidekiq_missing?
end
Expand Down
2 changes: 2 additions & 0 deletions lib/interactify/hooks.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Interactify
module Hooks
def reset
Expand Down
4 changes: 3 additions & 1 deletion spec/integration/all_the_things_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
before do
load_interactify_fixtures("each")
load_interactify_fixtures("if/")
load "./spec/fixtures/integration_app/app/interactors/all_the_things.rb"
silence_warnings do
load "./spec/fixtures/integration_app/app/interactors/all_the_things.rb"
end
end

context "without an optional thing" do
Expand Down
4 changes: 3 additions & 1 deletion spec/integration/each_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
before do
files = Dir.glob("./spec/fixtures/integration_app/app/interactors/each/**/*.rb")
files.each do |file|
require file
silence_warnings do
load file
end
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/lib/interactify/async/job_maker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

describe "concerning JobClass" do
describe "#job_klass" do
if Interactify.sidekiq
if Interactify.sidekiq?
it "returns a job class" do
job_klass = subject.job_klass

Expand All @@ -43,16 +43,16 @@
expect(job_klass.included_modules).to include(Sidekiq::Job)
end
else
it "returns a null job_klass" do
it "returns nil" do
job_klass = subject.job_klass

expect(job_klass).to eq(Interactify::Async::NullJob)
expect(job_klass).to eq(nil)
end
end
end
end

if Interactify.sidekiq
if Interactify.sidekiq?
describe "concerning JobClass" do
describe "#job_klass" do
it "job class includes JOBABLE_OPTS constant" do
Expand Down
139 changes: 138 additions & 1 deletion spec/lib/interactify/wiring_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
end

before do
Dir.glob("#{root}**/*.rb").each { |f| require(f) }
Dir.glob("#{root}**/*.rb").each { |f| silence_warnings { require(f) } }
end

def f(path)
Expand Down Expand Up @@ -82,4 +82,141 @@ def f(path)
end
end
end

describe "#ignore_klass?" do
self::DummyInteractor = Class.new do
include Interactify
end

let(:klass) { self.class::DummyInteractor }
let(:wiring) { described_class.new(root:, ignore:) }
let(:result) { wiring.send(:ignore_klass?, wiring.ignore, klass) }

def self.it_ignores
it "ignores" do
expect(result).to be true
end
end

context "with an array of classes" do
let(:ignore) { [klass] }

it_ignores
end

context "with a regexp" do
let(:ignore) { [/Dummy/] }

it_ignores
end

context "with a string" do
let(:ignore) { ["DummyInteractor"] }

it_ignores
end

context "proc condition" do
let(:ignore) { [->(k) { k.to_s =~ /DummyInteractor/ }] }

it_ignores
end

context "empty ignore" do
let(:ignore) { [] }

it "does not ignore class" do
expect(result).to be false
end
end
end
context "with errors" do
let(:organizer1) { double("Organizer1", klass: "SomeOrganizer1") }
let(:organizer2) { double("Organizer2", klass: "SomeOrganizer2") }

let(:interactor1) { double("Interactor1", klass: "SomeInteractor1") }
let(:interactor2) { double("Interactor2", klass: "SomeInteractor2") }
let(:interactor3) { double("Interactor3", klass: "SomeInteractor3") }

describe "#format_error" do
let(:missing_keys) { %i[foo bar] }
let(:interactor) { double("Interactor", klass: "SomeInteractor") }
let(:organizer) { double("Organizer", klass: "SomeOrganizer") }
let(:formatted_errors) { [] }

it "formats the error message correctly" do
subject.send(:format_error, missing_keys, interactor, organizer, formatted_errors)
expect(formatted_errors.first).to include("Missing keys: :foo, :bar")
expect(formatted_errors.first).to include("expected in: SomeInteractor")
expect(formatted_errors.first).to include("called by: SomeOrganizer")
end
end

describe "#format_errors" do
it "formats and combines all errors" do
all_errors = {
organizer1 => double(
"ErrorContext",
missing_keys: {
interactor1 => %i[key1 key2]
}
),
organizer2 => double(
"ErrorContext",
missing_keys: {
interactor2 => [:key3]
}
)
}

expect(subject).to receive(:format_error).twice.and_call_original
formatted_error_string = subject.format_errors(all_errors)
expect(formatted_error_string).to include("Missing keys: :key1, :key2")
expect(formatted_error_string).to include("Missing keys: :key3")
expect(formatted_error_string).to match(/expected in:.*\n\s+called by:/)
end
end

describe "#each_error" do
it "yields each error unless the class is ignored" do
all_errors = {
organizer1 => double(
"ErrorContext",
missing_keys: {
interactor1 => [:key1],
interactor2 => [:key2]
}
),
organizer2 => double(
"ErrorContext",
missing_keys: {
interactor3 => [:key3]
}
)
}

allow(subject).to receive(:ignore_klass?).and_return(false)

expect { |b| subject.each_error(all_errors, &b) }
.to yield_successive_args(
[[:key1], anything, anything],
[[:key2], anything, anything],
[[:key3], anything, anything]
)
end

it "does not yield errors for ignored classes" do
all_errors = {
organizer1 => double(
"ErrorContext",
missing_keys: {
interactor1 => [:key1]
}
)
}
allow(subject).to receive(:ignore_klass?).and_return(true)
expect { |b| subject.each_error(all_errors, &b) }.not_to yield_control
end
end
end
end
2 changes: 1 addition & 1 deletion spec/support/spec_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def load_interactify_fixtures(sub_directory)
files = Dir.glob("./spec/fixtures/integration_app/app/interactors/#{sub_directory}/**/*.rb")

files.each do |file|
load file
silence_warnings { load file }
end
end
end
Expand Down

0 comments on commit c2c6303

Please sign in to comment.