Commit 8610f33d authored by Brett Walker's avatar Brett Walker Committed by Michael Kozono

Use shared pagination specs

for board lists
parent b47db5cb
...@@ -5,8 +5,8 @@ require 'spec_helper' ...@@ -5,8 +5,8 @@ require 'spec_helper'
RSpec.describe 'get board lists' do RSpec.describe 'get board lists' do
include GraphqlHelpers include GraphqlHelpers
let_it_be(:user) { create(:user) } let_it_be(:current_user) { create(:user) }
let_it_be(:project) { create(:project, creator_id: user.id, namespace: user.namespace ) } let_it_be(:project) { create(:project, creator_id: current_user.id, namespace: current_user.namespace ) }
let_it_be(:group) { create(:group, :private) } let_it_be(:group) { create(:group, :private) }
let_it_be(:project_milestone) { create(:milestone, project: project) } let_it_be(:project_milestone) { create(:milestone, project: project) }
let_it_be(:project_milestone2) { create(:milestone, project: project) } let_it_be(:project_milestone2) { create(:milestone, project: project) }
...@@ -58,46 +58,45 @@ RSpec.describe 'get board lists' do ...@@ -58,46 +58,45 @@ RSpec.describe 'get board lists' do
context 'when user can read the board' do context 'when user can read the board' do
before do before do
board_parent.add_reporter(user) board_parent.add_reporter(current_user)
end end
describe 'sorting and pagination' do describe 'sorting and pagination' do
let(:data_path) { [board_parent_type, :boards, :edges, 0, :node, :lists] }
def pagination_query(params, page_info)
graphql_query_for(
board_parent_type,
{ 'fullPath' => board_parent.full_path },
<<~BOARDS
boards(first: 1) {
edges {
node {
#{query_graphql_field('lists', params, "#{page_info} edges { node { id } }")}
}
}
}
BOARDS
)
end
def pagination_results_data(data)
data.map { |list| list.dig('node', 'id') }
end
context 'when using default sorting' do context 'when using default sorting' do
let!(:milestone_list) { create(:milestone_list, board: board, milestone: milestone, position: 10) } let!(:milestone_list) { create(:milestone_list, board: board, milestone: milestone, position: 10) }
let!(:milestone_list2) { create(:milestone_list, board: board, milestone: milestone2, position: 2) } let!(:milestone_list2) { create(:milestone_list, board: board, milestone: milestone2, position: 2) }
let!(:assignee_list) { create(:user_list, board: board, user: assignee, position: 5) } let!(:assignee_list) { create(:user_list, board: board, user: assignee, position: 5) }
let!(:assignee_list2) { create(:user_list, board: board, user: assignee2, position: 1) } let!(:assignee_list2) { create(:user_list, board: board, user: assignee2, position: 1) }
let(:closed_list) { board.lists.find_by(list_type: :closed) } let(:closed_list) { board.lists.find_by(list_type: :closed) }
let(:lists) { [closed_list, assignee_list2, assignee_list, milestone_list2, milestone_list] }
before do
post_graphql(query, current_user: user)
end
it_behaves_like 'a working graphql query'
context 'when ascending' do context 'when ascending' do
let(:lists) { [closed_list, assignee_list2, assignee_list, milestone_list2, milestone_list] } it_behaves_like 'sorted paginated query' do
let(:expected_list_gids) do let(:sort_param) { }
lists.map { |list| list.to_global_id.to_s } let(:first_param) { 2 }
end let(:expected_results) { lists.map { |list| list.to_global_id.to_s } }
it 'sorts lists' do
expect(grab_ids).to eq expected_list_gids
end
context 'when paginating' do
let(:params) { 'first: 2' }
it 'sorts boards' do
expect(grab_ids).to eq expected_list_gids.first(2)
cursored_query = query("after: \"#{end_cursor}\"")
post_graphql(cursored_query, current_user: user)
response_data = grab_list_data(response.body)
expect(grab_ids(response_data)).to eq expected_list_gids.drop(2).first(3)
end
end end
end end
end end
...@@ -108,7 +107,7 @@ RSpec.describe 'get board lists' do ...@@ -108,7 +107,7 @@ RSpec.describe 'get board lists' do
let!(:list_with_limit_metrics) { create(:list, board: board, **limit_metric_params) } let!(:list_with_limit_metrics) { create(:list, board: board, **limit_metric_params) }
before do before do
post_graphql(query, current_user: user) post_graphql(query, current_user: current_user)
end end
it 'returns the expected limit metric settings' do it 'returns the expected limit metric settings' do
...@@ -144,11 +143,8 @@ RSpec.describe 'get board lists' do ...@@ -144,11 +143,8 @@ RSpec.describe 'get board lists' do
it_behaves_like 'group and project board lists query' it_behaves_like 'group and project board lists query'
end end
def grab_ids(data = lists_data)
data.map { |list| list.dig('node', 'id') }
end
def grab_list_data(response_body) def grab_list_data(response_body)
Gitlab::Json.parse(response_body)['data'][board_parent_type]['boards']['edges'][0]['node']['lists']['edges'] keys = [:data, board_parent_type, :boards, :edges, 0, :node, :lists, :edges]
graphql_dig_at(Gitlab::Json.parse(response_body), *keys)
end end
end end
...@@ -15,7 +15,7 @@ RSpec.describe 'getting an issue list for a project' do ...@@ -15,7 +15,7 @@ RSpec.describe 'getting an issue list for a project' do
graphql_query_for( graphql_query_for(
'project', 'project',
{ 'fullPath' => sort_project.full_path }, { 'fullPath' => sort_project.full_path },
"issues(#{params}) { #{page_info} edges { node { iid weight } } }" query_graphql_field('issues', params, "#{page_info} edges { node { iid weight} }")
) )
end end
......
...@@ -65,41 +65,41 @@ describe 'get board lists' do ...@@ -65,41 +65,41 @@ describe 'get board lists' do
end end
describe 'sorting and pagination' do describe 'sorting and pagination' do
let_it_be(:current_user) { user }
let(:data_path) { [board_parent_type, :boards, :edges, 0, :node, :lists] }
def pagination_query(params, page_info)
graphql_query_for(
board_parent_type,
{ 'fullPath' => board_parent.full_path },
<<~BOARDS
boards(first: 1) {
edges {
node {
#{query_graphql_field('lists', params, "#{page_info} edges { node { id } }")}
}
}
}
BOARDS
)
end
def pagination_results_data(data)
data.map { |list| list.dig('node', 'id') }
end
context 'when using default sorting' do context 'when using default sorting' do
let!(:label_list) { create(:list, board: board, label: label, position: 10) } let!(:label_list) { create(:list, board: board, label: label, position: 10) }
let!(:label_list2) { create(:list, board: board, label: label2, position: 2) } let!(:label_list2) { create(:list, board: board, label: label2, position: 2) }
let!(:backlog_list) { create(:backlog_list, board: board) } let!(:backlog_list) { create(:backlog_list, board: board) }
let(:closed_list) { board.lists.find_by(list_type: :closed) } let(:closed_list) { board.lists.find_by(list_type: :closed) }
let(:lists) { [backlog_list, label_list2, label_list, closed_list] }
before do
post_graphql(query, current_user: user)
end
it_behaves_like 'a working graphql query'
context 'when ascending' do context 'when ascending' do
let(:lists) { [backlog_list, label_list2, label_list, closed_list] } it_behaves_like 'sorted paginated query' do
let(:expected_list_gids) do let(:sort_param) { }
lists.map { |list| list.to_global_id.to_s } let(:first_param) { 2 }
end let(:expected_results) { lists.map { |list| list.to_global_id.to_s } }
it 'sorts lists' do
expect(grab_ids).to eq expected_list_gids
end
context 'when paginating' do
let(:params) { 'first: 2' }
it 'sorts boards' do
expect(grab_ids).to eq expected_list_gids.first(2)
cursored_query = query("after: \"#{end_cursor}\"")
post_graphql(cursored_query, current_user: user)
response_data = grab_list_data(response.body)
expect(grab_ids(response_data)).to eq expected_list_gids.drop(2).first(2)
end
end end
end end
end end
...@@ -126,12 +126,4 @@ describe 'get board lists' do ...@@ -126,12 +126,4 @@ describe 'get board lists' do
it_behaves_like 'group and project board lists query' it_behaves_like 'group and project board lists query'
end end
def grab_ids(data = lists_data)
data.map { |list| list.dig('node', 'id') }
end
def grab_list_data(response_body)
Gitlab::Json.parse(response_body)['data'][board_parent_type]['boards']['edges'][0]['node']['lists']['edges']
end
end end
...@@ -124,7 +124,7 @@ describe 'getting an issue list for a project' do ...@@ -124,7 +124,7 @@ describe 'getting an issue list for a project' do
graphql_query_for( graphql_query_for(
'project', 'project',
{ 'fullPath' => sort_project.full_path }, { 'fullPath' => sort_project.full_path },
"issues(#{params}) { #{page_info} edges { node { iid dueDate } } }" query_graphql_field('issues', params, "#{page_info} edges { node { iid dueDate} }")
) )
end end
......
...@@ -279,8 +279,13 @@ module GraphqlHelpers ...@@ -279,8 +279,13 @@ module GraphqlHelpers
end end
def graphql_dig_at(data, *path) def graphql_dig_at(data, *path)
keys = path.map { |segment| GraphqlHelpers.fieldnamerize(segment) } keys = path.map { |segment| segment.is_a?(Integer) ? segment : GraphqlHelpers.fieldnamerize(segment) }
data.dig(*keys)
# Allows for array indexing, like this
# ['project', 'boards', 'edges', 0, 'node', 'lists']
keys.reduce(data) do |memo, key|
memo.is_a?(Array) ? memo[key] : memo&.dig(key)
end
end end
def graphql_errors def graphql_errors
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
# graphql_query_for( # graphql_query_for(
# 'project', # 'project',
# { 'fullPath' => sort_project.full_path }, # { 'fullPath' => sort_project.full_path },
# "issues(#{params}) { #{page_info} edges { node { iid weight } } }" # query_graphql_field('issues', params, "#{page_info} edges { node { id } }")
# ) # )
# end # end
# #
...@@ -47,11 +47,13 @@ RSpec.shared_examples 'sorted paginated query' do ...@@ -47,11 +47,13 @@ RSpec.shared_examples 'sorted paginated query' do
end end
describe do describe do
let(:params) { "sort: #{sort_param}" } let(:sort_argument) { "sort: #{sort_param}" if sort_param.present? }
let(:start_cursor) { graphql_data_at(*data_path, :pageInfo, :startCursor) } let(:first_argument) { "first: #{first_param}" if first_param.present? }
let(:end_cursor) { graphql_data_at(*data_path, :pageInfo, :endCursor) } let(:params) { sort_argument }
let(:sorted_edges) { graphql_data_at(*data_path, :edges) } let(:start_cursor) { graphql_data_at(*data_path, :pageInfo, :startCursor) }
let(:page_info) { "pageInfo { startCursor endCursor }" } let(:end_cursor) { graphql_data_at(*data_path, :pageInfo, :endCursor) }
let(:sorted_edges) { graphql_data_at(*data_path, :edges) }
let(:page_info) { "pageInfo { startCursor endCursor }" }
def pagination_query(params, page_info) def pagination_query(params, page_info)
raise('pagination_query(params, page_info) must be defined in the test, see example in comment') unless defined?(super) raise('pagination_query(params, page_info) must be defined in the test, see example in comment') unless defined?(super)
...@@ -75,12 +77,12 @@ RSpec.shared_examples 'sorted paginated query' do ...@@ -75,12 +77,12 @@ RSpec.shared_examples 'sorted paginated query' do
end end
context 'when paginating' do context 'when paginating' do
let(:params) { "sort: #{sort_param}, first: #{first_param}" } let(:params) { [sort_argument, first_argument].compact.join(',') }
it 'paginates correctly' do it 'paginates correctly' do
expect(pagination_results_data(sorted_edges)).to eq expected_results.first(first_param) expect(pagination_results_data(sorted_edges)).to eq expected_results.first(first_param)
cursored_query = pagination_query("sort: #{sort_param}, after: \"#{end_cursor}\"", page_info) cursored_query = pagination_query([sort_argument, "after: \"#{end_cursor}\""].compact.join(','), page_info)
post_graphql(cursored_query, current_user: current_user) post_graphql(cursored_query, current_user: current_user)
response_data = graphql_dig_at(Gitlab::Json.parse(response.body), :data, *data_path, :edges) response_data = graphql_dig_at(Gitlab::Json.parse(response.body), :data, *data_path, :edges)
......
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