From 9cf73b708e9b58aa359c60ae558963ad1e3be244 Mon Sep 17 00:00:00 2001 From: Joao P Dubas Date: Sat, 26 Jun 2021 19:09:01 +0000 Subject: [PATCH] [day-05] test graphql schema --- test/wabanex_web/schema_test.exs | 68 ++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 test/wabanex_web/schema_test.exs diff --git a/test/wabanex_web/schema_test.exs b/test/wabanex_web/schema_test.exs new file mode 100644 index 0000000..25194e2 --- /dev/null +++ b/test/wabanex_web/schema_test.exs @@ -0,0 +1,68 @@ +defmodule WabanexWeb.SchemaTest do + use WabanexWeb.ConnCase, async: true + + alias Wabanex.{User, Users} + + describe "users queries" do + test "when a valid id is given returns the user", %{conn: conn} do + params = %{name: "Joe Doe", email: "joe.doe@anywhere.com", password: "aLongEnough"} + + {:ok, %User{id: user_id}} = Users.Create.call(params) + + query = """ + query TestGetUser($id: UUID4!) { + getUser(id: $id) { + id + name + email + } + } + """ + + response = + conn + |> post("/api/graphql", %{query: query, variables: %{"id" => user_id}}) + |> json_response(:ok) + + expected_response = %{ + "data" => %{ + "getUser" => %{"id" => user_id, "name" => "Joe Doe", "email" => "joe.doe@anywhere.com"} + } + } + + assert expected_response == response + end + end + + describe "user mutations" do + test "when all params are valid creates an user", %{conn: conn} do + mutation = """ + mutation TestCreateUser($input: CreateUserInput!) { + createUser(input: $input) { + name + email + } + } + """ + + input = %{ + "input" => %{ + "name" => "Joe Doe", + "email" => "joe.doe@nowhere.com", + "password" => "aLongEnough" + } + } + + response = + conn + |> post("/api/graphql", %{query: mutation, variables: input}) + |> json_response(:ok) + + expected_response = %{ + "data" => %{"createUser" => %{"name" => "Joe Doe", "email" => "joe.doe@nowhere.com"}} + } + + assert expected_response == response + end + end +end