Commit 22f9fdf4 authored by Adam Hegyi's avatar Adam Hegyi

Merge branch 'nfriend-fix-releases-graphql-pagination' into 'master'

Fix Releases page pagination

See merge request gitlab-org/gitlab!62256
parents c9b524d7 52341a0b
......@@ -39,10 +39,10 @@ class Release < ApplicationRecord
scope :released_within_2hrs, -> { where(released_at: Time.zone.now - 1.hour..Time.zone.now + 1.hour) }
# Sorting
scope :order_created, -> { reorder('created_at ASC') }
scope :order_created_desc, -> { reorder('created_at DESC') }
scope :order_released, -> { reorder('released_at ASC') }
scope :order_released_desc, -> { reorder('released_at DESC') }
scope :order_created, -> { reorder(created_at: :asc) }
scope :order_created_desc, -> { reorder(created_at: :desc) }
scope :order_released, -> { reorder(released_at: :asc) }
scope :order_released_desc, -> { reorder(released_at: :desc) }
delegate :repository, to: :project
......
......@@ -121,10 +121,10 @@ Object {
},
],
"paginationInfo": Object {
"endCursor": "eyJpZCI6IjEifQ",
"endCursor": "eyJyZWxlYXNlZF9hdCI6IjIwMTgtMTItMTAgMDA6MDA6MDAuMDAwMDAwMDAwIFVUQyIsImlkIjoiMSJ9",
"hasNextPage": false,
"hasPreviousPage": false,
"startCursor": "eyJpZCI6IjEifQ",
"startCursor": "eyJyZWxlYXNlZF9hdCI6IjIwMTgtMTItMTAgMDA6MDA6MDAuMDAwMDAwMDAwIFVUQyIsImlkIjoiMSJ9",
},
}
`;
......
......@@ -295,75 +295,69 @@ RSpec.describe 'Query.project(fullPath).releases()' do
end
end
describe 'sorting behavior' do
let_it_be(:today) { Time.now }
let_it_be(:yesterday) { today - 1.day }
let_it_be(:tomorrow) { today + 1.day }
describe 'sorting and pagination' do
let_it_be(:sort_project) { create(:project, :public) }
let_it_be(:project) { create(:project, :repository, :public) }
let(:data_path) { [:project, :releases] }
let(:current_user) { developer }
let_it_be(:release_v1) { create(:release, project: project, tag: 'v1', released_at: yesterday, created_at: tomorrow) }
let_it_be(:release_v2) { create(:release, project: project, tag: 'v2', released_at: today, created_at: yesterday) }
let_it_be(:release_v3) { create(:release, project: project, tag: 'v3', released_at: tomorrow, created_at: today) }
let(:current_user) { developer }
let(:params) { nil }
let(:sorted_tags) do
graphql_data.dig('project', 'releases', 'nodes').map { |release| release['tagName'] }
end
let(:query) do
graphql_query_for(:project, { fullPath: project.full_path },
%{
releases#{params ? "(#{params})" : ""} {
nodes {
tagName
}
}
})
end
before do
post_query
def pagination_query(params)
graphql_query_for(
:project,
{ full_path: sort_project.full_path },
query_graphql_field(:releases, params, "#{page_info} nodes { tagName }")
)
end
context 'when no sort: parameter is provided' do
it 'returns the results with the default sort applied (sort: RELEASED_AT_DESC)' do
expect(sorted_tags).to eq(%w(v3 v2 v1))
end
def pagination_results_data(nodes)
nodes.map { |release| release['tagName'] }
end
context 'with sort: RELEASED_AT_DESC' do
let(:params) { 'sort: RELEASED_AT_DESC' }
it 'returns the releases ordered by released_at in descending order' do
expect(sorted_tags).to eq(%w(v3 v2 v1))
context 'when sorting by released_at' do
let_it_be(:release5) { create(:release, project: sort_project, tag: 'v5.5.0', released_at: 3.days.from_now) }
let_it_be(:release1) { create(:release, project: sort_project, tag: 'v5.1.0', released_at: 3.days.ago) }
let_it_be(:release4) { create(:release, project: sort_project, tag: 'v5.4.0', released_at: 2.days.from_now) }
let_it_be(:release2) { create(:release, project: sort_project, tag: 'v5.2.0', released_at: 2.days.ago) }
let_it_be(:release3) { create(:release, project: sort_project, tag: 'v5.3.0', released_at: 1.day.ago) }
context 'when ascending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { :RELEASED_AT_ASC }
let(:first_param) { 2 }
let(:expected_results) { [release1.tag, release2.tag, release3.tag, release4.tag, release5.tag] }
end
end
end
context 'with sort: RELEASED_AT_ASC' do
let(:params) { 'sort: RELEASED_AT_ASC' }
it 'returns the releases ordered by released_at in ascending order' do
expect(sorted_tags).to eq(%w(v1 v2 v3))
context 'when descending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { :RELEASED_AT_DESC }
let(:first_param) { 2 }
let(:expected_results) { [release5.tag, release4.tag, release3.tag, release2.tag, release1.tag] }
end
end
end
context 'with sort: CREATED_DESC' do
let(:params) { 'sort: CREATED_DESC' }
it 'returns the releases ordered by created_at in descending order' do
expect(sorted_tags).to eq(%w(v1 v3 v2))
context 'when sorting by created_at' do
let_it_be(:release5) { create(:release, project: sort_project, tag: 'v5.5.0', created_at: 3.days.from_now) }
let_it_be(:release1) { create(:release, project: sort_project, tag: 'v5.1.0', created_at: 3.days.ago) }
let_it_be(:release4) { create(:release, project: sort_project, tag: 'v5.4.0', created_at: 2.days.from_now) }
let_it_be(:release2) { create(:release, project: sort_project, tag: 'v5.2.0', created_at: 2.days.ago) }
let_it_be(:release3) { create(:release, project: sort_project, tag: 'v5.3.0', created_at: 1.day.ago) }
context 'when ascending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { :CREATED_ASC }
let(:first_param) { 2 }
let(:expected_results) { [release1.tag, release2.tag, release3.tag, release4.tag, release5.tag] }
end
end
end
context 'with sort: CREATED_ASC' do
let(:params) { 'sort: CREATED_ASC' }
it 'returns the releases ordered by created_at in ascending order' do
expect(sorted_tags).to eq(%w(v2 v3 v1))
context 'when descending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { :CREATED_DESC }
let(:first_param) { 2 }
let(:expected_results) { [release5.tag, release4.tag, release3.tag, release2.tag, release1.tag] }
end
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment