Skip to content

Commit 814dc28

Browse files
authored
fix: pageInfo.hasPreviousPage and pageInfo.hasNextPage (#55)
1 parent 4f351bb commit 814dc28

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

src/__tests__/connectionResolver-test.js

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -347,41 +347,87 @@ describe('connectionResolver', () => {
347347

348348
describe('"Relay Cursor Connections Specification (PageInfo)":', () => {
349349
describe('HasPreviousPage', () => {
350-
it('If last was not set (but first is present), return false.', () => {
351-
expect(preparePageInfo(fiveEdges, { first: 2 }, 5, 2).hasPreviousPage).toBe(false);
350+
it('If first is set (and after is empty), return false.', () => {
351+
expect(preparePageInfo(fiveEdges, { first: 4 }, 4, 0).hasPreviousPage).toBe(false);
352352
});
353-
it('If last was not set (and first is empty), return true.', () => {
354-
expect(preparePageInfo(fiveEdges, {}, 5, 2).hasPreviousPage).toBe(true);
353+
it('If first is set (and after is present), return true.', () => {
354+
expect(preparePageInfo(fiveEdges, { first: 4, after: 'abc' }, 4, 0).hasPreviousPage).toBe(
355+
true
356+
);
355357
});
356-
it('If last was not set (but after is present), return true.', () => {
357-
expect(preparePageInfo(fiveEdges, { after: 'abc' }, 5, 0).hasPreviousPage).toBe(true);
358+
it('If last is set (and before is empty), and edges contains more than last elements, return true.', () => {
359+
// if `first` is empty and `last` is set, `first` will be assigned according to count of edges targeted
360+
expect(preparePageInfo(fiveEdges, { first: 10, last: 5 }, 5, 5).hasPreviousPage).toBe(
361+
true
362+
);
358363
});
359-
it('If edges contains more than last elements, return true.', () => {
360-
expect(preparePageInfo(fiveEdges, { last: 3 }, 3, 2).hasPreviousPage).toBe(true);
364+
it('If last is set (and before is empty), and edges contains no more than last elements, return false.', () => {
365+
expect(preparePageInfo(fiveEdges, { first: 5, last: 5 }, 5, 0).hasPreviousPage).toBe(
366+
false
367+
);
361368
});
362-
it('Return false', () => {
363-
expect(preparePageInfo(fiveEdges, { last: 5 }, 5, 0).hasPreviousPage).toBe(false);
369+
it('If last is set (and before is present), and edges contains more than last elements, return true.', () => {
370+
expect(
371+
preparePageInfo(fiveEdges, { first: 10, last: 4, before: 'abc' }, 4, 6).hasPreviousPage
372+
).toBe(true);
373+
});
374+
it('If last is set (and before is present), and edges contains no more than last elements, return false.', () => {
375+
expect(
376+
preparePageInfo(fiveEdges, { first: 4, last: 4, before: 'abc' }, 4, 0).hasPreviousPage
377+
).toBe(false);
378+
});
379+
it('If both first and last are set, and edges contains more than last elements, return true.', () => {
380+
expect(preparePageInfo(fiveEdges, { first: 10, last: 4 }, 4, 6).hasPreviousPage).toBe(
381+
true
382+
);
383+
});
384+
it('If both first and last are set, and edges contains no more than last elements, return true.', () => {
385+
expect(preparePageInfo(fiveEdges, { first: 4, last: 4 }, 4, 0).hasPreviousPage).toBe(
386+
false
387+
);
364388
});
365389
});
366390

367391
describe('HasNextPage', () => {
368-
it('If first was not set (and last is empty), return true.', () => {
392+
it('If first was not set (and last is empty), and there is more edges, return true.', () => {
369393
// By current Relay Cursor Connections Specification
370394
// if `first` and `last` are empty `hasNextPage` should be false.
371395
// This rule is deviation from specification for better dev experience:
372396
// when first and last args are empty
373397
// we should check if exist more edges and provide correct `hasNextPage` value.
374398
expect(preparePageInfo(fiveEdges, {}, 4, 0).hasNextPage).toBe(true);
375399
});
376-
it('If first was not set (but last is present), return false.', () => {
377-
expect(preparePageInfo(fiveEdges, { last: 200 }, 4, 0).hasNextPage).toBe(false);
400+
it('If last is set (and before is empty), return false.', () => {
401+
// if `first` is empty and `last` is set, `first` will be assigned according to count of edges targeted
402+
expect(preparePageInfo(fiveEdges, { first: 10, last: 5 }, 5, 5).hasNextPage).toBe(false);
403+
});
404+
it('If last is set (and before is present), return true.', () => {
405+
expect(
406+
preparePageInfo(fiveEdges, { first: 10, last: 4, before: 'abc' }, 4, 6).hasNextPage
407+
).toBe(true);
378408
});
379-
it('If edges contains more than first elements, return true.', () => {
409+
it('If first is set (and after is empty), and edges contains more than first elements, return true.', () => {
380410
expect(preparePageInfo(fiveEdges, { first: 4 }, 4, 0).hasNextPage).toBe(true);
381411
});
382-
it('Return false', () => {
412+
it('If first is set (and after is empty), and edges contains no more than first elements, return false.', () => {
383413
expect(preparePageInfo(fiveEdges, { first: 5 }, 5, 0).hasNextPage).toBe(false);
384414
});
415+
it('If first is set (and after is present), and edges contains more than first elements, return true.', () => {
416+
expect(preparePageInfo(fiveEdges, { first: 4, after: 'abc' }, 4, 0).hasNextPage).toBe(
417+
true
418+
);
419+
});
420+
it('If first is set (and after is present), and edges contains no more than first elements, return false.', () => {
421+
expect(preparePageInfo(fiveEdges, { first: 5, after: 'abc' }, 5, 0).hasNextPage).toBe(
422+
false
423+
);
424+
});
425+
it('If both first and last are set, and edges contains more than first elements, return true.', () => {
426+
expect(preparePageInfo(fiveEdges, { first: 10, last: 4 }, 4, 6).hasNextPage).toBe(true);
427+
});
428+
it('If both first and last are set, and edges contains no more than first elements, return false.', () => {
429+
expect(preparePageInfo(fiveEdges, { first: 10, last: 6 }, 6, 4).hasNextPage).toBe(false);
430+
});
385431
});
386432

387433
it('should return startCursor', () => {

src/connectionResolver.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ export function preparePageInfo(
314314
last?: ?number,
315315
first?: ?number,
316316
after?: string,
317+
before?: string,
317318
},
318319
limit: number,
319320
skip: number
@@ -335,8 +336,8 @@ export function preparePageInfo(
335336
} else {
336337
pageInfo.endCursor = edges[edges.length - 1].cursor;
337338
}
338-
pageInfo.hasPreviousPage = (!!args.last || !args.first) && (skip > 0 || !!args.after);
339-
pageInfo.hasNextPage = (!!args.first || !args.last) && hasExtraRecords;
339+
pageInfo.hasPreviousPage = skip > 0 || !!args.after;
340+
pageInfo.hasNextPage = hasExtraRecords || !!args.before;
340341
}
341342

342343
return pageInfo;

0 commit comments

Comments
 (0)