Skip to content

Commit

Permalink
add spec
Browse files Browse the repository at this point in the history
  • Loading branch information
markburns committed Jul 3, 2024
1 parent 15ecc99 commit 41b5be8
Showing 1 changed file with 86 additions and 11 deletions.
97 changes: 86 additions & 11 deletions spec/lib/interactify/wiring_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,93 @@ def self.it_ignores
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

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")
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

0 comments on commit 41b5be8

Please sign in to comment.