diff options
Diffstat (limited to 'spec/lib/gitlab/pagination')
3 files changed, 69 insertions, 13 deletions
diff --git a/spec/lib/gitlab/pagination/cursor_based_keyset_spec.rb b/spec/lib/gitlab/pagination/cursor_based_keyset_spec.rb new file mode 100644 index 00000000000..ac2695977c4 --- /dev/null +++ b/spec/lib/gitlab/pagination/cursor_based_keyset_spec.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Pagination::CursorBasedKeyset do + subject { described_class } + + describe '.available_for_type?' do + it 'returns true for Group' do + expect(subject.available_for_type?(Group.all)).to be_truthy + end + + it 'return false for other types of relations' do + expect(subject.available_for_type?(User.all)).to be_falsey + end + end + + describe '.available?' do + let(:request_context) { double('request_context', params: { order_by: order_by, sort: sort }) } + let(:cursor_based_request_context) { Gitlab::Pagination::Keyset::CursorBasedRequestContext.new(request_context) } + + context 'with order-by name asc' do + let(:order_by) { :name } + let(:sort) { :asc } + + it 'returns true for Group' do + expect(subject.available?(cursor_based_request_context, Group.all)).to be_truthy + end + + it 'return false for other types of relations' do + expect(subject.available?(cursor_based_request_context, User.all)).to be_falsey + end + end + + context 'with other order-by columns' do + let(:order_by) { :path } + let(:sort) { :asc } + + it 'returns false for Group' do + expect(subject.available?(cursor_based_request_context, Group.all)).to be_falsey + end + + it 'return false for other types of relations' do + expect(subject.available?(cursor_based_request_context, User.all)).to be_falsey + end + end + end +end diff --git a/spec/lib/gitlab/pagination/keyset/cursor_based_request_context_spec.rb b/spec/lib/gitlab/pagination/keyset/cursor_based_request_context_spec.rb index 4998abd2186..79de6f230ec 100644 --- a/spec/lib/gitlab/pagination/keyset/cursor_based_request_context_spec.rb +++ b/spec/lib/gitlab/pagination/keyset/cursor_based_request_context_spec.rb @@ -3,32 +3,40 @@ require 'spec_helper' RSpec.describe Gitlab::Pagination::Keyset::CursorBasedRequestContext do - let(:params) { { per_page: 2, cursor: 'eyJuYW1lIjoiR2l0TGFiIEluc3RhbmNlIiwiaWQiOiI1MiIsIl9rZCI6Im4ifQ==' } } - let(:request) { double('request', params: params) } + let(:params) { { per_page: 2, cursor: 'eyJuYW1lIjoiR2l0TGFiIEluc3RhbmNlIiwiaWQiOiI1MiIsIl9rZCI6Im4ifQ==', order_by: :name, sort: :asc } } + let(:request) { double('request', url: 'http://localhost') } + let(:request_context) { double('request_context', header: nil, params: params, request: request) } describe '#per_page' do - subject(:per_page) { described_class.new(request).per_page } + subject(:per_page) { described_class.new(request_context).per_page } it { is_expected.to eq 2 } end describe '#cursor' do - subject(:cursor) { described_class.new(request).cursor } + subject(:cursor) { described_class.new(request_context).cursor } it { is_expected.to eq 'eyJuYW1lIjoiR2l0TGFiIEluc3RhbmNlIiwiaWQiOiI1MiIsIl9rZCI6Im4ifQ==' } end + describe '#order_by' do + subject(:order_by) { described_class.new(request_context).order_by } + + it { is_expected.to eq({ name: :asc }) } + end + describe '#apply_headers' do - let(:request) { double('request', url: "http://#{Gitlab.config.gitlab.host}/api/v4/projects?per_page=3", params: params) } + let(:request) { double('request', url: "http://#{Gitlab.config.gitlab.host}/api/v4/projects?per_page=3") } let(:params) { { per_page: 3 } } + let(:request_context) { double('request_context', header: nil, params: params, request: request) } let(:cursor_for_next_page) { 'eyJuYW1lIjoiSDVicCIsImlkIjoiMjgiLCJfa2QiOiJuIn0=' } - subject(:apply_headers) { described_class.new(request).apply_headers(cursor_for_next_page) } + subject(:apply_headers) { described_class.new(request_context).apply_headers(cursor_for_next_page) } it 'sets Link header with same host/path as the original request' do - orig_uri = URI.parse(request.url) + orig_uri = URI.parse(request_context.request.url) - expect(request).to receive(:header).once do |name, header| + expect(request_context).to receive(:header).once do |name, header| first_link, _ = /<([^>]+)>; rel="next"/.match(header).captures uri = URI.parse(first_link) @@ -42,9 +50,9 @@ RSpec.describe Gitlab::Pagination::Keyset::CursorBasedRequestContext do end it 'sets Link header with a cursor to the next page' do - orig_uri = URI.parse(request.url) + orig_uri = URI.parse(request_context.request.url) - expect(request).to receive(:header).once do |name, header| + expect(request_context).to receive(:header).once do |name, header| first_link, _ = /<([^>]+)>; rel="next"/.match(header).captures query = CGI.parse(URI.parse(first_link).query) diff --git a/spec/lib/gitlab/pagination/keyset/cursor_pager_spec.rb b/spec/lib/gitlab/pagination/keyset/cursor_pager_spec.rb index 5ba381834ea..783e728b34c 100644 --- a/spec/lib/gitlab/pagination/keyset/cursor_pager_spec.rb +++ b/spec/lib/gitlab/pagination/keyset/cursor_pager_spec.rb @@ -6,8 +6,8 @@ RSpec.describe Gitlab::Pagination::Keyset::CursorPager do let(:relation) { Group.all.order(:name, :id) } let(:per_page) { 3 } let(:params) { { cursor: nil, per_page: per_page } } - let(:request) { double('request', params: params) } - let(:cursor_based_request_context) { Gitlab::Pagination::Keyset::CursorBasedRequestContext.new(request) } + let(:request_context) { double('request_context', params: params) } + let(:cursor_based_request_context) { Gitlab::Pagination::Keyset::CursorBasedRequestContext.new(request_context) } before_all do create_list(:group, 7) @@ -33,7 +33,7 @@ RSpec.describe Gitlab::Pagination::Keyset::CursorPager do it 'passes information about next page to request' do cursor_for_next_page = relation.keyset_paginate(**params).cursor_for_next_page - expect_next_instance_of(Gitlab::Pagination::Keyset::HeaderBuilder, cursor_based_request_context) do |builder| + expect_next_instance_of(Gitlab::Pagination::Keyset::HeaderBuilder, request_context) do |builder| expect(builder).to receive(:add_next_page_header).with({ cursor: cursor_for_next_page }) end |