require 'helper' describe Bearcat::ApiArray do describe 'get_page' do let(:api_client) { double } let(:original_response) { double(body: [], status: 200, headers: {}, env: {}) } 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 end