diff options
Diffstat (limited to 'spec/support_specs/graphql')
-rw-r--r-- | spec/support_specs/graphql/arguments_spec.rb | 71 | ||||
-rw-r--r-- | spec/support_specs/graphql/field_selection_spec.rb | 62 | ||||
-rw-r--r-- | spec/support_specs/graphql/var_spec.rb | 34 |
3 files changed, 167 insertions, 0 deletions
diff --git a/spec/support_specs/graphql/arguments_spec.rb b/spec/support_specs/graphql/arguments_spec.rb new file mode 100644 index 00000000000..ffb58503a0e --- /dev/null +++ b/spec/support_specs/graphql/arguments_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Graphql::Arguments do + it 'returns a blank string if the arguments are blank' do + args = described_class.new({}) + + expect("#{args}").to be_blank + end + + it 'returns a serialized arguments if the arguments are not blank' do + units = described_class.new({ temp: :CELSIUS, time: :MINUTES }) + args = described_class.new({ temp: 180, time: 45, units: units }) + + expect("#{args}").to eq('temp: 180, time: 45, units: {temp: CELSIUS, time: MINUTES}') + end + + it 'supports merge with +' do + lhs = described_class.new({ a: 1, b: 2 }) + rhs = described_class.new({ b: 3, c: 4 }) + + expect(lhs + rhs).to eq({ a: 1, b: 3, c: 4 }) + end + + it 'supports merge with + and a string' do + lhs = described_class.new({ a: 1, b: 2 }) + rhs = 'x: no' + + expect(lhs + rhs).to eq('a: 1, b: 2, x: no') + end + + it 'supports merge with + and a string when empty' do + lhs = described_class.new({}) + rhs = 'x: no' + + expect(lhs + rhs).to eq('x: no') + end + + it 'supports merge with + and an empty string' do + lhs = described_class.new({ a: 1 }) + rhs = '' + + expect(lhs + rhs).to eq({ a: 1 }) + end + + it 'serializes all values correctly' do + args = described_class.new({ + array: [1, 2.5, "foo", nil, true, false, :BAR, { power: :on }], + hash: { a: 1, b: 2, c: 3 }, + int: 42, + float: 2.7, + string: %q[he said "no"], + enum: :OFF, + null: nil, # we expect this to be omitted - absence is the same as explicit nullness + bool_true: true, + bool_false: false, + var: ::Graphql::Var.new('x', 'Int') + }) + + expect(args.to_s).to eq([ + %q(array: [1,2.5,"foo",null,true,false,BAR,{power: on}]), + %q(hash: {a: 1, b: 2, c: 3}), + 'int: 42, float: 2.7', + %q(string: "he said \\"no\\""), + 'enum: OFF', + 'boolTrue: true, boolFalse: false', + 'var: $x' + ].join(', ')) + end +end diff --git a/spec/support_specs/graphql/field_selection_spec.rb b/spec/support_specs/graphql/field_selection_spec.rb new file mode 100644 index 00000000000..8818e59e598 --- /dev/null +++ b/spec/support_specs/graphql/field_selection_spec.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Graphql::FieldSelection do + it 'can report on the paths that are selected' do + selection = described_class.new({ + 'foo' => nil, + 'bar' => nil, + 'quux' => { + 'a' => nil, + 'b' => { 'x' => nil, 'y' => nil } + }, + 'qoox' => { + 'q' => nil, + 'r' => { 's' => { 't' => nil } } + } + }) + + expect(selection.paths).to include( + %w[foo], + %w[quux a], + %w[quux b x], + %w[qoox r s t] + ) + end + + it 'can serialize a field selection nicely' do + selection = described_class.new({ + 'foo' => nil, + 'bar' => nil, + 'quux' => { + 'a' => nil, + 'b' => { 'x' => nil, 'y' => nil } + }, + 'qoox' => { + 'q' => nil, + 'r' => { 's' => { 't' => nil } } + } + }) + + expect(selection.to_s).to eq(<<~FRAG.strip) + foo + bar + quux { + a + b { + x + y + } + } + qoox { + q + r { + s { + t + } + } + } + FRAG + end +end diff --git a/spec/support_specs/graphql/var_spec.rb b/spec/support_specs/graphql/var_spec.rb new file mode 100644 index 00000000000..f708f01a11e --- /dev/null +++ b/spec/support_specs/graphql/var_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe ::Graphql::Var do + subject(:var) { described_class.new('foo', 'Int') } + + it 'associates a name with a type and an initially empty value' do + expect(var).to have_attributes( + name: 'foo', + type: 'Int', + value: be_nil + ) + end + + it 'has a correct signature' do + expect(var).to have_attributes(sig: '$foo: Int') + end + + it 'implements to_graphql_value as $name' do + expect(var.to_graphql_value).to eq('$foo') + end + + it 'can set a value using with, returning a new object' do + with_value = var.with(42) + + expect(with_value).to have_attributes(name: 'foo', type: 'Int', value: 42) + expect(var).to have_attributes(value: be_nil) + end + + it 'returns an object suitable for passing to post_graphql(variables:)' do + expect(var.with(17).to_h).to eq('foo' => 17) + end +end |