File: page_info_spec.rb

package info (click to toggle)
ruby-graphql 2.2.17-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 9,584 kB
  • sloc: ruby: 67,505; ansic: 1,753; yacc: 831; javascript: 331; makefile: 6
file content (120 lines) | stat: -rw-r--r-- 5,126 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true
require "spec_helper"

describe "GraphQL::Relay::PageInfo" do
  def get_page_info(result)
    result["data"]["empire"]["bases"]["pageInfo"]
  end

  def get_first_cursor(result)
    result["data"]["empire"]["bases"]["edges"].first["cursor"]
  end

  def get_last_cursor(result)
    result["data"]["empire"]["bases"]["edges"].last["cursor"]
  end

  let(:cursor_of_last_base) {
    result = star_wars_query(query_string, { "first" => 100 })
    get_last_cursor(result)
  }

  let(:query_string) {%|
    query getShips($first: Int, $after: String, $last: Int, $before: String, $nameIncludes: String){
      empire {
        bases(first: $first, after: $after, last: $last, before: $before, nameIncludes: $nameIncludes) {
          pageInfo {
            hasNextPage
            hasPreviousPage
            startCursor
            endCursor
          },
          edges {
            cursor
          }
        }
      }
    }
  |}

  describe 'hasNextPage / hasPreviousPage' do
    it "hasNextPage is true if there are more items" do
      result = star_wars_query(query_string, { "first" => 2 })
      assert_equal(true, get_page_info(result)["hasNextPage"])
      assert_equal(false, get_page_info(result)["hasPreviousPage"], "hasPreviousPage is false if 'last' is missing")
      assert_equal("MQ", get_page_info(result)["startCursor"])
      assert_equal("Mg", get_page_info(result)["endCursor"])

      last_cursor = get_last_cursor(result)
      result = star_wars_query(query_string, { "first" => 100, "after" => last_cursor })
      assert_equal(false, get_page_info(result)["hasNextPage"])
      assert_equal(true, get_page_info(result)["hasPreviousPage"])
      assert_equal("Mw", get_page_info(result)["startCursor"])
      assert_equal("Mw", get_page_info(result)["endCursor"])
    end

    it "hasPreviousPage if there are more items" do
      result = star_wars_query(query_string, { "last" => 100, "before" => cursor_of_last_base })
      assert_equal(true, get_page_info(result)["hasNextPage"])
      assert_equal(false, get_page_info(result)["hasPreviousPage"])
      assert_equal("MQ", get_page_info(result)["startCursor"])
      assert_equal("Mg", get_page_info(result)["endCursor"])

      result = star_wars_query(query_string, { "last" => 1, "before" => cursor_of_last_base })
      assert_equal(true, get_page_info(result)["hasNextPage"])
      assert_equal(true, get_page_info(result)["hasPreviousPage"])
      assert_equal("Mg", get_page_info(result)["startCursor"])
      assert_equal("Mg", get_page_info(result)["endCursor"])
    end

    it "has both if first and last are present" do
      result = star_wars_query(query_string, { "last" => 1, "first" => 1, "before" => cursor_of_last_base })
      assert_equal(true, get_page_info(result)["hasNextPage"])
      # I think this was actually a bug in the previous implementation.
      # This query returns the first node in the list:
      #     Base64.decode64("MQ") # => "1"
      # So, there is _not_ a previous page.
      assert_equal(false, get_page_info(result)["hasPreviousPage"])
      assert_equal("MQ", get_page_info(result)["startCursor"])
      assert_equal("MQ", get_page_info(result)["endCursor"])
    end

    it "startCursor and endCursor are the cursors of the first and last edge" do
      result = star_wars_query(query_string, { "first" => 2 })
      assert_equal(true, get_page_info(result)["hasNextPage"])
      assert_equal(false, get_page_info(result)["hasPreviousPage"])
      assert_equal("MQ", get_page_info(result)["startCursor"])
      assert_equal("Mg", get_page_info(result)["endCursor"])
      assert_equal("MQ", get_first_cursor(result))
      assert_equal("Mg", get_last_cursor(result))

      result = star_wars_query(query_string, { "first" => 1, "after" => get_page_info(result)["endCursor"] })
      assert_equal(false, get_page_info(result)["hasNextPage"])
      assert_equal(true, get_page_info(result)["hasPreviousPage"])
      assert_equal("Mw", get_page_info(result)["startCursor"])
      assert_equal("Mw", get_page_info(result)["endCursor"])
      assert_equal("Mw", get_first_cursor(result))
      assert_equal("Mw", get_last_cursor(result))

      result = star_wars_query(query_string, { "last" => 1, "before" => get_page_info(result)["endCursor"] })
      assert_equal(true, get_page_info(result)["hasNextPage"])
      assert_equal(true, get_page_info(result)["hasPreviousPage"])
      assert_equal("Mg", get_page_info(result)["startCursor"])
      assert_equal("Mg", get_page_info(result)["endCursor"])
      assert_equal("Mg", get_first_cursor(result))
      assert_equal("Mg", get_last_cursor(result))
    end
  end

  it "can be redefined" do
    conn_type = Class.new(GraphQL::Schema::Object) do
      include GraphQL::Types::Relay::ConnectionBehaviors

      get_field("pageInfo").type = GraphQL::Types::Int.to_non_null_type
    end

    assert_equal "Int!", conn_type.fields["pageInfo"].type.to_type_signature
    # The original is unchanged:
    assert_equal "PageInfo!", GraphQL::Types::Relay::BaseConnection.fields["pageInfo"].type.to_type_signature
  end
end