# frozen_string_literal: true

shared_context "github request mocking" do
  let(:github_token) { 'MY_GITHUB_TOKEN' }

  def stub_github_request(query:, variables:, response_data: nil, response_errors: nil, token: github_token)
    headers = {
      'Authorization' => "bearer #{token}"
    }
    response = {}
    response[:data] = response_data if response_data
    response[:errors] = response_errors if response_errors
    stub_request(:post, GraphQLSchemaCompatibility::Github::GITHUB_GRAPHQL_URL).with do |request|
      json = JSON.parse(request.body)
      headers <= request.headers &&
        json['query'] == query.document.to_query_string &&
        json['variables'] == variables.deep_stringify_keys
    end.to_return(body: response.to_json, status: 200)
  end
end
