summaryrefslogtreecommitdiff
path: root/spec/subscribers/solidus_subscriptions/churn_buster_subscriber_spec.rb
blob: a3e9a23bd07f943150de5e8392d9ecc8f174ba6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# frozen_string_literal: true

RSpec.describe SolidusSubscriptions::ChurnBusterSubscriber do
  describe '#report_subscription_cancellation' do
    it 'reports the cancellation to Churn Buster' do
      churn_buster = instance_spy(SolidusSubscriptions::ChurnBuster::Client)
      allow(SolidusSubscriptions).to receive(:churn_buster).and_return(churn_buster)

      subscription = create(:subscription)
      Spree::Event.fire('solidus_subscriptions.subscription_canceled', subscription: subscription)

      expect(churn_buster).to have_received(:report_subscription_cancellation).with(subscription)
    end
  end

  describe '#report_subscription_ending' do
    it 'reports the cancellation to Churn Buster' do
      churn_buster = instance_spy(SolidusSubscriptions::ChurnBuster::Client)
      allow(SolidusSubscriptions).to receive(:churn_buster).and_return(churn_buster)

      subscription = create(:subscription)
      Spree::Event.fire('solidus_subscriptions.subscription_ended', subscription: subscription)

      expect(churn_buster).to have_received(:report_subscription_cancellation).with(subscription)
    end
  end

  describe '#report_payment_success' do
    it 'reports the success to Churn Buster' do
      churn_buster = instance_spy(SolidusSubscriptions::ChurnBuster::Client)
      allow(SolidusSubscriptions).to receive(:churn_buster).and_return(churn_buster)

      order = build_stubbed(:order)
      installment = build_stubbed(:installment)
      Spree::Event.fire(
        'solidus_subscriptions.installment_succeeded',
        installment: installment,
        order: order,
      )

      expect(churn_buster).to have_received(:report_successful_payment).with(order)
    end
  end

  describe '#report_payment_failure' do
    it 'reports the failure to Churn Buster' do
      churn_buster = instance_spy(SolidusSubscriptions::ChurnBuster::Client)
      allow(SolidusSubscriptions).to receive(:churn_buster).and_return(churn_buster)

      order = build_stubbed(:order)
      installment = build_stubbed(:installment)
      Spree::Event.fire(
        'solidus_subscriptions.installment_failed_payment',
        installment: installment,
        order: order,
      )

      expect(churn_buster).to have_received(:report_failed_payment).with(order)
    end
  end

  describe '#report_payment_method_change' do
    it 'reports the payment method change to Churn Buster' do
      churn_buster = instance_spy(SolidusSubscriptions::ChurnBuster::Client)
      allow(SolidusSubscriptions).to receive(:churn_buster).and_return(churn_buster)

      subscription = create(:subscription)
      Spree::Event.fire(
        'solidus_subscriptions.subscription_payment_method_changed',
        subscription: subscription,
      )

      expect(churn_buster).to have_received(:report_payment_method_change).with(subscription)
    end
  end
end