|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "stimpack/result_monad" |
| 4 | + |
| 5 | +RSpec.describe Stimpack::ResultMonad::GuardClause do |
| 6 | + subject(:service) { klass } |
| 7 | + |
| 8 | + let(:instance) { service.new } |
| 9 | + |
| 10 | + let(:klass) do |
| 11 | + Class.new do |
| 12 | + include Stimpack::ResultMonad |
| 13 | + |
| 14 | + result :foo |
| 15 | + |
| 16 | + def success_result(**options) |
| 17 | + success(**options) |
| 18 | + end |
| 19 | + |
| 20 | + def error_result(errors:) |
| 21 | + error(errors: errors) |
| 22 | + end |
| 23 | + |
| 24 | + def self.to_s |
| 25 | + "Foo" |
| 26 | + end |
| 27 | + |
| 28 | + def call |
| 29 | + guard :foo |
| 30 | + guard { bar } |
| 31 | + |
| 32 | + success(foo: "bar") |
| 33 | + end |
| 34 | + |
| 35 | + private |
| 36 | + |
| 37 | + def foo |
| 38 | + # Stubbed in test cases. |
| 39 | + end |
| 40 | + |
| 41 | + def bar |
| 42 | + # Stubbed in test cases. |
| 43 | + end |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + let(:inner_service_error) do |
| 48 | + double( |
| 49 | + Stimpack::ResultMonad::Result, |
| 50 | + failed?: true, |
| 51 | + errors: ["Inner error"] |
| 52 | + ) |
| 53 | + end |
| 54 | + |
| 55 | + let(:inner_service_success) do |
| 56 | + double( |
| 57 | + Stimpack::ResultMonad::Result, |
| 58 | + failed?: false, |
| 59 | + errors: [] |
| 60 | + ) |
| 61 | + end |
| 62 | + |
| 63 | + describe "#guard" do |
| 64 | + context "when using a label" do |
| 65 | + it { expect { instance.guard(:foo) }.not_to raise_error(ArgumentError) } |
| 66 | + end |
| 67 | + |
| 68 | + context "when using a block" do |
| 69 | + it { expect { instance.guard { :foo } }.not_to raise_error(ArgumentError) } |
| 70 | + end |
| 71 | + |
| 72 | + context "when using both a label and a block" do |
| 73 | + it { expect { instance.guard(:foo) { :foo } }.not_to raise_error(ArgumentError) } |
| 74 | + end |
| 75 | + |
| 76 | + context "when using no arguments" do |
| 77 | + it { expect { instance.guard }.to raise_error(ArgumentError) } |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + describe ".call" do |
| 82 | + context "when all guards pass" do |
| 83 | + before do |
| 84 | + allow(instance).to receive(:foo).and_return(inner_service_success) |
| 85 | + allow(instance).to receive(:bar).and_return(inner_service_success) |
| 86 | + end |
| 87 | + |
| 88 | + it { expect(instance.()).to be_successful } |
| 89 | + end |
| 90 | + |
| 91 | + context "when a guard fails" do |
| 92 | + context "when guard is invoked using a label" do |
| 93 | + before do |
| 94 | + allow(instance).to receive(:foo).and_return(inner_service_error) |
| 95 | + allow(instance).to receive(:bar).and_return(inner_service_success) |
| 96 | + end |
| 97 | + |
| 98 | + it { expect(instance.()).to be_failed } |
| 99 | + it { expect(instance.().errors).to eq(["Inner error"]) } |
| 100 | + end |
| 101 | + |
| 102 | + context "when guard is invoked using a block" do |
| 103 | + before do |
| 104 | + allow(instance).to receive(:foo).and_return(inner_service_success) |
| 105 | + allow(instance).to receive(:bar).and_return(inner_service_error) |
| 106 | + end |
| 107 | + |
| 108 | + it { expect(instance.()).to be_failed } |
| 109 | + it { expect(instance.().errors).to eq(["Inner error"]) } |
| 110 | + end |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + describe ".before_error" do |
| 115 | + before do |
| 116 | + allow(instance).to receive(:inspect) |
| 117 | + allow(instance).to receive(:foo).and_return(inner_service_error) |
| 118 | + |
| 119 | + service.before_error { inspect } |
| 120 | + |
| 121 | + instance.() |
| 122 | + end |
| 123 | + |
| 124 | + it { expect(instance).to have_received(:inspect).once } |
| 125 | + end |
| 126 | +end |
0 commit comments