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'
RSpec.describe 'get board lists' do
include GraphqlHelpers
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, creator_id: user.id, namespace: user.namespace ) }
let_it_be(:current_user) { create(:user) }
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(:project_milestone) { create(:milestone, project: project) }
let_it_be(:project_milestone2) { create(:milestone, project: project) }
......@@ -58,46 +58,45 @@ RSpec.describe 'get board lists' do
context 'when user can read the board' do
before do
board_parent.add_reporter(user)
board_parent.add_reporter(current_user)
end
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
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!(:assignee_list) { create(:user_list, board: board, user: assignee, position: 5) }
let!(:assignee_list2) { create(:user_list, board: board, user: assignee2, position: 1) }
let(:closed_list) { board.lists.find_by(list_type: :closed) }
before do
post_graphql(query, current_user: user)
end
it_behaves_like 'a working graphql query'
let(:lists) { [closed_list, assignee_list2, assignee_list, milestone_list2, milestone_list] }
context 'when ascending' do
let(:lists) { [closed_list, assignee_list2, assignee_list, milestone_list2, milestone_list] }
let(:expected_list_gids) do
lists.map { |list| list.to_global_id.to_s }
end
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
it_behaves_like 'sorted paginated query' do
let(:sort_param) { }
let(:first_param) { 2 }
let(:expected_results) { lists.map { |list| list.to_global_id.to_s } }
end
end
end
......@@ -108,7 +107,7 @@ RSpec.describe 'get board lists' do
let!(:list_with_limit_metrics) { create(:list, board: board, **limit_metric_params) }
before do
post_graphql(query, current_user: user)
post_graphql(query, current_user: current_user)
end
it 'returns the expected limit metric settings' do
......@@ -144,11 +143,8 @@ RSpec.describe 'get board lists' do
it_behaves_like 'group and project board lists query'
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']
keys = [:data, board_parent_type, :boards, :edges, 0, :node, :lists, :edges]
graphql_dig_at(Gitlab::Json.parse(response_body), *keys)
end
end
......@@ -15,7 +15,7 @@ RSpec.describe 'getting an issue list for a project' do
graphql_query_for(
'project',
{ '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
......
......@@ -65,41 +65,41 @@ describe 'get board lists' do
end
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
let!(:label_list) { create(:list, board: board, label: label, position: 10) }
let!(:label_list2) { create(:list, board: board, label: label2, position: 2) }
let!(:backlog_list) { create(:backlog_list, board: board) }
let(:closed_list) { board.lists.find_by(list_type: :closed) }
before do
post_graphql(query, current_user: user)
end
it_behaves_like 'a working graphql query'
let(:lists) { [backlog_list, label_list2, label_list, closed_list] }
context 'when ascending' do
let(:lists) { [backlog_list, label_list2, label_list, closed_list] }
let(:expected_list_gids) do
lists.map { |list| list.to_global_id.to_s }
end
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
it_behaves_like 'sorted paginated query' do
let(:sort_param) { }
let(:first_param) { 2 }
let(:expected_results) { lists.map { |list| list.to_global_id.to_s } }
end
end
end
......@@ -126,12 +126,4 @@ describe 'get board lists' do
it_behaves_like 'group and project board lists query'
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
......@@ -124,7 +124,7 @@ describe 'getting an issue list for a project' do
graphql_query_for(
'project',
{ '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
......
......@@ -279,8 +279,13 @@ module GraphqlHelpers
end
def graphql_dig_at(data, *path)
keys = path.map { |segment| GraphqlHelpers.fieldnamerize(segment) }
data.dig(*keys)
keys = path.map { |segment| segment.is_a?(Integer) ? segment : GraphqlHelpers.fieldnamerize(segment) }
# 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
def graphql_errors
......
......@@ -23,7 +23,7 @@
# graphql_query_for(
# 'project',
# { 'fullPath' => sort_project.full_path },
# "issues(#{params}) { #{page_info} edges { node { iid weight } } }"
# query_graphql_field('issues', params, "#{page_info} edges { node { id } }")
# )
# end
#
......@@ -47,11 +47,13 @@ RSpec.shared_examples 'sorted paginated query' do
end
describe do
let(:params) { "sort: #{sort_param}" }
let(:start_cursor) { graphql_data_at(*data_path, :pageInfo, :startCursor) }
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 }" }
let(:sort_argument) { "sort: #{sort_param}" if sort_param.present? }
let(:first_argument) { "first: #{first_param}" if first_param.present? }
let(:params) { sort_argument }
let(:start_cursor) { graphql_data_at(*data_path, :pageInfo, :startCursor) }
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)
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
end
context 'when paginating' do
let(:params) { "sort: #{sort_param}, first: #{first_param}" }
let(:params) { [sort_argument, first_argument].compact.join(',') }
it 'paginates correctly' do
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)
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