class AgentsControllerTest < ActionDispatch::IntegrationTest
  class ShowTest < self
    test "should return success" do
      get agent_url(id: Agent.last.id)
      assert_response :success
    end

    test "should return the correct resource json" do
      agent = Agent.last
      get agent_url(id: agent.id)
      json = JSON.parse(response.body)
      assert_equal json, JSON.parse(agent.to_json)
    end

    test "should return not found with incorrect ID" do
      get agent_url(id: "xyz")
      assert_response :not_found
    end
  end

  class SearchTest < self
    test "should get search and return success" do
      get agent_search_url(phone_number: Agent.last.phone_number)
      assert_response :success
    end

    test "should return the correct resource" do
      agent = Agent.last
      get agent_search_url(phone_number: agent.phone_number)
      json = JSON.parse(response.body)
      assert_equal json, JSON.parse(agent.to_json)
    end

    test "should return not found with incorrect phone" do
      get agent_search_url(phone_number: "xyz")
      assert_response :not_found
    end
  end

  class MatchTest < self
    test "should return success" do
      get agent_match_url
      assert_response :success
    end

    test "should return the correct items" do
      matching_agent           = Agent.find_by_name('James Willhelm')
      matching_agent_industry  = matching_agent.carriers.first.industries.first
      matching_agent_state     = matching_agent.licenses.pluck(:state).first
      expected_matching_agents = Agent.match(matching_agent_industry.id, matching_agent_state)

      get agent_match_url(industry: matching_agent_industry.name, state: matching_agent_state)
      json = JSON.parse(response.body)
      assert_equal json, JSON.parse(expected_matching_agents.to_json)
    end
  end
end
