|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +RSpec.describe XenditApi::Api::SplitRule do |
| 4 | + let(:client) { XenditApi::Client.new('FILTERED_AUTH_KEY') } |
| 5 | + let(:split_rule_api) { described_class.new(client) } |
| 6 | + |
| 7 | + describe 'PATH constant' do |
| 8 | + it 'has the correct path' do |
| 9 | + expect(described_class::PATH).to eq('/split_rules') |
| 10 | + end |
| 11 | + end |
| 12 | + |
| 13 | + describe 'inheritance' do |
| 14 | + it 'inherits from Base' do |
| 15 | + expect(described_class.superclass).to eq(XenditApi::Api::Base) |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + describe '#create' do |
| 20 | + let(:params) do |
| 21 | + { |
| 22 | + name: 'Test Split Rule', |
| 23 | + description: 'A test split rule', |
| 24 | + routes: [ |
| 25 | + { |
| 26 | + flat_amount: 1000, |
| 27 | + currency: 'IDR', |
| 28 | + destination: { |
| 29 | + type: 'MERCHANT', |
| 30 | + account_code: 'MERCHANT_001' |
| 31 | + } |
| 32 | + } |
| 33 | + ] |
| 34 | + } |
| 35 | + end |
| 36 | + |
| 37 | + let(:expected_response) do |
| 38 | + { |
| 39 | + id: 'sr_123456789', |
| 40 | + name: 'Test Split Rule', |
| 41 | + description: 'A test split rule', |
| 42 | + created: '2023-10-08T10:00:00.000Z', |
| 43 | + updated: '2023-10-08T10:00:00.000Z', |
| 44 | + routes: [ |
| 45 | + { |
| 46 | + flat_amount: 1000, |
| 47 | + currency: 'IDR', |
| 48 | + destination: { |
| 49 | + type: 'MERCHANT', |
| 50 | + account_code: 'MERCHANT_001' |
| 51 | + } |
| 52 | + } |
| 53 | + ] |
| 54 | + } |
| 55 | + end |
| 56 | + |
| 57 | + it 'creates a split rule and returns a SplitRule model' do |
| 58 | + allow(client).to receive(:post).with('/split_rules', params, { 'Path-Group' => '/split_rules' }) |
| 59 | + .and_return(expected_response) |
| 60 | + |
| 61 | + result = split_rule_api.create(params) |
| 62 | + |
| 63 | + expect(result).to be_a(XenditApi::Model::SplitRule) |
| 64 | + expect(result.id).to eq('sr_123456789') |
| 65 | + expect(result.name).to eq('Test Split Rule') |
| 66 | + expect(result.description).to eq('A test split rule') |
| 67 | + end |
| 68 | + |
| 69 | + it 'sends correct headers with Path-Group' do |
| 70 | + allow(client).to receive(:post).and_return(expected_response) |
| 71 | + |
| 72 | + split_rule_api.create(params) |
| 73 | + |
| 74 | + expect(client).to have_received(:post).with( |
| 75 | + '/split_rules', |
| 76 | + params, |
| 77 | + { 'Path-Group' => '/split_rules' } |
| 78 | + ) |
| 79 | + end |
| 80 | + |
| 81 | + it 'passes through the correct parameters' do |
| 82 | + allow(client).to receive(:post).and_return(expected_response) |
| 83 | + |
| 84 | + split_rule_api.create(params) |
| 85 | + |
| 86 | + expect(client).to have_received(:post).with('/split_rules', params, anything) |
| 87 | + end |
| 88 | + |
| 89 | + context 'when client raises an error' do |
| 90 | + it 'propagates the error' do |
| 91 | + allow(client).to receive(:post).and_raise(StandardError, 'Network error') |
| 92 | + |
| 93 | + expect { split_rule_api.create(params) }.to raise_error(StandardError, 'Network error') |
| 94 | + end |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + describe '#initialize' do |
| 99 | + it 'accepts a client parameter' do |
| 100 | + expect(split_rule_api.client).to eq(client) |
| 101 | + end |
| 102 | + end |
| 103 | +end |
0 commit comments