Skip to content

Instantly share code, notes, and snippets.

@wpiekutowski
Created August 7, 2020 13:34
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save wpiekutowski/8e4de4b9c8253584765ca0106c190dad to your computer and use it in GitHub Desktop.
Testing subscriptions: Elixir + Absinthe + Phoenix WebSocket
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
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