Skip to content

Commit 0d0a054

Browse files
authored
improve test coverage and fix rubocop violations (#39)
* improve test coverage and fix rubocop violations * use predicate method
1 parent 0c9f96d commit 0d0a054

File tree

12 files changed

+161
-42
lines changed

12 files changed

+161
-42
lines changed

lib/interactify/async/job_maker.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

33
require "interactify/async/job_klass"
4-
require "interactify/async/null_job"
54

65
module Interactify
76
module Async
@@ -23,7 +22,7 @@ def job_klass
2322
private
2423

2524
def define_job_klass
26-
return NullJob if Interactify.sidekiq_missing?
25+
return if Interactify.sidekiq_missing?
2726

2827
this = self
2928

lib/interactify/async/jobable.rb

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module Jobable
1515
next if Interactify.sidekiq_missing?
1616

1717
def base.inherited(klass)
18+
return if Interactify.sidekiq_missing?
19+
1820
super_klass = klass.superclass
1921
super_job = super_klass::Job # really spiffing
2022

lib/interactify/async/null_job.rb

-23
This file was deleted.

lib/interactify/configure.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Interactify
24
module Configure
35
def validate_app(ignore: [])

lib/interactify/contracts/breaches.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Interactify
24
module Breaches
35
def self.handle_with_failure(context, breaches)

lib/interactify/dependency_inference.rb

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Interactify
24
class << self
35
delegate :on_definition_error, :trigger_definition_error, to: :configuration
@@ -10,10 +12,6 @@ def railties_missing!
1012
@railties_missing = true
1113
end
1214

13-
def railties
14-
railties?
15-
end
16-
1715
def railties?
1816
!railties_missing?
1917
end
@@ -26,10 +24,6 @@ def sidekiq_missing!
2624
@sidekiq_missing = true
2725
end
2826

29-
def sidekiq
30-
sidekiq?
31-
end
32-
3327
def sidekiq?
3428
!sidekiq_missing?
3529
end

lib/interactify/hooks.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Interactify
24
module Hooks
35
def reset

spec/integration/all_the_things_integration_spec.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
before do
1717
load_interactify_fixtures("each")
1818
load_interactify_fixtures("if/")
19-
load "./spec/fixtures/integration_app/app/interactors/all_the_things.rb"
19+
silence_warnings do
20+
load "./spec/fixtures/integration_app/app/interactors/all_the_things.rb"
21+
end
2022
end
2123

2224
context "without an optional thing" do

spec/integration/each_integration_spec.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
before do
55
files = Dir.glob("./spec/fixtures/integration_app/app/interactors/each/**/*.rb")
66
files.each do |file|
7-
require file
7+
silence_warnings do
8+
load file
9+
end
810
end
911
end
1012

spec/lib/interactify/async/job_maker_spec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919
end
2020

21-
if Interactify.sidekiq
21+
if Interactify.sidekiq?
2222
describe "#initialize" do
2323
it "initializes with expected attributes" do
2424
expect(subject.container_klass).to eq(container_klass)
@@ -31,7 +31,7 @@
3131

3232
describe "concerning JobClass" do
3333
describe "#job_klass" do
34-
if Interactify.sidekiq
34+
if Interactify.sidekiq?
3535
it "returns a job class" do
3636
job_klass = subject.job_klass
3737

@@ -43,16 +43,16 @@
4343
expect(job_klass.included_modules).to include(Sidekiq::Job)
4444
end
4545
else
46-
it "returns a null job_klass" do
46+
it "returns nil" do
4747
job_klass = subject.job_klass
4848

49-
expect(job_klass).to eq(Interactify::Async::NullJob)
49+
expect(job_klass).to eq(nil)
5050
end
5151
end
5252
end
5353
end
5454

55-
if Interactify.sidekiq
55+
if Interactify.sidekiq?
5656
describe "concerning JobClass" do
5757
describe "#job_klass" do
5858
it "job class includes JOBABLE_OPTS constant" do

spec/lib/interactify/wiring_spec.rb

+138-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
end
1313

1414
before do
15-
Dir.glob("#{root}**/*.rb").each { |f| require(f) }
15+
Dir.glob("#{root}**/*.rb").each { |f| silence_warnings { require(f) } }
1616
end
1717

1818
def f(path)
@@ -82,4 +82,141 @@ def f(path)
8282
end
8383
end
8484
end
85+
86+
describe "#ignore_klass?" do
87+
self::DummyInteractor = Class.new do
88+
include Interactify
89+
end
90+
91+
let(:klass) { self.class::DummyInteractor }
92+
let(:wiring) { described_class.new(root:, ignore:) }
93+
let(:result) { wiring.send(:ignore_klass?, wiring.ignore, klass) }
94+
95+
def self.it_ignores
96+
it "ignores" do
97+
expect(result).to be true
98+
end
99+
end
100+
101+
context "with an array of classes" do
102+
let(:ignore) { [klass] }
103+
104+
it_ignores
105+
end
106+
107+
context "with a regexp" do
108+
let(:ignore) { [/Dummy/] }
109+
110+
it_ignores
111+
end
112+
113+
context "with a string" do
114+
let(:ignore) { ["DummyInteractor"] }
115+
116+
it_ignores
117+
end
118+
119+
context "proc condition" do
120+
let(:ignore) { [->(k) { k.to_s =~ /DummyInteractor/ }] }
121+
122+
it_ignores
123+
end
124+
125+
context "empty ignore" do
126+
let(:ignore) { [] }
127+
128+
it "does not ignore class" do
129+
expect(result).to be false
130+
end
131+
end
132+
end
133+
context "with errors" do
134+
let(:organizer1) { double("Organizer1", klass: "SomeOrganizer1") }
135+
let(:organizer2) { double("Organizer2", klass: "SomeOrganizer2") }
136+
137+
let(:interactor1) { double("Interactor1", klass: "SomeInteractor1") }
138+
let(:interactor2) { double("Interactor2", klass: "SomeInteractor2") }
139+
let(:interactor3) { double("Interactor3", klass: "SomeInteractor3") }
140+
141+
describe "#format_error" do
142+
let(:missing_keys) { %i[foo bar] }
143+
let(:interactor) { double("Interactor", klass: "SomeInteractor") }
144+
let(:organizer) { double("Organizer", klass: "SomeOrganizer") }
145+
let(:formatted_errors) { [] }
146+
147+
it "formats the error message correctly" do
148+
subject.send(:format_error, missing_keys, interactor, organizer, formatted_errors)
149+
expect(formatted_errors.first).to include("Missing keys: :foo, :bar")
150+
expect(formatted_errors.first).to include("expected in: SomeInteractor")
151+
expect(formatted_errors.first).to include("called by: SomeOrganizer")
152+
end
153+
end
154+
155+
describe "#format_errors" do
156+
it "formats and combines all errors" do
157+
all_errors = {
158+
organizer1 => double(
159+
"ErrorContext",
160+
missing_keys: {
161+
interactor1 => %i[key1 key2]
162+
}
163+
),
164+
organizer2 => double(
165+
"ErrorContext",
166+
missing_keys: {
167+
interactor2 => [:key3]
168+
}
169+
)
170+
}
171+
172+
expect(subject).to receive(:format_error).twice.and_call_original
173+
formatted_error_string = subject.format_errors(all_errors)
174+
expect(formatted_error_string).to include("Missing keys: :key1, :key2")
175+
expect(formatted_error_string).to include("Missing keys: :key3")
176+
expect(formatted_error_string).to match(/expected in:.*\n\s+called by:/)
177+
end
178+
end
179+
180+
describe "#each_error" do
181+
it "yields each error unless the class is ignored" do
182+
all_errors = {
183+
organizer1 => double(
184+
"ErrorContext",
185+
missing_keys: {
186+
interactor1 => [:key1],
187+
interactor2 => [:key2]
188+
}
189+
),
190+
organizer2 => double(
191+
"ErrorContext",
192+
missing_keys: {
193+
interactor3 => [:key3]
194+
}
195+
)
196+
}
197+
198+
allow(subject).to receive(:ignore_klass?).and_return(false)
199+
200+
expect { |b| subject.each_error(all_errors, &b) }
201+
.to yield_successive_args(
202+
[[:key1], anything, anything],
203+
[[:key2], anything, anything],
204+
[[:key3], anything, anything]
205+
)
206+
end
207+
208+
it "does not yield errors for ignored classes" do
209+
all_errors = {
210+
organizer1 => double(
211+
"ErrorContext",
212+
missing_keys: {
213+
interactor1 => [:key1]
214+
}
215+
)
216+
}
217+
allow(subject).to receive(:ignore_klass?).and_return(true)
218+
expect { |b| subject.each_error(all_errors, &b) }.not_to yield_control
219+
end
220+
end
221+
end
85222
end

spec/support/spec_support.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def load_interactify_fixtures(sub_directory)
88
files = Dir.glob("./spec/fixtures/integration_app/app/interactors/#{sub_directory}/**/*.rb")
99

1010
files.each do |file|
11-
load file
11+
silence_warnings { load file }
1212
end
1313
end
1414
end

0 commit comments

Comments
 (0)