Programming Phoenix LiveView (Section 10 - Unit Test Test Survey Results State)

Title: Name of book: Programming Phoenix LiveView
Second to last page on Section 10 - Unit Test Test Survey Results State

testing/pento/test/pento_web/live/survey_results_live_test.exs

    test "ratings are filtered by age group", %{
        socket: socket,
        user: user,
        product: product,
        user2: user2} do
      create_rating(2, user, product)
      create_rating(3, user2, product)

      socket
      |> SurveyResultsLive.assign_age_group_filter()
      |> assert_keys(:age_group_filter, "all")
      |> update_socket(:age_group_filter, "18 and under")
      |> SurveyResultsLive.assign_age_group_filter()
      |> assert_keys(:age_group_filter, "18 and under")
      |> SurveyResultsLive.assign_gender_filter()
      |> SurveyResultsLive.assign_products_with_average_ratings()
      |> assert_keys(:products_with_average_ratings, [{"Test Game", 2.0}])
    end

Should be updated with “18 and under” on second SurveyResultsLive.assign_age_group_filter()

testing/pento/test/pento_web/live/survey_results_live_test.exs

    test "ratings are filtered by age group", %{
        socket: socket,
        user: user,
        product: product,
        user2: user2} do
      create_rating(2, user, product)
      create_rating(3, user2, product)

      socket
      |> SurveyResultsLive.assign_age_group_filter()
      |> assert_keys(:age_group_filter, "all")
      |> update_socket(:age_group_filter, "18 and under")
      |> SurveyResultsLive.assign_age_group_filter("18 and under")
      |> assert_keys(:age_group_filter, "18 and under")
      |> SurveyResultsLive.assign_gender_filter()
      |> SurveyResultsLive.assign_products_with_average_ratings()
      |> assert_keys(:products_with_average_ratings, [{"Test Game", 2.0}])
    end


In the same chapter, we should remove :registe_and_log_in_user and update the code as below. Otherwise, setup will call PentoWeb.ConnCase.register_and_log_in_user/1 which will call Pento.AccountsFixtures.user_fixture/1. And the user “test” will be replaced with the user from Pento.AccountsFixtures.user_fixture/1.

  describe "Socket state" do
    setup [
      :create_user,
      :create_product,
      :create_socket
    ]