Welcome to mirror list, hosted at ThFree Co, Russian Federation.

batch_key_spec.rb « graphql « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7b73b27f24bb98edab200866efbaeaee358de030 (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
72
73
74
75
76
77
78
79
# frozen_string_literal: true

require 'fast_spec_helper'
require 'test_prof/recipes/rspec/let_it_be'

RSpec.describe ::Gitlab::Graphql::BatchKey do
  let_it_be(:rect) { Struct.new(:len, :width) }
  let_it_be(:circle) { Struct.new(:radius) }

  let(:lookahead) { nil }
  let(:object) { rect.new(2, 3) }

  subject { described_class.new(object, lookahead, object_name: :rect) }

  it 'is equal to keys of the same object, regardless of lookahead or object name' do
    expect(subject).to eq(described_class.new(rect.new(2, 3)))
    expect(subject).to eq(described_class.new(rect.new(2, 3), :anything))
    expect(subject).to eq(described_class.new(rect.new(2, 3), lookahead, object_name: :does_not_matter))
    expect(subject).not_to eq(described_class.new(rect.new(2, 4)))
    expect(subject).not_to eq(described_class.new(circle.new(10)))
  end

  it 'delegates attribute lookup methods to the inner object' do
    other = rect.new(2, 3)

    expect(subject.hash).to eq(other.hash)
    expect(subject.len).to eq(other.len)
    expect(subject.width).to eq(other.width)
  end

  it 'allows the object to be named more meaningfully' do
    expect(subject.object).to eq(object)
    expect(subject.object).to eq(subject.rect)
  end

  it 'works as a hash key' do
    h = { subject => :foo }

    expect(h[described_class.new(object)]).to eq(:foo)
  end

  describe '#requires?' do
    it 'returns false if the lookahead was not provided' do
      expect(subject.requires?([:foo])).to be(false)
    end

    context 'lookahead was provided' do
      let(:lookahead) { double(:Lookahead) }

      before do
        allow(lookahead).to receive(:selection).with(Symbol).and_return(lookahead)
      end

      it 'returns false if the path is empty' do
        expect(subject.requires?([])).to be(false)
      end

      context 'it selects the field' do
        before do
          allow(lookahead).to receive(:selects?).with(Symbol).once.and_return(true)
        end

        it 'returns true' do
          expect(subject.requires?(%i[foo bar baz])).to be(true)
        end
      end

      context 'it does not select the field' do
        before do
          allow(lookahead).to receive(:selects?).with(Symbol).once.and_return(false)
        end

        it 'returns false' do
          expect(subject.requires?(%i[foo bar baz])).to be(false)
        end
      end
    end
  end
end