|
1 | 1 | import { Cursor } from '../cursor/Cursor';
|
2 | 2 | import { Foo, FooConnection, FooConnectionBuilder, FooEdge } from '../../test/FooConnection';
|
3 | 3 | import { BarConnectionBuilder, FruitBar, NutBar } from '../../test/BarConnection';
|
| 4 | +import { ConnectionArgsValidationError } from '../error'; |
4 | 5 |
|
5 | 6 | describe('ConnectionBuilder', () => {
|
6 | 7 | test('First page is built correctly', () => {
|
@@ -283,4 +284,115 @@ describe('ConnectionBuilder', () => {
|
283 | 284 | ],
|
284 | 285 | });
|
285 | 286 | });
|
| 287 | + |
| 288 | + test('Can fetch zero first edges', () => { |
| 289 | + const builder = new FooConnectionBuilder({ |
| 290 | + first: 0, |
| 291 | + }); |
| 292 | + |
| 293 | + expect(builder.edgesPerPage).toBe(0); |
| 294 | + expect(builder.afterCursor).toBeUndefined(); |
| 295 | + expect(builder.beforeCursor).toBeUndefined(); |
| 296 | + |
| 297 | + const connection = builder.build({ |
| 298 | + totalEdges: 12, |
| 299 | + nodes: [], |
| 300 | + }); |
| 301 | + |
| 302 | + expect(connection).toMatchObject({ |
| 303 | + pageInfo: { |
| 304 | + totalEdges: 12, |
| 305 | + hasNextPage: true, |
| 306 | + hasPreviousPage: false, |
| 307 | + }, |
| 308 | + edges: [], |
| 309 | + }); |
| 310 | + }); |
| 311 | + |
| 312 | + test('Can fetch zero last edges', () => { |
| 313 | + const builder = new FooConnectionBuilder({ |
| 314 | + last: 0, |
| 315 | + }); |
| 316 | + |
| 317 | + expect(builder.edgesPerPage).toBe(0); |
| 318 | + expect(builder.afterCursor).toBeUndefined(); |
| 319 | + expect(builder.beforeCursor).toBeUndefined(); |
| 320 | + |
| 321 | + const connection = builder.build({ |
| 322 | + totalEdges: 12, |
| 323 | + nodes: [], |
| 324 | + }); |
| 325 | + |
| 326 | + expect(connection).toMatchObject({ |
| 327 | + pageInfo: { |
| 328 | + totalEdges: 12, |
| 329 | + hasNextPage: true, |
| 330 | + hasPreviousPage: false, |
| 331 | + }, |
| 332 | + edges: [], |
| 333 | + }); |
| 334 | + }); |
| 335 | + |
| 336 | + describe('Connection arguments', () => { |
| 337 | + test('Should throw an error if the connection does not support offset pagination', () => { |
| 338 | + const createBuilder = () => |
| 339 | + new FooConnectionBuilder({ |
| 340 | + page: 1, |
| 341 | + }); |
| 342 | + |
| 343 | + expect(createBuilder).toThrow( |
| 344 | + new ConnectionArgsValidationError('This connection does not support the "page" argument for pagination.'), |
| 345 | + ); |
| 346 | + }); |
| 347 | + |
| 348 | + test('Should throw an error if first is less than zero', () => { |
| 349 | + const createBuilder = () => |
| 350 | + new FooConnectionBuilder({ |
| 351 | + first: -1, |
| 352 | + }); |
| 353 | + |
| 354 | + expect(createBuilder).toThrow( |
| 355 | + new ConnectionArgsValidationError(`The "first" argument accepts a value between 0 and 100, inclusive.`), |
| 356 | + ); |
| 357 | + }); |
| 358 | + |
| 359 | + test('Should throw an error if both "first" and "last" arguments are supplied', () => { |
| 360 | + const createBuilder = () => |
| 361 | + new FooConnectionBuilder({ |
| 362 | + first: 5, |
| 363 | + last: 5, |
| 364 | + }); |
| 365 | + |
| 366 | + expect(createBuilder).toThrow( |
| 367 | + new ConnectionArgsValidationError( |
| 368 | + 'It is not permitted to specify both "first" and "last" arguments simultaneously.', |
| 369 | + ), |
| 370 | + ); |
| 371 | + }); |
| 372 | + |
| 373 | + test('Should throw an error if last is less than zero', () => { |
| 374 | + const createBuilder = () => |
| 375 | + new FooConnectionBuilder({ |
| 376 | + last: -1, |
| 377 | + }); |
| 378 | + |
| 379 | + expect(createBuilder).toThrow( |
| 380 | + new ConnectionArgsValidationError(`The "last" argument accepts a value between 0 and 100, inclusive.`), |
| 381 | + ); |
| 382 | + }); |
| 383 | + |
| 384 | + test('Should throw an error if both "after" and "before" arguments are supplied', () => { |
| 385 | + const createBuilder = () => |
| 386 | + new FooConnectionBuilder({ |
| 387 | + after: '...', |
| 388 | + before: '...', |
| 389 | + }); |
| 390 | + |
| 391 | + expect(createBuilder).toThrow( |
| 392 | + new ConnectionArgsValidationError( |
| 393 | + 'It is not permitted to specify both "after" and "before" arguments simultaneously.', |
| 394 | + ), |
| 395 | + ); |
| 396 | + }); |
| 397 | + }); |
286 | 398 | });
|
0 commit comments