Skip to content

Commit

Permalink
fix: hasPreviousPage check if present only after arg
Browse files Browse the repository at this point in the history
  • Loading branch information
nodkz committed Sep 30, 2017
1 parent 9de6600 commit 461a33f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
29 changes: 27 additions & 2 deletions src/__tests__/connectionResolver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,11 @@ describe('connectionResolver', () => {

describe('"Relay Cursor Connections Specification (PageInfo)":', () => {
describe('HasPreviousPage', () => {
it('If last was not set, return false.', () => {
expect(preparePageInfo(fiveEdges, {}, 5, 2).hasPreviousPage).toBe(false);
it('If last was not set (but first is present), return false.', () => {
expect(preparePageInfo(fiveEdges, { first: 2 }, 5, 2).hasPreviousPage).toBe(false);
});
it('If last was not set (and first is empty), return true.', () => {
expect(preparePageInfo(fiveEdges, {}, 5, 2).hasPreviousPage).toBe(true);
});
it('If edges contains more than last elements, return true.', () => {
expect(preparePageInfo(fiveEdges, { last: 3 }, 3, 2).hasPreviousPage).toBe(true);
Expand Down Expand Up @@ -811,4 +814,26 @@ describe('connectionResolver', () => {
expect(result.edges[1].node.id).toBe(2);
});
});

describe('default `first` argument if first/last are empty', () => {
const defaultResolver = prepareConnectionResolver(userTypeComposer, {
countResolverName: 'count',
findResolverName: 'findMany',
sort: sortOptions,
defaultLimit: 5,
});

it('should use defaultLimit option', async () => {
const data = await defaultResolver.resolve({ args: {} });
expect(data.edges.length).toBe(5);
expect(data.pageInfo.hasNextPage).toBe(true);
});

it('should use defaultLimit option with after option', async () => {
const data = await defaultResolver.resolve({ args: { after: 'NA==' } });
expect(data.edges.length).toBe(5);
expect(data.pageInfo.hasNextPage).toBe(true);
expect(data.pageInfo.hasPreviousPage).toBe(true);
});
});
});
2 changes: 1 addition & 1 deletion src/connectionResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export function preparePageInfo(
} else {
pageInfo.endCursor = edges[edges.length - 1].cursor;
}
pageInfo.hasPreviousPage = !!args.last && skip > 0;
pageInfo.hasPreviousPage = (!!args.last || !args.first) && skip > 0;
pageInfo.hasNextPage = (!!args.first || !args.last) && hasExtraRecords;
}
Expand Down

0 comments on commit 461a33f

Please sign in to comment.