Skip to content

Commit

Permalink
fix: GraphQLWSClient test
Browse files Browse the repository at this point in the history
  • Loading branch information
tlux committed Mar 27, 2024
1 parent e5f66f0 commit 5f63dbc
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions test/graphql_ws_client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule GraphQLWSClientTest do
{:ok, @conn}
end)

assert {:ok, client} = start_supervised({GraphQLWSClient, @config})
assert {:ok, client} = GraphQLWSClient.start_link(@config) # start_supervised({GraphQLWSClient, @config})
assert GraphQLWSClient.connected?(client) == true
end

Expand All @@ -56,7 +56,7 @@ defmodule GraphQLWSClientTest do
{:ok, @conn}
end)

assert {:ok, client} = start_supervised({GraphQLWSClient, @opts})
assert {:ok, client} = GraphQLWSClient.start_link(@opts)
assert GraphQLWSClient.connected?(client) == true
end

Expand All @@ -68,7 +68,7 @@ defmodule GraphQLWSClientTest do
end)

assert capture_log(fn ->
start_supervised!({GraphQLWSClient, @config})
GraphQLWSClient.start_link(@config)

# wait a little bit until connection fails
Process.sleep(100)
Expand All @@ -87,7 +87,7 @@ defmodule GraphQLWSClientTest do
{:ok, conn}
end)

client = start_supervised!({GraphQLWSClient, config})
{:ok, client} = GraphQLWSClient.start_link(config)

assert GraphQLWSClient.connected?(client) == false
assert GraphQLWSClient.open(client) == :ok
Expand All @@ -101,13 +101,13 @@ defmodule GraphQLWSClientTest do
{:error, error}
end)

client = start_supervised!({GraphQLWSClient, config})
{:ok, client} = GraphQLWSClient.start_link(config)

assert GraphQLWSClient.open(client) == {:error, error}
end

test "no-op when already connected", %{config: config, conn: conn} do
client = start_supervised!({GraphQLWSClient, config})
{:ok, client} = GraphQLWSClient.start_link(config)

expect(MockDriver, :connect, fn ^conn ->
{:ok, conn}
Expand All @@ -130,7 +130,7 @@ defmodule GraphQLWSClientTest do
{:ok, conn}
end)

client = start_supervised!({GraphQLWSClient, config})
{:ok, client} = GraphQLWSClient.start_link(config)

assert GraphQLWSClient.connected?(client) == false
assert GraphQLWSClient.open!(client) == :ok
Expand All @@ -144,7 +144,7 @@ defmodule GraphQLWSClientTest do
{:error, error}
end)

client = start_supervised!({GraphQLWSClient, config})
{:ok, client} = GraphQLWSClient.start_link(config)

assert_raise SocketError, Exception.message(error), fn ->
GraphQLWSClient.open!(client)
Expand All @@ -158,6 +158,7 @@ defmodule GraphQLWSClientTest do
setup do
config = %{@config | connect_on_start: false}
conn = %{@conn | config: config, init_payload: @init_payload}

{:ok, config: config, conn: conn}
end

Expand All @@ -166,7 +167,7 @@ defmodule GraphQLWSClientTest do
{:ok, conn}
end)

client = start_supervised!({GraphQLWSClient, config})
{:ok, client} = GraphQLWSClient.start_link(config)

assert GraphQLWSClient.connected?(client) == false
assert GraphQLWSClient.open_with(client, @init_payload) == :ok
Expand All @@ -180,13 +181,13 @@ defmodule GraphQLWSClientTest do
{:error, error}
end)

client = start_supervised!({GraphQLWSClient, config})
{:ok, client} = GraphQLWSClient.start_link(config)

assert GraphQLWSClient.open_with(client, @init_payload) == {:error, error}
end

test "no-op when already connected", %{config: config, conn: conn} do
client = start_supervised!({GraphQLWSClient, config})
{:ok, client} = GraphQLWSClient.start_link(config)

expect(MockDriver, :connect, fn ^conn ->
{:ok, conn}
Expand All @@ -209,7 +210,7 @@ defmodule GraphQLWSClientTest do
|> expect(:disconnect, fn ^conn -> :ok end)
|> expect(:connect, fn ^reconnect_conn -> {:ok, reconnect_conn} end)

client = start_supervised!({GraphQLWSClient, config})
{:ok, client} = GraphQLWSClient.start_link(config)

assert GraphQLWSClient.open_with(client, @init_payload) == :ok
assert GraphQLWSClient.close(client) == :ok
Expand All @@ -234,7 +235,7 @@ defmodule GraphQLWSClientTest do
{:ok, conn}
end)

client = start_supervised!({GraphQLWSClient, config})
{:ok, client} = GraphQLWSClient.start_link(config)

assert GraphQLWSClient.open_with(client, @init_payload) == {:error, error}
assert_receive :reconnected_msg
Expand All @@ -259,7 +260,7 @@ defmodule GraphQLWSClientTest do
{:ok, conn}
end)

client = start_supervised!({GraphQLWSClient, config})
{:ok, client} = GraphQLWSClient.start_link(config)

assert GraphQLWSClient.open_with(client, @init_payload) == :ok
assert_receive :reconnected_msg
Expand All @@ -280,7 +281,7 @@ defmodule GraphQLWSClientTest do
{:ok, conn}
end)

client = start_supervised!({GraphQLWSClient, config})
{:ok, client} = GraphQLWSClient.start_link(config)

assert GraphQLWSClient.connected?(client) == false
assert GraphQLWSClient.open_with!(client, @init_payload) == :ok
Expand All @@ -294,7 +295,7 @@ defmodule GraphQLWSClientTest do
{:error, error}
end)

client = start_supervised!({GraphQLWSClient, config})
{:ok, client} = GraphQLWSClient.start_link(config)

assert_raise SocketError, Exception.message(error), fn ->
GraphQLWSClient.open_with!(client, @init_payload)
Expand Down Expand Up @@ -382,7 +383,7 @@ defmodule GraphQLWSClientTest do

test "not connected" do
config = %{@config | connect_on_start: false}
client = start_supervised!({GraphQLWSClient, config})
{:ok, client} = GraphQLWSClient.start_link(config)

assert GraphQLWSClient.query(client, @query, @variables) ==
{:error, %SocketError{cause: :closed}}
Expand All @@ -404,7 +405,7 @@ defmodule GraphQLWSClientTest do
:ok
end)

client = start_supervised!({GraphQLWSClient, config})
{:ok, client} = GraphQLWSClient.start_link(config)

assert GraphQLWSClient.query(client, @query, @variables) ==
{:error, %SocketError{cause: :timeout}}
Expand Down Expand Up @@ -587,7 +588,7 @@ defmodule GraphQLWSClientTest do

test "not connected" do
config = %{@config | connect_on_start: false}
client = start_supervised!({GraphQLWSClient, config})
{:ok, client} = GraphQLWSClient.start_link(config)
error = %SocketError{cause: :closed}

assert_raise SocketError, Exception.message(error), fn ->
Expand Down Expand Up @@ -925,7 +926,9 @@ defmodule GraphQLWSClientTest do
end

test "ignore message when not connected", %{client: client, conn: conn} do
stub(MockDriver, :disconnect, fn ^conn -> :ok end)
expect(MockDriver, :disconnect, fn ^conn ->
:ok
end)

:ok = GraphQLWSClient.close(client)
refute GraphQLWSClient.connected?(client)
Expand Down

0 comments on commit 5f63dbc

Please sign in to comment.