require 'helper' describe Bearcat::ApiArray do let(:api_client) { double } let(:response_body) { @response_body || [] } let(:original_response) { double(body: response_body, status: 200, headers: {}, env: {}) } describe 'get_page' do let(:api_array) { described_class.process_response(original_response, api_client) } let(:connection) { double } let(:request) { double } before :each do api_array.instance_variable_set('@page_count', nil) allow(api_client).to receive(:connection).and_return connection allow(connection).to receive(:send) do |&block| block.call(request) end end it 'gets a page of results' do expect(request).to receive(:url).with('https://fake.com', 'page' => '2') api_array.send(:get_page, 'https://fake.com?page=2', {}) end it 'merges additonal params to the url query' do expect(request).to receive(:url).with('https://fake.com', 'test' => 'param', 'page' => 2) api_array.send(:get_page, 'https://fake.com?test=param', 'page' => 2) end it 'handles array parameters' do expect(request).to receive(:url).with('https://fake.com', 'test' => ['param'], 'second' => ['param']) api_array.send(:get_page, 'https://fake.com?test%5B%5D=param', 'second' => ['param']) end it 'sets the per_page parameter if not already set and @page_count has a value' do api_array.instance_variable_set('@page_count', 50) expect(request).to receive(:url).with('https://fake.com', 'per_page' => 50) api_array.send(:get_page, 'https://fake.com?', {}) end it 'does not set the per_page parameter if @page_count is not available' do api_array.instance_variable_set('@page_count', nil) expect(request).to receive(:url).with('https://fake.com', {}) api_array.send(:get_page, 'https://fake.com?', {}) end it 'does not set the per_page parameter if it is already set' do api_array.instance_variable_set('@page_count', 50) expect(request).to receive(:url).with('https://fake.com', 'per_page' => 10) api_array.send(:get_page, 'https://fake.com?', 'per_page' => 10) end end context 'makes returned values indifferent' do it 'makes a Hash response indifferent' do @response_body = { something: 1 } result = described_class.process_response(original_response, api_client) expect(result[:something]).to eq 1 expect(result['something']).to eq 1 end it 'makes an Array response indifferent' do @response_body = [{ something: 1 }] result = described_class.process_response(original_response, api_client) expect(result[0][:something]).to eq 1 expect(result[0]['something']).to eq 1 end it 'makes a single-key Hash response indifferent' do @response_body = { 'something' => [ { foo: 1 } ] } allow(described_class).to receive(:array_key).and_return('something') result = described_class.process_response(original_response, api_client) expect(result[0][:foo]).to eq 1 expect(result[0]['foo']).to eq 1 end end end