Commit 61d280e3 authored by Alex Kalderimis's avatar Alex Kalderimis

GraphqlHelpers: Add tests for graphql-helper changes

parent 78868afe
......@@ -192,7 +192,7 @@ module GraphqlHelpers
"pageInfo { hasNextPage hasPreviousPage endCursor startCursor }"
end
def query_nodes(name, args = nil, fields = nil, of: name, include_pagination_info: false, max_depth: 1)
def query_nodes(name, fields = nil, args: nil, of: name, include_pagination_info: false, max_depth: 1)
fields ||= all_graphql_fields_for(of.to_s.classify, max_depth: max_depth)
node_selection = include_pagination_info ? "#{page_info_selection} nodes" : :nodes
query_graphql_path([[name, args], node_selection], fields)
......@@ -327,7 +327,7 @@ module GraphqlHelpers
end
def to_h
{ name.to_sym => value }
{ name => value }
end
end
......
......@@ -5,6 +5,188 @@ require 'spec_helper'
RSpec.describe GraphqlHelpers do
include GraphqlHelpers
# Normalize irrelevant whitespace to make comparison easier
def norm(query)
query.tr("\n", ' ').gsub(/\s+/, ' ').strip
end
describe ::GraphqlHelpers::Var do
it 'allocates a fresh name for each var' do
a = var('Int')
b = var('Int')
expect(a.name).not_to eq(b.name)
end
it 'can be used to construct correct signatures' do
a = var('Int')
b = var('String!')
q = with_signature([a, b], '{ foo bar }')
expect(q).to eq("query(#{a.to_graphql_value}: Int, #{b.to_graphql_value}: String!) { foo bar }")
end
it 'can be used to pass arguments to fields' do
a = var('ID!')
q = graphql_query_for(:project, { full_path: a }, :id)
expect(norm(q)).to eq("{ project(fullPath: #{a.to_graphql_value}){ id } }")
end
it 'can associate values with variables' do
a = var('Int')
expect(a.with(3).to_h).to eq(a.name => 3)
end
it 'does not mutate the variable when providing a value' do
a = var('Int')
three = a.with(3)
expect(three.value).to eq(3)
expect(a.value).to be_nil
end
it 'can associate many values with variables' do
a = var('Int').with(3)
b = var('String').with('foo')
expect(serialize_variables([a, b])).to eq({ a.name => 3, b.name => 'foo' }.to_json)
end
end
describe '.query_nodes' do
it 'can produce a basic connection selection' do
selection = query_nodes(:users)
expected = query_graphql_path([:users, :nodes], all_graphql_fields_for('User', max_depth: 1))
expect(selection).to eq(expected)
end
it 'allows greater depth' do
selection = query_nodes(:users, max_depth: 2)
expected = query_graphql_path([:users, :nodes], all_graphql_fields_for('User', max_depth: 2))
expect(selection).to eq(expected)
end
it 'accepts fields' do
selection = query_nodes(:users, :id)
expected = query_graphql_path([:users, :nodes], :id)
expect(selection).to eq(expected)
end
it 'accepts arguments' do
args = { username: 'foo' }
selection = query_nodes(:users, args: args)
expected = query_graphql_path([[:users, args], :nodes], all_graphql_fields_for('User', max_depth: 1))
expect(selection).to eq(expected)
end
it 'accepts arguments and fields' do
selection = query_nodes(:users, :id, args: { username: 'foo' })
expected = query_graphql_path([[:users, { username: 'foo' }], :nodes], :id)
expect(selection).to eq(expected)
end
it 'accepts explicit type name' do
selection = query_nodes(:members, of: 'User')
expected = query_graphql_path([:members, :nodes], all_graphql_fields_for('User', max_depth: 1))
expect(selection).to eq(expected)
end
it 'can optionally provide pagination info' do
selection = query_nodes(:users, include_pagination_info: true)
expected = query_graphql_path([:users, "#{page_info_selection} nodes"], all_graphql_fields_for('User', max_depth: 1))
expect(selection).to eq(expected)
end
end
describe '.query_graphql_path' do
it 'can build nested paths' do
selection = query_graphql_path(%i[foo bar wibble_wobble], :id)
expected = norm(<<-GQL)
foo{
bar{
wibbleWobble{
id
}
}
}
GQL
expect(norm(selection)).to eq(expected)
end
it 'can insert arguments at any point' do
selection = query_graphql_path(
[:foo, [:bar, { quux: true }], [:wibble_wobble, { eccentricity: :HIGH }]],
:id
)
expected = norm(<<-GQL)
foo{
bar(quux: true){
wibbleWobble(eccentricity: HIGH){
id
}
}
}
GQL
expect(norm(selection)).to eq(expected)
end
end
describe '.attributes_to_graphql' do
it 'can serialize hashes to literal arguments' do
x = var('Int')
args = {
an_array: [1, nil, "foo", true, [:foo, :bar]],
a_hash: {
nested: true,
value: "bar"
},
an_int: 42,
a_float: 0.1,
a_string: "wibble",
an_enum: :LOW,
null: nil,
a_bool: false,
a_var: x
}
literal = attributes_to_graphql(args)
expect(norm(literal)).to eq(norm(<<~EXP))
anArray: [1,null,"foo",true,[foo,bar]],
aHash: {nested: true, value: "bar"},
anInt: 42,
aFloat: 0.1,
aString: "wibble",
anEnum: LOW,
null: null,
aBool: false,
aVar: #{x.to_graphql_value}
EXP
end
end
describe '.graphql_mutation' do
shared_examples 'correct mutation definition' do
it 'returns correct mutation definition' do
......@@ -15,7 +197,7 @@ RSpec.describe GraphqlHelpers do
}
}
MUTATION
variables = %q({"updateAlertStatusInput":{"projectPath":"test/project"}})
variables = { "updateAlertStatusInput" => { "projectPath" => "test/project" } }
is_expected.to eq(GraphqlHelpers::MutationDefinition.new(query, variables))
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment