diff --git a/assets/js/launch-comments.ts b/assets/js/launch-comments.ts index ff56756..2fa9077 100644 --- a/assets/js/launch-comments.ts +++ b/assets/js/launch-comments.ts @@ -35,6 +35,9 @@ export class LaunchCommentsElement extends LitElement { @liveStateConfig('topic') get topic() { return `launch_comments:${this.siteId}`; } + @liveStateConfig('params.url') + get pageUrl() { return window.location.href; } + @query('input[name="author"]') author: HTMLInputElement | undefined; diff --git a/config/test.exs b/config/test.exs index 1b8ea08..6b39520 100644 --- a/config/test.exs +++ b/config/test.exs @@ -46,7 +46,7 @@ config :launch_cart, :sandbox, Ecto.Adapters.SQL.Sandbox config :wallaby, otp_app: :launch_cart, - base_url: "http://localhost:4002" - # chromedriver: [ - # headless: false - # ] + base_url: "http://localhost:4002", + chromedriver: [ + headless: false + ] diff --git a/lib/factory.ex b/lib/factory.ex index 95fa181..94d7a0a 100644 --- a/lib/factory.ex +++ b/lib/factory.ex @@ -1,5 +1,6 @@ defmodule LaunchCart.Factory do + alias Faker.Person alias Faker.Lorem alias Faker.Internet @@ -97,7 +98,8 @@ defmodule LaunchCart.Factory do def comment_factory() do %Comment{ comment_site: build(:comment_site), - author: "Bob", + author: Person.name, + url: Internet.url(), comment: "I think therefore I am" } end diff --git a/lib/launch_cart/comments.ex b/lib/launch_cart/comments.ex index 8ff523b..d623383 100644 --- a/lib/launch_cart/comments.ex +++ b/lib/launch_cart/comments.ex @@ -19,8 +19,12 @@ defmodule LaunchCart.Comments do [%Comment{}, ...] """ - def list_comments(site_id) do - from(c in Comment, where: c.comment_site_id == ^site_id, order_by: {:desc, c.inserted_at}) + def list_comments(site_id, url) do + from(c in Comment, + where: c.comment_site_id == ^site_id, + where: c.url == ^url, + order_by: {:desc, c.inserted_at} + ) |> Repo.all() end diff --git a/lib/launch_cart_web/channels/comments_channel.ex b/lib/launch_cart_web/channels/comments_channel.ex index 319a9a3..7429acb 100644 --- a/lib/launch_cart_web/channels/comments_channel.ex +++ b/lib/launch_cart_web/channels/comments_channel.ex @@ -2,21 +2,19 @@ defmodule LaunchCartWeb.CommentsChannel do use LiveState.Channel, web_module: LaunchCartWeb alias LaunchCart.Comments - alias LaunchCart.CommentSites - alias LaunchCart.CommentSites.CommentSite alias LiveState.Event @impl true def state_key, do: :state @impl true - @spec init(any, any, any) :: {:ok, %{comments: any}} - def init("launch_comments:" <> comment_site_id, _params, _socket) do + @spec init(any, any, any) :: {:ok, map()} + def init("launch_comments:" <> comment_site_id, %{"url" => url}, _socket) do Phoenix.PubSub.subscribe(LaunchCart.PubSub, "comments:#{comment_site_id}") - case CommentSites.get_comment_site!(comment_site_id) do + + case Comments.list_comments(comment_site_id, url) do nil -> {:error, "Comment site not found"} - %CommentSite{comments: comments} -> - {:ok, %{comments: comments}} + comments -> {:ok, %{comments: comments, url: url}} end end @@ -30,8 +28,9 @@ defmodule LaunchCartWeb.CommentsChannel do end @impl true - def handle_message({:comment_created, comment}, state) do - {:noreply, state |> Map.put(:comments, Comments.list_comments(comment.comment_site_id))} + def handle_message({:comment_created, comment}, %{url: url} = state) do + {:noreply, + state |> Map.put(:comments, Comments.list_comments(comment.comment_site_id, url))} end @impl true diff --git a/test/launch_cart/comments_test.exs b/test/launch_cart/comments_test.exs index 026b0f2..c40e2d5 100644 --- a/test/launch_cart/comments_test.exs +++ b/test/launch_cart/comments_test.exs @@ -12,7 +12,9 @@ defmodule LaunchCart.CommentsTest do test "list_comments/1 returns all comments for site" do comment = insert(:comment) - assert Comments.list_comments(comment.comment_site_id) |> Enum.map(& &1.id) == [comment.id] + + assert Comments.list_comments(comment.comment_site_id, comment.url) |> Enum.map(& &1.id) == + [comment.id] end test "get_comment!/1 returns the comment with given id" do @@ -35,7 +37,12 @@ defmodule LaunchCart.CommentsTest do test "update_comment/2 with valid data updates the comment" do comment = insert(:comment) - update_attrs = %{author: "some updated author", comment: "some updated comment", url: "some updated url"} + + update_attrs = %{ + author: "some updated author", + comment: "some updated comment", + url: "some updated url" + } assert {:ok, %Comment{} = comment} = Comments.update_comment(comment, update_attrs) assert comment.author == "some updated author" diff --git a/test/launch_cart_web/features/launch_comments_test.exs b/test/launch_cart_web/features/launch_comments_test.exs index 1951167..bf19881 100644 --- a/test/launch_cart_web/features/launch_comments_test.exs +++ b/test/launch_cart_web/features/launch_comments_test.exs @@ -5,6 +5,8 @@ defmodule LaunchCartWeb.Features.LaunchCommentsTest do alias LaunchCart.Repo alias LaunchCart.CommentSites alias LaunchCart.Comments + alias LaunchCartWeb.Endpoint + alias LaunchCartWeb.Router.Helpers, as: Routes import Wallaby.Query import LaunchCart.Factory @@ -29,20 +31,52 @@ defmodule LaunchCartWeb.Features.LaunchCommentsTest do end feature "sees new comments as they are created", %{session: session, comment_site: comment_site} do - session = session - |> visit("/fake_comment_site/#{comment_site.id}") + session = + session + |> visit("/fake_comment_site/#{comment_site.id}") + + {:ok, _comment} = + Comments.create_comment(%{ + comment_site_id: comment_site.id, + url: Routes.page_url(Endpoint, :fake_comment_site, comment_site.id), + author: "Yo Mamma", + comment: "is so great that you are lucky to have her." + }) + + session + |> find(css("launch-comments")) + |> shadow_root() + |> assert_has(css("div[part='comment-text']", text: "is so great")) + end + + feature "sees comments for current page url only", %{ + session: session, + comment_site: comment_site + } do + session = + session + |> visit("/fake_comment_site/#{comment_site.id}") {:ok, _comment} = Comments.create_comment(%{ comment_site_id: comment_site.id, - url: "http://foo.bar", + url: Routes.page_url(Endpoint, :fake_comment_site, comment_site.id), author: "Yo Mamma", comment: "is so great that you are lucky to have her." }) + {:ok, _comment} = + Comments.create_comment(%{ + comment_site_id: comment_site.id, + url: "http://foo.bar/dad", + author: "Yo Daddy", + comment: "is pretty good." + }) + session |> find(css("launch-comments")) |> shadow_root() |> assert_has(css("div[part='comment-text']", text: "is so great")) + |> refute_has(css("div[part='comment-text']", text: "is pretty good")) end end