Skip to content

Commit

Permalink
Stringify lists in the ChannelTest module
Browse files Browse the repository at this point in the history
When testing with channels, map keys are stringified to replicate what
would happen in the browser. This allows a test such as:

    push(socket, "topic", %{foo: "bar"})

The ChannelTest module will modify this to reflect a real client,
resulting in the channel receiving:

    %{"foo" => "bar"}

This commit also includes arrays, making the behaviour consistent when
pushing data containing an array.
  • Loading branch information
Gazler committed Sep 13, 2023
1 parent 3b1b767 commit 2893128
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/phoenix/test/channel_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,8 @@ defmodule Phoenix.ChannelTest do
do: struct
def __stringify__(%{} = params),
do: Enum.into(params, %{}, &stringify_kv/1)
def __stringify__(params) when is_list(params),
do: Enum.map(params, &__stringify__/1)
def __stringify__(other),
do: other

Expand Down
6 changes: 6 additions & 0 deletions test/phoenix/test/channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ defmodule Phoenix.Test.ChannelTest do
assert_reply ref, :ok, %{"resp" => "foo"}
end

test "works with list data structures" do
{:ok, _, socket} = join(socket(UserSocket), Channel, "foo:ok")
ref = push(socket, "reply", %{req: [%{bar: "baz"}, %{bar: "foo"}]})
assert_reply ref, :ok, %{"resp" => [%{"bar" => "baz"}, %{"bar" => "foo"}]}
end

test "receives async replies" do
{:ok, _, socket} = join(socket(UserSocket), Channel, "foo:ok")

Expand Down

0 comments on commit 2893128

Please sign in to comment.