summaryrefslogtreecommitdiff
path: root/spec/decorators/controllers/solidus_subscriptions/spree/orders_controller/create_subscription_line_items_spec.rb
blob: d3c3ce9b511cf214be7cb3374beaf50f167d2d6f (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
77
78
79
80
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe SolidusSubscriptions::Spree::OrdersController::CreateSubscriptionLineItems, type: :controller do
  controller(Spree::OrdersController) {}
  routes { Spree::Core::Engine.routes }

  let!(:user) { create :user }

  before do
    allow(controller).to receive_messages(try_spree_current_user: user)
    create :store
  end

  describe 'POST /orders/populate' do
    subject(:populate) { post :populate, params: params }

    let!(:variant) { create :variant }
    let(:params) { line_item_params }
    let(:line_item_params) do
      {
        quantity: 1,
        variant_id: variant.id
      }
    end

    shared_examples 'a new order line item' do
      it { is_expected.to redirect_to cart_path }

      it 'creates an order' do
        expect { populate }.
          to change { Spree::Order.count }.
          from(0).to(1)
      end

      it 'creates a line item' do
        expect { populate }.
          to change { Spree::LineItem.count }.
          from(0).to(1)
      end
    end

    context 'with subscription_line_item params' do
      let(:params) { line_item_params.merge(subscription_line_item_params) }
      let(:subscription_line_item_params) do
        {
          subscription_line_item: {
            quantity: 2,
            end_date: Date.parse('2000/10/11'),
            subscribable_id: variant.id,
            interval_length: 30,
            interval_units: "day"
          }
        }
      end

      it_behaves_like 'a new order line item'

      it 'creates a new subscription line item' do
        expect { populate }.
          to change { SolidusSubscriptions::LineItem.count }.
          from(0).to(1)
      end

      it 'creates a subscription line item with the correct values' do
        populate
        subscription_line_item = SolidusSubscriptions::LineItem.last

        expect(subscription_line_item).to have_attributes(
          subscription_line_item_params[:subscription_line_item]
        )
      end
    end

    context 'without subscription_line_item params' do
      it_behaves_like 'a new order line item'
    end
  end
end