diff --git a/lib/mindwendel/brainstormings.ex b/lib/mindwendel/brainstormings.ex index dc9cff1b..d9a56ff1 100644 --- a/lib/mindwendel/brainstormings.ex +++ b/lib/mindwendel/brainstormings.ex @@ -344,12 +344,12 @@ defmodule Mindwendel.Brainstormings do ## Examples - iex> subscribe + iex> subscribe(3) :ok """ - def subscribe do - Phoenix.PubSub.subscribe(Mindwendel.PubSub, "ideas") + def subscribe(brainstorming_id) do + Phoenix.PubSub.subscribe(Mindwendel.PubSub, "brainstormings:" <> brainstorming_id) end @doc """ @@ -364,7 +364,7 @@ defmodule Mindwendel.Brainstormings do def broadcast({:ok, idea}, event) do Phoenix.PubSub.broadcast( Mindwendel.PubSub, - "ideas", + "brainstormings:" <> idea.brainstorming_id, {event, idea |> Repo.preload([:link, :likes])} ) diff --git a/lib/mindwendel_web/live/brainstorming_live/show.ex b/lib/mindwendel_web/live/brainstorming_live/show.ex index d1b4b93e..acfea095 100644 --- a/lib/mindwendel_web/live/brainstorming_live/show.ex +++ b/lib/mindwendel_web/live/brainstorming_live/show.ex @@ -7,7 +7,7 @@ defmodule MindwendelWeb.BrainstormingLive.Show do @impl true def mount(%{"id" => id}, session, socket) do - if connected?(socket), do: Brainstormings.subscribe() + if connected?(socket), do: Brainstormings.subscribe(id) current_user_id = MindwendelService.SessionService.get_current_user_id(session) diff --git a/test/mindwendel/brainstormings_test.exs b/test/mindwendel/brainstormings_test.exs index c401e933..251bd62f 100644 --- a/test/mindwendel/brainstormings_test.exs +++ b/test/mindwendel/brainstormings_test.exs @@ -4,9 +4,12 @@ defmodule Mindwendel.BrainstormingsTest do alias Mindwendel.Brainstormings setup do + brainstorming = Factory.insert!(:brainstorming) + %{ - brainstorming: Factory.insert!(:brainstorming), - idea: Factory.insert!(:idea), + brainstorming: brainstorming, + idea: + Factory.insert!(:idea, brainstorming: brainstorming, inserted_at: ~N[2021-01-01 15:04:30]), user: Factory.insert!(:user), like: Factory.insert!(:like, :with_idea_and_user) } @@ -28,7 +31,7 @@ defmodule Mindwendel.BrainstormingsTest do end describe "list_ideas_for_brainstorming" do - test "orders by like count", %{brainstorming: brainstorming, user: user} do + test "orders by like count", %{brainstorming: brainstorming, user: user, idea: idea} do first_idea = Factory.insert!(:idea, brainstorming_id: brainstorming.id) third_idea = Factory.insert!(:idea, brainstorming_id: brainstorming.id) second_idea = Factory.insert!(:idea, brainstorming_id: brainstorming.id) @@ -42,10 +45,14 @@ defmodule Mindwendel.BrainstormingsTest do ids = Enum.map(Brainstormings.list_ideas_for_brainstorming(brainstorming.id), fn x -> x.id end) - assert ids == [first_idea.id, second_idea.id, third_idea.id] + assert ids == [first_idea.id, second_idea.id, third_idea.id, idea.id] end - test "orders by date if like count is equal", %{brainstorming: brainstorming, user: user} do + test "orders by date if like count is equal", %{ + brainstorming: brainstorming, + user: user, + idea: idea + } do older_idea = Factory.insert!(:idea, brainstorming_id: brainstorming.id, @@ -64,7 +71,7 @@ defmodule Mindwendel.BrainstormingsTest do ids = Enum.map(Brainstormings.list_ideas_for_brainstorming(brainstorming.id), fn x -> x.id end) - assert ids == [younger_idea.id, older_idea.id] + assert ids == [younger_idea.id, older_idea.id, idea.id] end end diff --git a/test/mindwendel_web/channels/brainstorming_channel_test.exs b/test/mindwendel_web/channels/brainstorming_channel_test.exs new file mode 100644 index 00000000..bd0fcaa6 --- /dev/null +++ b/test/mindwendel_web/channels/brainstorming_channel_test.exs @@ -0,0 +1,32 @@ +defmodule MindwendelWeb.BrainstormingChannelTest do + use MindwendelWeb.ChannelCase + + alias Mindwendel.Factory + alias Mindwendel.Brainstormings + alias Mindwendel.Brainstormings.Idea + + setup do + %{ + brainstorming: Factory.insert!(:brainstorming) + } + end + + describe "subscribe" do + test "listening for the brainstorming gets updates for ideas", %{brainstorming: brainstorming} do + idea = Factory.insert!(:idea, brainstorming: brainstorming) + + Brainstormings.subscribe(brainstorming.id) + Brainstormings.update_idea(idea, %{body: "lalala"}) + assert_received {:idea_updated, idea} + end + + test "does not receive messages from other brainstormings", %{brainstorming: brainstorming} do + idea = Factory.insert!(:idea, brainstorming: brainstorming) + other_brainstorming = Factory.insert!(:brainstorming) + + Brainstormings.subscribe(other_brainstorming.id) + Brainstormings.update_idea(idea, %{body: "lalala"}) + refute_received {:idea_updated, idea} + end + end +end diff --git a/test/support/factory.ex b/test/support/factory.ex index 7fc4eb99..a5ff56bf 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -40,7 +40,7 @@ defmodule Mindwendel.Factory do def build(:like, :with_idea_and_user) do %Like{ user: build(:user), - idea: build(:idea) + idea: build(:idea, brainstorming: build(:brainstorming)) } end