1# frozen_string_literal: true
2
3# Use this for testing how a GraphQL query handles sorting and pagination.
4# This is particularly important when using keyset pagination connection,
5# which is the default for ActiveRecord relations, as certain sort keys
6# might not be supportable.
7#
8# sort_param: the value to specify the sort
9# data_path: the keys necessary to dig into the return GraphQL data to get the
10#   returned results
11# first_param: number of items expected (like a page size)
12# all_records: array of comparison data of all items sorted correctly
13# pagination_query: method that specifies the GraphQL query
14# pagination_results_data: method that extracts the sorted data used to compare against
15#   the expected results
16#
17# Example:
18#   describe 'sorting and pagination' do
19#     let_it_be(:sort_project) { create(:project, :public) }
20#     let(:data_path)    { [:project, :issues] }
21#
22#     def pagination_query(arguments)
23#       graphql_query_for(:project, { full_path: sort_project.full_path },
24#         query_nodes(:issues, :iid, include_pagination_info: true, args: arguments)
25#       )
26#     end
27#
28#     # A method transforming nodes to data to match against
29#     # default: the identity function
30#     def pagination_results_data(issues)
31#       issues.map { |issue| issue['iid].to_i }
32#     end
33#
34#     context 'when sorting by weight' do
35#       let_it_be(:issues) { make_some_issues_with_weights }
36#
37#       context 'when ascending' do
38#         let(:ordered_issues) { issues.sort_by(&:weight) }
39#
40#         it_behaves_like 'sorted paginated query' do
41#           let(:sort_param) { :WEIGHT_ASC }
42#           let(:first_param) { 2 }
43#           let(:all_records) { ordered_issues.map(&:iid) }
44#         end
45#       end
46#
47RSpec.shared_examples 'sorted paginated query' do |conditions = {}|
48  # Provided as a convenience when constructing queries using string concatenation
49  let(:page_info) { 'pageInfo { startCursor endCursor }' }
50  # Convenience for using default implementation of pagination_results_data
51  let(:node_path) { ['id'] }
52
53  it_behaves_like 'requires variables' do
54    let(:required_variables) { [:sort_param, :first_param, :all_records, :data_path, :current_user] }
55  end
56
57  describe do
58    let(:sort_argument)  { graphql_args(sort: sort_param) }
59    let(:params)         { sort_argument }
60
61    # Convenience helper for the large number of queries defined as a projection
62    # from some root value indexed by full_path to a collection of objects with IID
63    def nested_internal_id_query(root_field, parent, field, args, selection: :iid)
64      graphql_query_for(root_field, { full_path: parent.full_path },
65        query_nodes(field, selection, args: args, include_pagination_info: true)
66      )
67    end
68
69    def pagination_query(params)
70      raise('pagination_query(params) must be defined in the test, see example in comment') unless defined?(super)
71
72      super
73    end
74
75    def pagination_results_data(nodes)
76      if defined?(super)
77        super(nodes)
78      else
79        nodes.map { |n| n.dig(*node_path) }
80      end
81    end
82
83    def results
84      nodes = graphql_dig_at(graphql_data(fresh_response_data), *data_path, :nodes)
85      pagination_results_data(nodes)
86    end
87
88    def end_cursor
89      graphql_dig_at(graphql_data(fresh_response_data), *data_path, :page_info, :end_cursor)
90    end
91
92    def start_cursor
93      graphql_dig_at(graphql_data(fresh_response_data), *data_path, :page_info, :start_cursor)
94    end
95
96    let(:query) { pagination_query(params) }
97
98    before do
99      post_graphql(query, current_user: current_user)
100    end
101
102    context 'when sorting' do
103      it 'sorts correctly' do
104        expect(results).to eq all_records
105      end
106
107      context 'when paginating' do
108        let(:params) { sort_argument.merge(first: first_param) }
109        let(:first_page) { all_records.first(first_param) }
110        let(:rest) { all_records.drop(first_param) }
111
112        it 'paginates correctly' do
113          expect(results).to eq first_page
114
115          fwds = pagination_query(sort_argument.merge(after: end_cursor))
116          post_graphql(fwds, current_user: current_user)
117
118          expect(results).to eq rest
119
120          bwds = pagination_query(sort_argument.merge(before: start_cursor))
121          post_graphql(bwds, current_user: current_user)
122
123          expect(results).to eq first_page
124        end
125      end
126
127      context 'when last and sort params are present', if: conditions[:is_reversible] do
128        let(:params) { sort_argument.merge(last: 1) }
129
130        it 'fetches last elements without error' do
131          post_graphql(pagination_query(params), current_user: current_user)
132
133          expect(results.first).to eq(all_records.last)
134        end
135      end
136    end
137  end
138end
139