summaryrefslogtreecommitdiff
path: root/spec/support_specs/graphql/arguments_spec.rb
blob: ffb58503a0e3d45b8c8c97d619e6c54dbdfe3939 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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