Skip to content

Commit

Permalink
🐛 fixed a bug that resulted in broadcasting to every brainstorming + …
Browse files Browse the repository at this point in the history
…added tests (#50)
  • Loading branch information
JannikStreek authored Feb 13, 2021
1 parent fe57109 commit ae1fcf7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
8 changes: 4 additions & 4 deletions lib/mindwendel/brainstormings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand All @@ -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])}
)

Expand Down
2 changes: 1 addition & 1 deletion lib/mindwendel_web/live/brainstorming_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
19 changes: 13 additions & 6 deletions test/mindwendel/brainstormings_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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

Expand Down
32 changes: 32 additions & 0 deletions test/mindwendel_web/channels/brainstorming_channel_test.exs
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ae1fcf7

Please sign in to comment.