summaryrefslogtreecommitdiff
path: root/spec/controllers/spree/api/line_items_controller_spec.rb
blob: 4571d89db0cebb4143564528fcca8601515d6ccb (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require 'spec_helper'
require 'spree/api/testing_support/helpers'

RSpec.describe Spree::Api::LineItemsController, type: :controller do
  include Spree::Api::TestingSupport::Helpers
  routes { Spree::Core::Engine.routes }

  describe 'POST :create' do
    subject { post :create, params: params }

    let(:params) { line_item_params }
    let!(:variant) { create :variant }
    let!(:order) { create :order }

    let(:line_item_params) do
      {
        order_id: order.number,
        order_token: order.guest_token,
        format: 'json',
        line_item: {
          quantity: 1,
          variant_id: variant.id
        }
      }
    end

    shared_examples 'a new line item' do
      it { is_expected.to be_created }

      it 'creates a line item' do
        expect { subject }.
          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: '1990/10/12',
            subscribable_id: variant.id,
            interval_length: 30,
            interval_units: "day"
          }
        }
      end

      it_behaves_like 'a new line item'

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

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

  describe 'patch :update' do
    subject { patch :create, params: params }

    let(:params) { line_item_params }
    let!(:variant) { create :variant }
    let!(:order) { create :order }
    let!(:line_item) { create :line_item, order: order, variant: variant }

    let(:line_item_params) do
      {
        id: line_item.id,
        order_id: order.number,
        order_token: order.guest_token,
        format: 'json',
        line_item: {
          quantity: 1,
          variant_id: variant.id
        },
        subscription_line_item: {
          quantity: 2,
          end_date: '1990/10/12',
          subscribable_id: variant.id,
          interval_length: 30,
          interval_units: "day"
        }
      }
    end

    it { is_expected.to be_successful }

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