ash_graphql/test/enum_test.exs
Riccardo Binetti 98baf6f0be
feat: allow providing descriptions for enum values (#117)
* chore: update ash to use Ash.Type.Enum descriptions

Contextually, remove an unreachable clause that caused dyalizer to fail,
see https://github.com/ash-project/ash/pull/873 for the commit that made
it unreachable

* feat: allow providing descriptions for enum values

If the module exports `graphql_describe_enum_value/1`, use that. Otherwise,
fallback to the Ash.Type.Enum value descriptions introduced in
https://github.com/ash-project/ash/pull/940.

Close #116
2024-03-25 23:43:59 -04:00

84 lines
2.3 KiB
Elixir

defmodule AshGraphql.EnumTest do
use ExUnit.Case, async: false
setup do
on_exit(fn ->
Application.delete_env(:ash_graphql, AshGraphql.Test.Api)
AshGraphql.TestHelpers.stop_ets()
end)
end
test "enum without value descriptions returns a nil description" do
{:ok, %{data: data}} =
"""
query {
__type(name: "StatusEnum") {
enumValues {
name
description
}
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema)
assert %{"name" => "CLOSED", "description" => nil} =
data["__type"]["enumValues"]
|> Enum.find(fn value -> value["name"] == "CLOSED" end)
assert %{"name" => "OPEN", "description" => nil} =
data["__type"]["enumValues"]
|> Enum.find(fn value -> value["name"] == "OPEN" end)
end
test "Ash.Type.Enum value descriptions are used as description source" do
{:ok, %{data: data}} =
"""
query {
__type(name: "EnumWithAshDescription") {
enumValues {
name
description
}
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema)
assert %{"name" => "FIZZ", "description" => "A fizz"} =
data["__type"]["enumValues"]
|> Enum.find(fn value -> value["name"] == "FIZZ" end)
assert %{"name" => "BUZZ", "description" => "A buzz"} =
data["__type"]["enumValues"]
|> Enum.find(fn value -> value["name"] == "BUZZ" end)
end
test "graphql_describe_enum_value/1 overrides Ash.Type.Enum descriptions" do
{:ok, %{data: data}} =
"""
query {
__type(name: "EnumWithAshGraphqlDescription") {
enumValues {
name
description
}
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema)
assert %{"name" => "FOO", "description" => "A foo"} =
data["__type"]["enumValues"]
|> Enum.find(fn value -> value["name"] == "FOO" end)
assert %{"name" => "BAR", "description" => "A bar"} =
data["__type"]["enumValues"]
|> Enum.find(fn value -> value["name"] == "BAR" end)
assert %{"name" => "NO_DESCRIPTION", "description" => nil} =
data["__type"]["enumValues"]
|> Enum.find(fn value -> value["name"] == "NO_DESCRIPTION" end)
end
end