From a0f1976c1adba3de67c6204dec2df1324d5fa34a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Fri, 2 Dec 2016 21:53:36 +0100 Subject: [PATCH] Refactor GraphQL::TypeCreator specs --- spec/lib/type_creator_spec.rb | 96 ++++++++++++++--------------------- 1 file changed, 38 insertions(+), 58 deletions(-) diff --git a/spec/lib/type_creator_spec.rb b/spec/lib/type_creator_spec.rb index 4ad667812..2fc1bf78d 100644 --- a/spec/lib/type_creator_spec.rb +++ b/spec/lib/type_creator_spec.rb @@ -3,76 +3,56 @@ require 'rails_helper' describe GraphQL::TypeCreator do 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 "creates fields" do - it "for int attributes" do - debate_type = type_creator.create(Debate, %I[ id ]) - created_field = debate_type.fields['id'] + it "creates fields for Int attributes" do + debate_type = type_creator.create(Debate, %I[ id ]) + created_field = debate_type.fields['id'] - expect(created_field).to be_a(GraphQL::Field) - expect(created_field.type).to be_a(GraphQL::ScalarType) - expect(created_field.type.name).to eq('Int') - end - - it "for string attributes" do - skip - created_field = debate_type.fields['title'] - - expect(created_field).to be_a(GraphQL::Field) - expect(created_field.type).to be_a(GraphQL::ScalarType) - expect(created_field.type.name).to eq('String') - end + expect(created_field).to be_a(GraphQL::Field) + expect(created_field.type).to be_a(GraphQL::ScalarType) + expect(created_field.type.name).to eq('Int') end - describe "creates connections for" do - it ":belongs_to associations" do - user_type = type_creator.create(User, %I[ id ]) - debate_type = type_creator.create(Debate, %I[ author ]) + it "creates fields for String attributes" do + debate_type = type_creator.create(Debate, %I[ title ]) + created_field = debate_type.fields['title'] - connection = debate_type.fields['author'] + expect(created_field).to be_a(GraphQL::Field) + expect(created_field.type).to be_a(GraphQL::ScalarType) + expect(created_field.type.name).to eq('String') + end - # 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 + it "creates connections for :belongs_to associations" do + user_type = type_creator.create(User, %I[ id ]) + debate_type = type_creator.create(Debate, %I[ author ]) - expect(connection).to be_a(GraphQL::Field) - #debugger - expect(connection.type).to eq(user_type) - expect(connection.name).to eq('author') - end + connection = debate_type.fields['author'] - it ":has_one associations" do - skip "need to find association example that uses :has_one" - end + expect(connection).to be_a(GraphQL::Field) + expect(connection.type).to eq(user_type) + expect(connection.name).to eq('author') + end - it ":has_many associations" do - #skip "still don't know how to handle relay connections inside RSpec" - comment_type = type_creator.create(Comment, %I[ id ]) - debate_type = type_creator.create(Debate, %I[ author ]) + it "creates connections for :has_one associations" do + user_type = type_creator.create(User, %I[ organization ]) + organization_type = type_creator.create(Organization, %I[ id ]) - connection = debate_type.fields['comments'] + connection = user_type.fields['organization'] - # 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.type).to eq(organization_type) + expect(connection.name).to eq('organization') + end - expect(connection).to be_a(GraphQL::Field) - expect(connection.type).to be_a(api_types[Comment]) - expect(connection.name).to eq('comments') - end + it "creates connections for :has_many associations" do + comment_type = type_creator.create(Comment, %I[ id ]) + debate_type = type_creator.create(Debate, %I[ comments ]) + + connection = debate_type.fields['comments'] + + expect(connection).to be_a(GraphQL::Field) + expect(connection.type).to eq(comment_type.connection_type) + expect(connection.name).to eq('comments') end end end