|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe BeyondApi::Checkout::Cart, vcr: true do |
| 4 | + let(:client) { described_class.new(api_url: ENV.fetch('API_URL', nil), access_token: beyond_access_token) } |
| 5 | + |
| 6 | + context 'with cart' do |
| 7 | + before(:each) do |
| 8 | + @cart = client.create |
| 9 | + end |
| 10 | + |
| 11 | + describe '.create' do |
| 12 | + it 'creates a new cart' do |
| 13 | + expect(@cart).not_to be nil |
| 14 | + expect(@cart[:id]).to be_kind_of(String) |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + describe '.find' do |
| 19 | + it 'retrieves the details of a cart' do |
| 20 | + cart_details = client.find(@cart[:id]) |
| 21 | + expect(cart_details).not_to be nil |
| 22 | + expect(cart_details[:id]).to eq(@cart[:id]) |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + describe '.billing_address' do |
| 27 | + it 'sets the billing address of the cart' do |
| 28 | + body = build(:address_data) |
| 29 | + billing_address = client.billing_address(@cart[:id], body) |
| 30 | + expect(billing_address).not_to be nil |
| 31 | + expect(billing_address[:billing_address][:first_name]).to eq(body[:first_name]) |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + describe '.shipping_address' do |
| 36 | + it 'sets the shipping address of the cart' do |
| 37 | + body = build(:address_data) |
| 38 | + shipping_address = client.shipping_address(@cart[:id], body) |
| 39 | + expect(shipping_address).not_to be nil |
| 40 | + expect(shipping_address[:shipping_address][:first_name]).to eq(body[:first_name]) |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + describe '.payment_methods' do |
| 45 | + it 'lists all applicable payment methods for the current cart' do |
| 46 | + response = client.payment_methods(@cart[:id]) |
| 47 | + expect(response).not_to be nil |
| 48 | + expect(response.dig(:embedded, :payment_methods)).to be_an(Array) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + describe '.assign_payment_method_to_default' do |
| 53 | + it 'sets the cart payment method to the current default payment method' do |
| 54 | + response = client.assign_payment_method_to_default(@cart[:id]) |
| 55 | + expect(response).not_to be nil |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + describe '.create_payment' do |
| 60 | + it 'initiates the creation of a payment' do |
| 61 | + body = { |
| 62 | + return_uri: 'https://example.com/return', |
| 63 | + cancel_uri: 'https://example.com/cancel' |
| 64 | + } |
| 65 | + # There is no payment method definition associated with this payment method |
| 66 | + expect { client.create_payment(@cart[:id], body) }.to raise_error(BeyondApi::Error) |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + describe '.create_payment_and_order' do |
| 71 | + it 'initiates the creation of a payment and order for the cart' do |
| 72 | + body = build(:payment_data) |
| 73 | + # There is no payment method definition associated with this payment method |
| 74 | + expect { client.create_payment_and_order(@cart[:id], body) }.to raise_error(BeyondApi::Error) |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + describe '.assign_shipping_method_to_default' do |
| 79 | + it 'sets the cart shipping method to the current default shipping method' do |
| 80 | + response = client.assign_shipping_method_to_default(@cart[:id]) |
| 81 | + expect(response).not_to be nil |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + describe '.pickup_options' do |
| 86 | + it 'lists all applicable pickup options for the current cart' do |
| 87 | + response = client.pickup_options(@cart[:id]) |
| 88 | + expect(response).not_to be nil |
| 89 | + expect(response.dig(:embedded, :pickup_options)).to be_an(Array) |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + describe '.redeem_coupon' do |
| 94 | + it 'redeems a coupon' do |
| 95 | + coupon_code = 'FIRST-ORDER' |
| 96 | + # The coupon code validation failed with following messages: Code doesn't exist |
| 97 | + expect { client.redeem_coupon(@cart[:id], coupon_code) }.to raise_error(BeyondApi::Error) |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + describe '.delete_coupon' do |
| 102 | + it 'removes a coupon from the cart' do |
| 103 | + coupon_code = 'FIRST-ORDER' |
| 104 | + # The coupon code validation failed with following messages: Code doesn't exist |
| 105 | + expect { client.redeem_coupon(@cart[:id], coupon_code) } .to raise_error(BeyondApi::Error) |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + describe '.delete' do |
| 110 | + it 'deletes a cart' do |
| 111 | + response = client.delete(@cart[:id]) |
| 112 | + expect(response).to eq({}) |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + after(:each) do |
| 117 | + client.delete(@cart[:id]) |
| 118 | + rescue BeyondApi::Error # rubocop:disable Lint/SuppressedException |
| 119 | + end |
| 120 | + end |
| 121 | +end |
0 commit comments