summaryrefslogtreecommitdiff
path: root/spec/lib/bulk_imports/common/extractors/graphql_extractor_spec.rb
blob: cde8e2d5c18143b03b0771e9d6bd478dca92bcda (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BulkImports::Common::Extractors::GraphqlExtractor do
  let(:graphql_client) { instance_double(BulkImports::Clients::Graphql) }
  let(:import_entity) { create(:bulk_import_entity) }
  let(:response) { double(original_hash: { foo: :bar }) }
  let(:query) { { query: double(to_s: 'test', variables: {}) } }
  let(:context) do
    instance_double(
      BulkImports::Pipeline::Context,
      entity: import_entity
    )
  end

  subject { described_class.new(query) }

  before do
    allow(subject).to receive(:graphql_client).and_return(graphql_client)
    allow(graphql_client).to receive(:parse)
  end

  describe '#extract' do
    before do
      allow(subject).to receive(:query_variables).and_return({})
      allow(graphql_client).to receive(:execute).and_return(response)
    end

    it 'returns an enumerator with fetched results' do
      response = subject.extract(context)

      expect(response).to be_instance_of(Enumerator)
      expect(response.first).to eq({ foo: :bar })
    end
  end

  describe 'query variables' do
    before do
      allow(graphql_client).to receive(:execute).and_return(response)
    end

    context 'when variables are present' do
      let(:query) { { query: double(to_s: 'test', variables: { full_path: :source_full_path }) } }

      it 'builds graphql query variables for import entity' do
        expected_variables = { full_path: import_entity.source_full_path }

        expect(graphql_client).to receive(:execute).with(anything, expected_variables)

        subject.extract(context).first
      end
    end

    context 'when no variables are present' do
      let(:query) { { query: double(to_s: 'test', variables: nil) } }

      it 'returns empty hash' do
        expect(graphql_client).to receive(:execute).with(anything, nil)

        subject.extract(context).first
      end
    end

    context 'when variables are empty hash' do
      let(:query) { { query: double(to_s: 'test', variables: {}) } }

      it 'makes graphql request with empty hash' do
        expect(graphql_client).to receive(:execute).with(anything, {})

        subject.extract(context).first
      end
    end
  end
end