module GdsApi::TestHelpers::Worldwide

def stub_worldwide_api_has_locations(location_slugs)

by calling stub_worldwide_api_has_location below
This also sets up the individual endpoints for each slug

The stubs are setup to paginate in chunks of 20
Sets up the index endpoints for the given country slugs
def stub_worldwide_api_has_locations(location_slugs)
  location_slugs.each { |s| stub_worldwide_api_has_location(s) }
  pages = []
  location_slugs.each_slice(20) do |slugs|
    pages << slugs.map { |s| world_location_details_for_slug(s) }
  end
  pages.each_with_index do |page, i|
    page_details = plural_response_base.merge(
      "results" => page,
      "total" => location_slugs.size,
      "pages" => pages.size,
      "current_page" => i + 1,
      "page_size" => 20,
      "start_index" => i * 20 + 1,
    )
    links = { self: "#{WORLDWIDE_API_ENDPOINT}/api/world-locations?page=#{i + 1}" }
    links[:next] = "#{WORLDWIDE_API_ENDPOINT}/api/world-locations?page=#{i + 2}" if pages[i + 1]
    links[:previous] = "#{WORLDWIDE_API_ENDPOINT}/api/world-locations?page=#{i}" unless i.zero?
    page_details["_response_info"]["links"] = []
    link_headers = []
    links.each do |rel, href|
      page_details["_response_info"]["links"] << { "rel" => rel, "href" => href }
      link_headers << "<#{href}>; rel=\"#{rel}\""
    end
    stub_request(:get, links[:self])
      .to_return(status: 200, body: page_details.to_json, headers: { "Link" => link_headers.join(", ") })
    next unless i.zero?
    # First page exists at URL with and without page param
    stub_request(:get, links[:self].sub(/\?page=1/, ""))
      .to_return(status: 200, body: page_details.to_json, headers: { "Link" => link_headers.join(", ") })
  end
end