# frozen_string_literal: true require 'spec_helper' require 'git_helper' describe GitHelper::GitHubClient do let(:git_config_reader) { double(:git_config_reader, github_token: :token) } subject { GitHelper::GitHubClient.new } before do allow(GitHelper::GitConfigReader).to receive(:new).and_return(git_config_reader) end describe '#repository' do it 'should call to run a query' do expect(subject).to receive(:run) subject.repository(Faker::Lorem.word) end it "should return the run command's answer" do expect(subject).to receive(:run).and_return(:command_complete) expect(subject.repository(Faker::Lorem.word)).to eq(:command_complete) end end describe '#pull_request' do it 'should call to run a query' do expect(subject).to receive(:run) subject.pull_request(Faker::Lorem.word, Faker::Number.number) end it "should return the run command's answer" do expect(subject).to receive(:run).and_return(:command_complete) expect(subject.pull_request(Faker::Lorem.word, Faker::Number.number)).to eq(:command_complete) end end describe '#create_pull_request' do it 'should call to run a query' do expect(subject).to receive(:run) subject.create_pull_request(Faker::Lorem.word, {}) end it 'should generate a string list of options' do expect(subject).to receive(:format_options).with({}) subject.create_pull_request(Faker::Lorem.word, {}) end it "should return the run command's answer" do expect(subject).to receive(:run).and_return(:command_complete) expect(subject.create_pull_request(Faker::Lorem.word, {})).to eq(:command_complete) end end describe '#merge_pull_request' do it 'should call to run a query' do expect(subject).to receive(:run) subject.merge_pull_request(Faker::Lorem.word, Faker::Number.number, {}) end it 'should generate a string list of options' do expect(subject).to receive(:format_options).with({}) subject.merge_pull_request(Faker::Lorem.word, Faker::Number.number, {}) end it "should return the run command's answer" do expect(subject).to receive(:run).and_return(:command_complete) expect(subject.merge_pull_request(Faker::Lorem.word, Faker::Number.number, {})).to eq(:command_complete) end end describe '#format_options' do it 'will make a list of hash options into a JSON parsed chunk of key/value pairs as string' do options = { key1: 'value1', key2: true, key3: '', key4: false, key5: 'value5' } # rubocop:disable Style/StringLiterals result = "{\"key1\":\"value1\",\"key2\":true,\"key4\":false,\"key5\":\"value5\"}" # rubocop:enable Style/StringLiterals expect(subject.send(:format_options, options)).to eq(result) end it 'will return an empty string if an empty hash is given' do expect(subject.send(:format_options, {})).to eq('') end it 'will return an empty string if all values are empty strings' do options = { key1: '', key2: '', key3: '' } expect(subject.send(:format_options, options)).to eq('') end end describe '#run' do it 'should call CURL' do expect(subject).to receive(:`).and_return('{}') subject.send(:run, Faker::Lorem.word, 'GET', "/projects/#{Faker::Lorem.word}") end it 'should use JSON to parse the response' do expect(JSON).to receive(:parse).and_return({}) subject.send(:run, Faker::Lorem.word, 'GET', "/projects/#{Faker::Lorem.word}") end it 'should use OpenStruct to turn the hash into an object' do expect(OpenStruct).to receive(:new).and_return(OpenStruct.new) subject.send(:run, Faker::Lorem.word, 'GET', "/projects/#{Faker::Lorem.word}") end end describe '#github_token' do it 'should return a token' do expect(subject.send(:github_token)).to eq(:token) end end describe '#git_config_reader' do it 'should make a new git config reader' do expect(GitHelper::GitConfigReader).to receive(:new) subject.send(:git_config_reader) end end end