Skip to content

Commit

Permalink
Set links.next to null when last page is passed
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed Apr 5, 2018
1 parent b1da391 commit 08db7fe
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
10 changes: 10 additions & 0 deletions lib/utils/createPagePath-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ test('should return path for next page and replace existing page qs', (t) => {
t.is(ret, expected)
})

test('should return null when no next page', (t) => {
const paging = {next: null}
const request = {path: '/pages'}
const expected = null

const ret = createPagePath(paging, request)

t.is(ret, expected)
})

test('should return path with baseUri', (t) => {
const paging = {next: {type: 'page', pageAfter: 'page19', pageSize: 20}}
const request = {path: '/pages'}
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/createPagePath.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const querystring = require('querystring')

function createPagePath (paging, {path}, baseUri = '') {
if (!paging.next) {
return null
}

if (baseUri.endsWith('/')) {
baseUri = baseUri.substr(0, baseUri.length - 1)
}
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "integreat-api-json",
"version": "0.1.1",
"version": "0.1.2",
"description": "Integreat json api",
"author": "Kjell-Morten Bratsberg Thorsen <[email protected]> (http://kjellmorten.no)",
"license": "ISC",
Expand Down Expand Up @@ -53,6 +53,6 @@
"standard": "^11.0.1"
},
"dependencies": {
"jsonwebtoken": "^8.2.0"
"jsonwebtoken": "^8.2.1"
}
}
8 changes: 8 additions & 0 deletions tests/helpers/adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ const send = async ({params, action, data, relationship}) => {
next: {type: 'page', pageSize: 2, pageAfter: 'page4'}
}
}
} else if (params.pageAfter === 'page4') {
return {
status: 'ok',
data: setDateAttrs([]),
paging: {
next: null
}
}
}
} else if (params.authCode === '12345') {
return {
Expand Down
38 changes: 32 additions & 6 deletions tests/paging-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const defs = {

const great = integreat(defs, {adapters})

const firstPage = 'eyJ0eXBlIjoicGFnZSIsInBhZ2VTaXplIjoyLCJwYWdlQWZ0ZXIiOiJwYWdlMiJ9'
const secondPage = 'eyJ0eXBlIjoicGFnZSIsInBhZ2VTaXplIjoyLCJwYWdlQWZ0ZXIiOiJwYWdlNCJ9'
const secondPage = 'eyJ0eXBlIjoicGFnZSIsInBhZ2VTaXplIjoyLCJwYWdlQWZ0ZXIiOiJwYWdlMiJ9'
const thirdPage = 'eyJ0eXBlIjoicGFnZSIsInBhZ2VTaXplIjoyLCJwYWdlQWZ0ZXIiOiJwYWdlNCJ9'

// Tests

Expand Down Expand Up @@ -55,7 +55,7 @@ test('should GET first page', async (t) => {
first: null,
last: null,
prev: null,
next: `https://api.somesite.com/pages?page=${firstPage}`
next: `https://api.somesite.com/pages?page=${secondPage}`
}
}

Expand All @@ -72,8 +72,8 @@ test('should GET first page', async (t) => {
test('should GET second page', async (t) => {
const request = {
method: 'GET',
path: `/pages?page=${firstPage}`,
params: {page: firstPage}
path: `/pages?page=${secondPage}`,
params: {page: secondPage}
}
const expected = {
data: [
Expand All @@ -94,7 +94,33 @@ test('should GET second page', async (t) => {
first: null,
last: null,
prev: null,
next: `/pages?page=${secondPage}`
next: `/pages?page=${thirdPage}`
}
}

const routes = jsonapi(great)
const route = findRoute(routes, {path: '/pages', method: 'GET'})
const response = await route.handler(request)

t.truthy(response)
t.is(response.statusCode, 200, response.statusMessage)
t.truthy(response.body)
t.deepEqual(response.body, expected)
})

test('should GET empty result after last page', async (t) => {
const request = {
method: 'GET',
path: `/pages?page=${thirdPage}`,
params: {page: thirdPage}
}
const expected = {
data: [],
links: {
first: null,
last: null,
prev: null,
next: null
}
}

Expand Down

0 comments on commit 08db7fe

Please sign in to comment.