test('should handle backward pagination at root with "last" and "before" args', async t => { const query = makeUsersQuery({ last: 1, before: objToCursor({ id: 2 }) }) const { data, errors } = await run(query) errCheck(t, errors) t.deepEqual(data.users.pageInfo, { hasNextPage: false, hasPreviousPage: false, startCursor: objToCursor({ id: 1 }), endCursor: objToCursor({ id: 1 }) }) t.is(data.users.edges.length, 1) })
test('nested paging should handle "first" arg', async t => { const query = makePostsQuery({ first: 3 }) const { data, errors } = await run(query) errCheck(t, errors) const posts = data.user.posts t.is(posts.total, 8) t.deepEqual(posts.pageInfo, { hasNextPage: true, startCursor: offsetToCursor(0), endCursor: offsetToCursor(2) }) t.is(posts.edges.length, 3) t.is(posts.edges.last().cursor, posts.pageInfo.endCursor) })
test('handle pagination for filtered nodes', async () => { const { errors, data } = await createSchemaAndExecute(`{ allProduct (perPage: 2, filter: { title: { regex: "Do[l|j]or" } }) { totalCount pageInfo { totalPages } edges { node { id } } } }`) expect(errors).toBeUndefined() expect(data.allProduct.edges).toHaveLength(2) expect(data.allProduct.totalCount).toEqual(2) expect(data.allProduct.pageInfo.totalPages).toEqual(1) })
test('should return empty results', async () => { const results = await graphql('1', 0) expect(results.errors).toBeUndefined() expect(results.data.tag.belongsTo.totalCount).toEqual(0) expect(results.data.tag.belongsTo.edges).toHaveLength(0) expect(results.data.tag.belongsTo.pageInfo).toMatchObject({ perPage: 0, totalPages: 1, currentPage: 1, totalItems: 0, hasPreviousPage: false, hasNextPage: false, isFirst: true, isLast: true }) })
test('nested paging should handle "first" and "after" args that reaches the last page', async t => { const query = makePostsQuery({ first: 5, after: offsetToCursor(3) }) const { data, errors } = await run(query) errCheck(t, errors) const posts = data.user.posts t.is(posts.total, 8) t.deepEqual(posts.pageInfo, { hasNextPage: false, startCursor: offsetToCursor(4), endCursor: offsetToCursor(7) }) t.is(posts.edges.length, 4) t.is(posts.edges.last().cursor, posts.pageInfo.endCursor) })
test('should handle backward pagination at root with "last" arg', async t => { const query = makeUsersQuery({ last: 2 }) const { data, errors } = await run(query) errCheck(t, errors) t.deepEqual(data.users.pageInfo, { hasNextPage: false, hasPreviousPage: true, startCursor: objToCursor({ id: 5 }), endCursor: objToCursor({ id: 6 }) }) t.is(data.users.edges[0].node.id, toGlobalId('User', 5)) t.is(data.users.edges[1].node.id, toGlobalId('User', 6)) })
test('should return empty results', async () => { const results = await graphql(0) expect(results.errors).toBeUndefined() expect(results.data.allPost.totalCount).toEqual(0) expect(results.data.allPost.edges).toHaveLength(0) expect(results.data.allPost.pageInfo).toMatchObject({ perPage: 0, totalPages: 1, currentPage: 1, totalItems: 0, hasPreviousPage: false, hasNextPage: false, isFirst: true, isLast: true }) })
test('nested paging should handle "first" arg', async t => { const query = makePostsQuery({ first: 3 }) const { data, errors } = await run(query) errCheck(t, errors) const posts = data.user.posts t.deepEqual(posts.pageInfo, { hasNextPage: true, hasPreviousPage: false, startCursor: objToCursor({ created_at: '2016-04-17T18:49:15.942Z', id: 2 }), endCursor: objToCursor({ created_at: '2016-04-13T15:07:15.119Z', id: 33 }) }) t.is(posts.edges.length, 3) t.is(posts.edges.last().cursor, posts.pageInfo.endCursor) })
test('limit results with perPage argument', async () => { const results = await graphql(100, 'perPage: 10') expect(results.errors).toBeUndefined() expect(results.data.allPost.totalCount).toEqual(100) expect(results.data.allPost.edges).toHaveLength(10) expect(results.data.allPost.pageInfo).toMatchObject({ perPage: 10, totalPages: 1, currentPage: 1, totalItems: 10, hasPreviousPage: false, hasNextPage: false, isFirst: true, isLast: true }) })
test('handle pagination for filtered nodes', async () => { const { errors, data } = await createSchemaAndExecute(`{ allProduct (perPage: 2, filter: { title: { regex: "Do[l|j]or" } }) { totalCount pageInfo { totalPages } edges { node { id } } } }`) expect(errors).toBeUndefined() expect(data.allProduct.edges).toHaveLength(2) expect(data.allProduct.totalCount).toEqual(2) expect(data.allProduct.pageInfo.totalPages).toEqual(1) })
test('should return all nodes', async () => { const results = await graphql(100) expect(results.errors).toBeUndefined() expect(results.data.allPost.totalCount).toEqual(100) expect(results.data.allPost.edges).toHaveLength(100) expect(results.data.allPost.pageInfo).toMatchObject({ perPage: 100, totalPages: 1, currentPage: 1, totalItems: 100, hasPreviousPage: false, hasNextPage: false, isFirst: true, isLast: true }) })
test('limit results with perPage argument', async () => { const results = await graphql(100, 'perPage: 10') expect(results.errors).toBeUndefined() expect(results.data.allPost.totalCount).toEqual(100) expect(results.data.allPost.edges).toHaveLength(10) expect(results.data.allPost.pageInfo).toMatchObject({ perPage: 10, totalPages: 1, currentPage: 1, totalItems: 10, hasPreviousPage: false, hasNextPage: false, isFirst: true, isLast: true }) })
test('limit results with perPage argument', async () => { const results = await graphql('1', 100, 'perPage: 10') expect(results.errors).toBeUndefined() expect(results.data.tag.belongsTo.totalCount).toEqual(100) expect(results.data.tag.belongsTo.edges).toHaveLength(10) expect(results.data.tag.belongsTo.pageInfo).toMatchObject({ perPage: 10, totalPages: 1, currentPage: 1, totalItems: 10, hasPreviousPage: false, hasNextPage: false, isFirst: true, isLast: true }) })
test('should return all nodes', async () => { const results = await graphql('1', 100) expect(results.errors).toBeUndefined() expect(results.data.tag.belongsTo.totalCount).toEqual(100) expect(results.data.tag.belongsTo.edges).toHaveLength(100) expect(results.data.tag.belongsTo.pageInfo).toMatchObject({ perPage: 100, totalPages: 1, currentPage: 1, totalItems: 100, hasPreviousPage: false, hasNextPage: false, isFirst: true, isLast: true }) })
test('should return empty results', async () => { const results = await graphql('1', 0) expect(results.errors).toBeUndefined() expect(results.data.tag.belongsTo.totalCount).toEqual(0) expect(results.data.tag.belongsTo.edges).toHaveLength(0) expect(results.data.tag.belongsTo.pageInfo).toMatchObject({ perPage: 0, totalPages: 1, currentPage: 1, totalItems: 0, hasPreviousPage: false, hasNextPage: false, isFirst: true, isLast: true }) })