spec/graphql/schema/object_spec.rb in graphql-1.8.0.pre10 vs spec/graphql/schema/object_spec.rb in graphql-1.8.0.pre11
- old
+ new
@@ -6,11 +6,11 @@
let(:object_class) { Jazz::Ensemble }
it "tells type data" do
assert_equal "Ensemble", object_class.graphql_name
assert_equal "A group of musicians playing together", object_class.description
assert_equal 6, object_class.fields.size
- assert_equal 2, object_class.interfaces.size
+ assert_equal 3, object_class.interfaces.size
# Compatibility methods are delegated to the underlying BaseType
assert object_class.respond_to?(:connection_type)
end
it "inherits fields and interfaces" do
@@ -20,11 +20,11 @@
end
# one more than the parent class
assert_equal 7, new_object_class.fields.size
# inherited interfaces are present
- assert_equal 2, new_object_class.interfaces.size
+ assert_equal 3, new_object_class.interfaces.size
# The new field is present
assert new_object_class.fields.key?("newField")
# The overridden field is present:
name_field = new_object_class.fields["name"]
assert_equal "The new description", name_field.description
@@ -38,29 +38,51 @@
new_subclass_2 = Class.new(new_subclass_1)
assert_equal "NewSubclass", new_subclass_1.graphql_name
assert_equal "NewSubclass", new_subclass_2.graphql_name
assert_equal object_class.description, new_subclass_2.description
end
+
+ it "should take Ruby name (without Type suffix) as default graphql name" do
+ TestingClassType = Class.new(GraphQL::Schema::Object)
+ assert_equal "TestingClass", TestingClassType.graphql_name
+ end
+
+ it "raise on anonymous class without declared graphql name" do
+ anonymous_class = Class.new(GraphQL::Schema::Object)
+ assert_raises NotImplementedError do
+ anonymous_class.graphql_name
+ end
+ end
end
describe "wrapping a Hash" do
it "automatically looks up symbol and string keys" do
query_str = <<-GRAPHQL
{
hashyEnsemble {
- name
musicians { name }
formedAt
}
}
GRAPHQL
res = Jazz::Schema.execute(query_str)
ensemble = res["data"]["hashyEnsemble"]
- assert_equal "The Grateful Dead", ensemble["name"]
assert_equal ["Jerry Garcia"], ensemble["musicians"].map { |m| m["name"] }
assert_equal "May 5, 1965", ensemble["formedAt"]
end
+
+ it "works with strings and symbols" do
+ query_str = <<-GRAPHQL
+ {
+ hashByString { falsey }
+ hashBySym { falsey }
+ }
+ GRAPHQL
+ res = Jazz::Schema.execute(query_str)
+ assert_equal false, res["data"]["hashByString"]["falsey"]
+ assert_equal false, res["data"]["hashBySym"]["falsey"]
+ end
end
describe ".to_graphql_type" do
let(:obj_type) { Jazz::Ensemble.to_graphql }
it "returns a matching GraphQL::ObjectType" do
@@ -84,11 +106,11 @@
ensembles { upcaseName }
}
GRAPHQL
res = Jazz::Schema.execute(query_str)
- assert_equal ["BELA FLECK AND THE FLECKTONES"], res["data"]["ensembles"].map { |e| e["upcaseName"] }
+ assert_equal ["BELA FLECK AND THE FLECKTONES", "ROBERT GLASPER EXPERIMENT"], res["data"]["ensembles"].map { |e| e["upcaseName"] }
end
end
describe "in queries" do
@@ -102,11 +124,15 @@
ensembles { name }
instruments { name }
}
GRAPHQL
res = Jazz::Schema.execute(query_str)
- assert_equal [{"name" => "Bela Fleck and the Flecktones"}], res["data"]["ensembles"]
+ expected_ensembles = [
+ {"name" => "Bela Fleck and the Flecktones"},
+ {"name" => "ROBERT GLASPER Experiment"},
+ ]
+ assert_equal expected_ensembles, res["data"]["ensembles"]
assert_equal({"name" => "Banjo"}, res["data"]["instruments"].first)
end
it "does mutations" do
mutation_str = <<-GRAPHQL
@@ -125,13 +151,13 @@
}
}
}
GRAPHQL
- res = Jazz::Schema.execute(mutation_str, variables: { "name" => "Miles Davis Quartet" })
+ res = Jazz::Schema.execute(mutation_str, variables: { name: "Miles Davis Quartet" })
new_id = res["data"]["addEnsemble"]["id"]
- res2 = Jazz::Schema.execute(query_str, variables: { "id" => new_id })
+ res2 = Jazz::Schema.execute(query_str, variables: { id: new_id })
assert_equal "Miles Davis Quartet", res2["data"]["find"]["name"]
end
it "initializes root wrappers once" do
query_str = " { oid1: objectId oid2: objectId }"