summaryrefslogtreecommitdiff
path: root/spec/graphql/types/snippets/blob_viewer_type_spec.rb
blob: 8210eb9a95c5eb5409cb37c749cc76722082bf9a (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe GitlabSchema.types['SnippetBlobViewer'] do
  let_it_be(:snippet) { create(:personal_snippet, :repository) }
  let_it_be(:blob) { snippet.repository.blob_at('HEAD', 'files/images/6049019_460s.jpg') }

  it 'has the correct fields' do
    expected_fields = [:type, :load_async, :too_large, :collapsed,
                       :render_error, :file_type, :loading_partial_name]

    expect(described_class).to have_graphql_fields(*expected_fields)
  end

  it { expect(described_class.fields['type'].type).to be_non_null }
  it { expect(described_class.fields['loadAsync'].type).to be_non_null }
  it { expect(described_class.fields['collapsed'].type).to be_non_null }
  it { expect(described_class.fields['tooLarge'].type).to be_non_null }
  it { expect(described_class.fields['renderError'].type).not_to be_non_null }
  it { expect(described_class.fields['fileType'].type).to be_non_null }
  it { expect(described_class.fields['loadingPartialName'].type).to be_non_null }

  shared_examples 'nil field converted to false' do
    subject { GitlabSchema.execute(query, context: { current_user: snippet.author }).as_json }

    before do
      allow_next_instance_of(SnippetPresenter) do |instance|
        allow(instance).to receive(:blob).and_return(blob)
      end
    end

    it 'returns false' do
      snippet_blob = subject.dig('data', 'snippets', 'edges')[0].dig('node', 'blob')

      expect(snippet_blob['path']).to eq blob.path
      expect(blob_attribute).to be_nil
      expect(snippet_blob['simpleViewer'][attribute]).to eq false
    end
  end

  describe 'collapsed' do
    it_behaves_like 'nil field converted to false' do
      let(:query) do
        %(
          query {
            snippets(ids:"#{snippet.to_global_id}"){
              edges {
                node {
                  blob {
                    path
                    simpleViewer {
                      collapsed
                    }
                  }
                }
              }
            }
          }
        )
      end

      let(:attribute) { 'collapsed' }
      let(:blob_attribute) { blob.simple_viewer.collapsed? }
    end
  end

  describe 'tooLarge' do
    it_behaves_like 'nil field converted to false' do
      let(:query) do
        %(
          query {
            snippets(ids:"#{snippet.to_global_id}"){
              edges {
                node {
                  blob {
                    path
                    simpleViewer {
                      tooLarge
                    }
                  }
                }
              }
            }
          }
        )
      end

      let(:attribute) { 'tooLarge' }
      let(:blob_attribute) { blob.simple_viewer.too_large? }
    end
  end
end