summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/graphql/connections/keyset/legacy_keyset_connection_spec.rb
blob: aaf28fed68436e987f28856e152a5ec1aaed2f24 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# frozen_string_literal: true

# TODO https://gitlab.com/gitlab-org/gitlab/issues/35104
require 'spec_helper'

describe Gitlab::Graphql::Connections::Keyset::LegacyKeysetConnection do
  describe 'old keyset_connection' do
    let(:described_class) { Gitlab::Graphql::Connections::Keyset::Connection }
    let(:nodes) { Project.all.order(id: :asc) }
    let(:arguments) { {} }
    subject(:connection) do
      described_class.new(nodes, arguments, max_page_size: 3)
    end

    before do
      stub_feature_flags(graphql_keyset_pagination: false)
    end

    def encoded_property(value)
      Base64Bp.urlsafe_encode64(value.to_s, padding: false)
    end

    describe '#cursor_from_nodes' do
      let(:project) { create(:project) }

      it 'returns an encoded ID' do
        expect(connection.cursor_from_node(project))
          .to eq(encoded_property(project.id))
      end

      context 'when an order was specified' do
        let(:nodes) { Project.order(:updated_at) }

        it 'returns the encoded value of the order' do
          expect(connection.cursor_from_node(project))
            .to eq(encoded_property(project.updated_at))
        end
      end
    end

    describe '#sliced_nodes' do
      let(:projects) { create_list(:project, 4) }

      context 'when before is passed' do
        let(:arguments) { { before: encoded_property(projects[1].id) } }

        it 'only returns the project before the selected one' do
          expect(subject.sliced_nodes).to contain_exactly(projects.first)
        end

        context 'when the sort order is descending' do
          let(:nodes) { Project.all.order(id: :desc) }

          it 'returns the correct nodes' do
            expect(subject.sliced_nodes).to contain_exactly(*projects[2..-1])
          end
        end
      end

      context 'when after is passed' do
        let(:arguments) { { after: encoded_property(projects[1].id) } }

        it 'only returns the project before the selected one' do
          expect(subject.sliced_nodes).to contain_exactly(*projects[2..-1])
        end

        context 'when the sort order is descending' do
          let(:nodes) { Project.all.order(id: :desc) }

          it 'returns the correct nodes' do
            expect(subject.sliced_nodes).to contain_exactly(projects.first)
          end
        end
      end

      context 'when both before and after are passed' do
        let(:arguments) do
          {
            after: encoded_property(projects[1].id),
            before: encoded_property(projects[3].id)
          }
        end

        it 'returns the expected set' do
          expect(subject.sliced_nodes).to contain_exactly(projects[2])
        end
      end
    end

    describe '#paged_nodes' do
      let!(:projects) { create_list(:project, 5) }

      it 'returns the collection limited to max page size' do
        expect(subject.paged_nodes.size).to eq(3)
      end

      it 'is a loaded memoized array' do
        expect(subject.paged_nodes).to be_an(Array)
        expect(subject.paged_nodes.object_id).to eq(subject.paged_nodes.object_id)
      end

      context 'when `first` is passed' do
        let(:arguments) { { first: 2 } }

        it 'returns only the first elements' do
          expect(subject.paged_nodes).to contain_exactly(projects.first, projects.second)
        end
      end

      context 'when `last` is passed' do
        let(:arguments) { { last: 2 } }

        it 'returns only the last elements' do
          expect(subject.paged_nodes).to contain_exactly(projects[3], projects[4])
        end
      end

      context 'when both are passed' do
        let(:arguments) { { first: 2, last: 2 } }

        it 'raises an error' do
          expect { subject.paged_nodes }.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
        end
      end
    end
  end
end