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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
# frozen_string_literal: true
require 'spec_helper'
describe "GraphQL::Relay::ArrayConnection" do
def get_names(result)
ships = result["data"]["rebels"]["ships"]["edges"]
ships.map { |e| e["node"]["name"] }
end
def get_last_cursor(result)
result["data"]["rebels"]["ships"]["edges"].last["cursor"]
end
def get_page_info(result, key = "bases")
result["data"]["rebels"][key]["pageInfo"]
end
describe "results" do
let(:query_string) {%|
query getShips($first: Int, $after: String, $last: Int, $before: String, $nameIncludes: String){
rebels {
ships(first: $first, after: $after, last: $last, before: $before, nameIncludes: $nameIncludes) {
edges {
cursor
node {
name
}
}
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
}
}
}
|}
it 'limits the result' do
result = star_wars_query(query_string, { "first" => 2 })
number_of_ships = get_names(result).length
assert_equal(2, number_of_ships)
assert_equal(true, result["data"]["rebels"]["ships"]["pageInfo"]["hasNextPage"])
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasPreviousPage"])
assert_equal("MQ", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
assert_equal("Mg", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
result = star_wars_query(query_string, { "first" => 3 })
number_of_ships = get_names(result).length
assert_equal(3, number_of_ships)
end
it 'provides pageInfo' do
result = star_wars_query(query_string, { "first" => 2 })
assert_equal(true, result["data"]["rebels"]["ships"]["pageInfo"]["hasNextPage"])
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasPreviousPage"])
assert_equal("MQ", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
assert_equal("Mg", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
result = star_wars_query(query_string, { "first" => 100 })
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasNextPage"])
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasPreviousPage"])
assert_equal("MQ", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
assert_equal("NQ", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
end
it 'slices the result' do
result = star_wars_query(query_string, { "first" => 1 })
assert_equal(["X-Wing"], get_names(result))
# After the last result, find the next 2:
last_cursor = get_last_cursor(result)
result = star_wars_query(query_string, { "after" => last_cursor, "first" => 2 })
assert_equal(["Y-Wing", "A-Wing"], get_names(result))
# After the last result, find the next 2:
last_cursor = get_last_cursor(result)
result = star_wars_query(query_string, { "after" => last_cursor, "first" => 2 })
assert_equal(["Millenium Falcon", "Home One"], get_names(result))
result = star_wars_query(query_string, { "before" => last_cursor, "last" => 2 })
assert_equal(["X-Wing", "Y-Wing"], get_names(result))
result = star_wars_query(query_string, { "last" => 2 })
assert_equal(["Millenium Falcon", "Home One"], get_names(result))
result = star_wars_query(query_string, { "last" => 10 })
assert_equal(["X-Wing", "Y-Wing", "A-Wing", "Millenium Falcon", "Home One"], get_names(result))
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasNextPage"])
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasPreviousPage"])
end
it 'works with before and after specified together' do
result = star_wars_query(query_string, { "first" => 1 })
assert_equal(["X-Wing"], get_names(result))
first_cursor = get_last_cursor(result)
# There is no records between before and after if they point to the same cursor
result = star_wars_query(query_string, { "before" => first_cursor, "after" => first_cursor, "last" => 2 })
assert_equal([], get_names(result))
result = star_wars_query(query_string, { "after" => first_cursor, "first" => 2 })
assert_equal(["Y-Wing", "A-Wing"], get_names(result))
third_cursor = get_last_cursor(result)
# There is only 1 result between the cursors
result = star_wars_query(query_string, { "after" => first_cursor, "before" => third_cursor, "first" => 5 })
assert_equal(["Y-Wing"], get_names(result))
end
it 'handles cursors beyond the bounds of the array' do
overreaching_cursor = Base64.strict_encode64("100")
result = star_wars_query(query_string, { "after" => overreaching_cursor, "first" => 2 })
assert_equal([], get_names(result))
end
it 'applies custom arguments' do
result = star_wars_query(query_string, { "nameIncludes" => "Wing", "first" => 2 })
names = get_names(result)
assert_equal(2, names.length)
after = get_last_cursor(result)
result = star_wars_query(query_string, { "nameIncludes" => "Wing", "after" => after, "first" => 3 })
names = get_names(result)
assert_equal(1, names.length)
end
it 'works without first/last/after/before' do
result = star_wars_query(query_string)
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasNextPage"])
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasPreviousPage"])
assert_equal("MQ", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
assert_equal("NQ", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
assert_equal(5, result["data"]["rebels"]["ships"]["edges"].length)
end
describe "applying max_page_size" do
def get_names(result)
result["data"]["rebels"]["bases"]["edges"].map { |e| e["node"]["name"] }
end
let(:query_string) {%|
query getShips($first: Int, $after: String, $last: Int, $before: String){
rebels {
bases: basesWithMaxLimitArray(first: $first, after: $after, last: $last, before: $before) {
edges {
cursor
node {
name
}
}
pageInfo {
hasNextPage
hasPreviousPage
}
}
}
}
|}
it "applies to queries by `first`" do
result = star_wars_query(query_string, { "first" => 100 })
assert_equal(["Yavin", "Echo Base"], get_names(result))
assert_equal(true, get_page_info(result)["hasNextPage"])
# Max page size is applied _without_ `first`, also
result = star_wars_query(query_string)
assert_equal(["Yavin", "Echo Base"], get_names(result))
assert_equal(true, get_page_info(result)["hasNextPage"], "hasNextPage is false when first is not specified")
end
it "applies to queries by `last`" do
last_cursor = "Ng=="
result = star_wars_query(query_string, { "last" => 100, "before" => last_cursor })
assert_equal(["Death Star", "Shield Generator"], get_names(result))
assert_equal(true, get_page_info(result)["hasPreviousPage"])
result = star_wars_query(query_string, { "before" => last_cursor })
assert_equal(["Yavin", "Echo Base"], get_names(result))
assert_equal(false, get_page_info(result)["hasPreviousPage"], "hasPreviousPage is false when last is not specified")
third_cursor = "Mw"
first_and_second_names = ["Yavin", "Echo Base"]
result = star_wars_query(query_string, { "last" => 100, "before" => third_cursor })
assert_equal(first_and_second_names, get_names(result))
result = star_wars_query(query_string, { "before" => third_cursor })
assert_equal(first_and_second_names, get_names(result))
end
end
describe "applying default_max_page_size" do
def get_names(result)
result["data"]["rebels"]["bases"]["edges"].map { |e| e["node"]["name"] }
end
def get_page_info(result)
result["data"]["rebels"]["bases"]["pageInfo"]
end
let(:query_string) {%|
query getShips($first: Int, $after: String, $last: Int, $before: String){
rebels {
bases: basesWithDefaultMaxLimitArray(first: $first, after: $after, last: $last, before: $before) {
edges {
cursor
node {
name
}
}
pageInfo {
hasNextPage
hasPreviousPage
}
}
}
}
|}
it "applies to queries by `first`" do
result = star_wars_query(query_string, { "first" => 100 })
assert_equal(["Yavin", "Echo Base", "Secret Hideout"], get_names(result))
assert_equal(true, get_page_info(result)["hasNextPage"])
# Max page size is applied _without_ `first`, also
result = star_wars_query(query_string)
assert_equal(["Yavin", "Echo Base", "Secret Hideout"], get_names(result))
assert_equal(true, get_page_info(result)["hasNextPage"], "hasNextPage is false when first is not specified")
end
it "applies to queries by `last`" do
last_cursor = "Ng=="
result = star_wars_query(query_string, { "last" => 100, "before" => last_cursor })
assert_equal(["Secret Hideout", "Death Star", "Shield Generator"], get_names(result))
assert_equal(true, get_page_info(result)["hasPreviousPage"])
result = star_wars_query(query_string, { "before" => last_cursor })
assert_equal(["Yavin", "Echo Base", "Secret Hideout"], get_names(result))
assert_equal(false, get_page_info(result)["hasPreviousPage"], "hasPreviousPage is false when last is not specified")
fourth_cursor = "NA=="
first_second_and_third_names = ["Yavin", "Echo Base", "Secret Hideout"]
result = star_wars_query(query_string, { "last" => 100, "before" => fourth_cursor })
assert_equal(first_second_and_third_names, get_names(result))
result = star_wars_query(query_string, { "before" => fourth_cursor })
assert_equal(first_second_and_third_names, get_names(result))
end
end
describe "bidirectional pagination" do
it "provides bidirectional_pagination by default" do
result = star_wars_query(query_string, { "first" => 1 })
last_cursor = get_last_cursor(result)
result = star_wars_query(query_string, { "first" => 3, "after" => last_cursor })
assert_equal(true, get_page_info(result, "ships")["hasNextPage"])
assert_equal(true, get_page_info(result, "ships")["hasPreviousPage"])
# When going backwards, bi-directional pagination
# returns true for `hasNextPage`
last_cursor = get_last_cursor(result)
result = star_wars_query(query_string, { "last" => 2, "before" => last_cursor })
assert_equal(true, get_page_info(result, "ships")["hasNextPage"])
assert_equal(true, get_page_info(result, "ships")["hasPreviousPage"])
end
it "returns correct page info when the before cursor belongs to the last element in the array" do
result = star_wars_query(query_string, { "last" => 1 })
last_cursor = get_last_cursor(result)
result = star_wars_query(query_string, { "before" => last_cursor, "last" => 1 })
assert_equal(true, get_page_info(result, "ships")["hasNextPage"])
assert_equal(true, get_page_info(result, "ships")["hasPreviousPage"])
end
end
end
end
|