Created
August 7, 2020 13:34
-
-
Save wpiekutowski/8e4de4b9c8253584765ca0106c190dad to your computer and use it in GitHub Desktop.
Testing subscriptions: Elixir + Absinthe + Phoenix WebSocket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule App.Schema.SomeSubscriptionTest do | |
use App.SubscriptionCase | |
test "subscription: someSubscription" do | |
subscription_query = """ | |
subscription { | |
someSubscription { | |
someData | |
} | |
} | |
""" | |
socket = authenticated_socket(token) | |
subscription_id = subscribe(socket, subscription_query) | |
mutation = """ | |
mutation m($data: String!){ | |
someMutation(input: {someData: $data}) { | |
status | |
} | |
} | |
""" | |
ref = push_doc(socket, mutation, variables: %{data: "foo"}) | |
assert_reply(ref, :ok, %{data: %{"someMutation" => %{"status" => "OK"}}}) | |
assert_push("subscription:data", push) | |
assert push == %{ | |
result: %{someData: "foo"}, | |
subscriptionId: subscription_id | |
} | |
# ensure no other pushes were sent | |
refute_push("subscription:data", %{}) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule App.SubscriptionCase do | |
@moduledoc """ | |
This module defines the test case to be used by | |
subscription tests. | |
""" | |
use ExUnit.CaseTemplate | |
alias Absinthe.Phoenix.SubscriptionTest | |
using do | |
quote do | |
use App.ChannelCase | |
use Absinthe.Phoenix.SubscriptionTest, schema: App.Schema | |
import App.SubscriptionCase | |
defp authenticated_socket(token) do | |
params = %{"Authorization" => "Bearer #{token}"} | |
{:ok, socket} = Phoenix.ChannelTest.connect(App.AbsintheSocket, params) | |
{:ok, socket} = SubscriptionTest.join_absinthe(socket) | |
socket | |
end | |
defp subscribe(socket, query) do | |
ref = push_doc(socket, query) | |
assert_reply ref, :ok, %{subscriptionId: subscription_id} | |
subscription_id | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment