Refactor GraphQL::TypeCreator specs

This commit is contained in:
Alberto Miedes Garcés
2016-12-02 21:53:36 +01:00
parent c973195267
commit a0f1976c1a

View File

@@ -3,18 +3,8 @@ require 'rails_helper'
describe GraphQL::TypeCreator do describe GraphQL::TypeCreator do
let(:type_creator) { GraphQL::TypeCreator.new } let(:type_creator) { GraphQL::TypeCreator.new }
#let(:api_types) { {} }
#let!(:user_type) { GraphQL::TypeCreator.create(User, %I[ id ], api_types) }
#let!(:comment_type) { GraphQL::TypeCreator.create(Comment, %I[ id ], api_types) }
#let!(:debate_type) { GraphQL::TypeCreator.create(Debate, %I[ id title author ], api_types) }
# TODO: no puedo añadir los comentarios a la field_list de Debate porque como
# las conexiones se crean de forma lazy creo que provoca que falle la creación
# del resto de tipos y provoca que fallen todos los tests.
# let!(:debate_type) { GraphQL::TypeCreator.create(Debate, %I[ id title author comments ], api_types) }
describe "::create" do describe "::create" do
describe "creates fields" do it "creates fields for Int attributes" do
it "for int attributes" do
debate_type = type_creator.create(Debate, %I[ id ]) debate_type = type_creator.create(Debate, %I[ id ])
created_field = debate_type.fields['id'] created_field = debate_type.fields['id']
@@ -23,56 +13,46 @@ describe GraphQL::TypeCreator do
expect(created_field.type.name).to eq('Int') expect(created_field.type.name).to eq('Int')
end end
it "for string attributes" do it "creates fields for String attributes" do
skip debate_type = type_creator.create(Debate, %I[ title ])
created_field = debate_type.fields['title'] created_field = debate_type.fields['title']
expect(created_field).to be_a(GraphQL::Field) expect(created_field).to be_a(GraphQL::Field)
expect(created_field.type).to be_a(GraphQL::ScalarType) expect(created_field.type).to be_a(GraphQL::ScalarType)
expect(created_field.type.name).to eq('String') expect(created_field.type.name).to eq('String')
end end
end
describe "creates connections for" do it "creates connections for :belongs_to associations" do
it ":belongs_to associations" do
user_type = type_creator.create(User, %I[ id ]) user_type = type_creator.create(User, %I[ id ])
debate_type = type_creator.create(Debate, %I[ author ]) debate_type = type_creator.create(Debate, %I[ author ])
connection = debate_type.fields['author'] connection = debate_type.fields['author']
# TODO: because connection types are created and added lazily to the
# api_types hash (with that proc thing ->) I don't really know how to
# test this.
# connection.class shows GraphQL::Field
# connection.inspect shows some weird info
expect(connection).to be_a(GraphQL::Field) expect(connection).to be_a(GraphQL::Field)
#debugger
expect(connection.type).to eq(user_type) expect(connection.type).to eq(user_type)
expect(connection.name).to eq('author') expect(connection.name).to eq('author')
end end
it ":has_one associations" do it "creates connections for :has_one associations" do
skip "need to find association example that uses :has_one" user_type = type_creator.create(User, %I[ organization ])
organization_type = type_creator.create(Organization, %I[ id ])
connection = user_type.fields['organization']
expect(connection).to be_a(GraphQL::Field)
expect(connection.type).to eq(organization_type)
expect(connection.name).to eq('organization')
end end
it ":has_many associations" do it "creates connections for :has_many associations" do
#skip "still don't know how to handle relay connections inside RSpec"
comment_type = type_creator.create(Comment, %I[ id ]) comment_type = type_creator.create(Comment, %I[ id ])
debate_type = type_creator.create(Debate, %I[ author ]) debate_type = type_creator.create(Debate, %I[ comments ])
connection = debate_type.fields['comments'] connection = debate_type.fields['comments']
# TODO: because connection types are created and added lazily to the
# api_types hash (with that proc thing ->) I don't really know how to
# test this.
# connection.class shows GraphQL::Field
# connection.inspect shows some weird info
expect(connection).to be_a(GraphQL::Field) expect(connection).to be_a(GraphQL::Field)
expect(connection.type).to be_a(api_types[Comment]) expect(connection.type).to eq(comment_type.connection_type)
expect(connection.name).to eq('comments') expect(connection.name).to eq('comments')
end end
end end
end
end end