Skip to content

Commit

Permalink
Fix cache related flaky specs
Browse files Browse the repository at this point in the history
  • Loading branch information
vassalloandrea committed Sep 15, 2021
1 parent d37b7bd commit 839a062
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
36 changes: 16 additions & 20 deletions spec/models/solidus_afterpay/payment_method_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe SolidusAfterpay::PaymentMethod, type: :model do
Expand Down Expand Up @@ -61,54 +63,48 @@
describe "#available_for_order?" do
subject { payment_method.available_for_order?(order) }

let(:cache) { Rails.cache }

let(:payment_method) { described_class.new }

let(:order) { build(:order, currency: order_currency, total: order_total) }
let(:payment_method) { create(:afterpay_payment_method) }

let(:order_total) { 5 }
let(:order_currency) { 'USD' }

context "with cache" do
after do
cache.clear
end

context 'when order total is inside the range' do
before do
cache.write("solidus_afterpay_configuration_maximumAmount_currency", "USD")
cache.write("solidus_afterpay_configuration_maximumAmount_amount", 10.0)
cache.write("solidus_afterpay_configuration_minimumAmount_amount", 1.0)
Rails.cache.write("solidus_afterpay_configuration_maximumAmount_currency", "USD")
Rails.cache.write("solidus_afterpay_configuration_maximumAmount_amount", 10.0)
Rails.cache.write("solidus_afterpay_configuration_minimumAmount_amount", 1.0)
end

it { is_expected.to be(true) }
end

context 'when order total is outside the range' do
before do
cache.write("solidus_afterpay_configuration_maximumAmount_currency", "USD")
cache.write("solidus_afterpay_configuration_maximumAmount_amount", 4.0)
cache.write("solidus_afterpay_configuration_minimumAmount_amount", 1.0)
Rails.cache.write("solidus_afterpay_configuration_maximumAmount_currency", "USD")
Rails.cache.write("solidus_afterpay_configuration_maximumAmount_amount", 4.0)
Rails.cache.write("solidus_afterpay_configuration_minimumAmount_amount", 1.0)
end

it { is_expected.to be(false) }
end

context 'when order currency is different from afterpay configuration' do
before do
cache.write("solidus_afterpay_configuration_maximumAmount_amount", 10.0)
cache.write("solidus_afterpay_configuration_minimumAmount_amount", 1.0)
cache.write("solidus_afterpay_configuration_maximumAmount_currency", "EUR")
Rails.cache.write("solidus_afterpay_configuration_maximumAmount_amount", 10.0)
Rails.cache.write("solidus_afterpay_configuration_minimumAmount_amount", 1.0)
Rails.cache.write("solidus_afterpay_configuration_maximumAmount_currency", "EUR")
end

it { is_expected.to be(false) }
end

context 'when order currency is the same from afterpay configuration' do
before do
cache.write("solidus_afterpay_configuration_maximumAmount_amount", 10.0)
cache.write("solidus_afterpay_configuration_minimumAmount_amount", 1.0)
cache.write("solidus_afterpay_configuration_maximumAmount_currency", "USD")
Rails.cache.write("solidus_afterpay_configuration_maximumAmount_amount", 10.0)
Rails.cache.write("solidus_afterpay_configuration_minimumAmount_amount", 1.0)
Rails.cache.write("solidus_afterpay_configuration_maximumAmount_currency", "USD")
end

it { is_expected.to be(true) }
Expand Down
5 changes: 5 additions & 0 deletions spec/support/cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

RSpec.configure do |config|
config.after { Rails.cache.clear }
end

0 comments on commit 839a062

Please sign in to comment.