Skip to content

Instantly share code, notes, and snippets.

@terakilobyte
Created January 13, 2016 03:06
Show Gist options
  • Save terakilobyte/d5d539f1074614860e8e to your computer and use it in GitHub Desktop.
Save terakilobyte/d5d539f1074614860e8e to your computer and use it in GitHub Desktop.
defmodule Votio.TopicChannelTest do
use Votio.ChannelCase
alias Votio.TopicChannel
alias Votio.Topic
alias Votio.TopicView
import Votio.Factory
@valid_attrs %{"message" => %{"categories" => %{"yep" => 0, "nope" => 0}, "title" => "some content"}}
@updated_vote update_in @valid_attrs, ["message", "categories", "yep"], &(&1 + 1)
@changeset_data %{"categories" => %{"yep" => 0, "nope" => 0}, "title" => "some content"}
@invalid_attrs %{}
setup do
Repo.delete_all(Topic)
user = create(:user)
{:ok, jwt, full_claims} = Guardian.encode_and_sign(user)
{:ok, _, socket} =
socket()
|> subscribe_and_join(TopicChannel, "topics:lobby", %{"guardian_token" => "#{jwt}"})
{:ok, socket: socket}
end
test "ping replies with status ok", %{socket: socket} do
ref = push socket, "ping", %{}
assert_reply ref, :ok, _
end
test "shout broadcasts to topics:lobby", %{socket: socket} do
push socket, "shout", %{"hello" => "all"}
assert_broadcast "shout", %{"hello" => "all"}
end
test "broadcasts are pushed to the client", %{socket: socket} do
broadcast_from! socket, "broadcast", %{"some" => "data"}
assert_push "broadcast", %{"some" => "data"}
end
test "updates the db on new_topic and broadcasts the topic", %{socket: socket} do
push socket, "new_topic", @valid_attrs
assert_broadcast "new_topic", %{data: %{categories: %{"yep" => 0, "nope" => 0}, id: _, title: "some content"}}
assert Repo.get_by(Topic, [title: "some content", categories: %{"yep" => 0, "nope" => 0}])
end
test "handles voting and broadcasts the change", %{socket: socket} do
changeset = Topic.vote_changeset(%Topic{}, @changeset_data)
topic =
Repo.insert!(changeset)
|> Phoenix.View.render_one(TopicView, "show.json")
|> Map.get(:data)
|> update_in([:categories, "yep"], &(&1 + 1))
push socket, "topic_vote", %{"message" => topic}
assert_broadcast "topic_vote", topic
assert Map.get(Repo.get(Topic, topic.data.id).categories, "yep") == 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment