pagination.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * This file is part of the storage node for the Joystream project.
  3. * Copyright (C) 2019 Joystream Contributors
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. 'use strict'
  19. const expect = require('chai').expect
  20. const mockHttp = require('node-mocks-http')
  21. const pagination = require('@joystream/storage-utils/pagination')
  22. describe('util/pagination', function () {
  23. describe('openapi()', function () {
  24. it('should add parameters and definitions to an API spec', function () {
  25. const api = pagination.openapi({})
  26. // Parameters
  27. expect(api).to.have.property('components')
  28. expect(api.components).to.have.property('parameters')
  29. expect(api.components.parameters).to.have.property('paginationLimit')
  30. expect(api.components.parameters.paginationLimit).to.have.property('name')
  31. expect(api.components.parameters.paginationLimit.name).to.equal('limit')
  32. expect(api.components.parameters.paginationLimit).to.have.property('schema')
  33. expect(api.components.parameters.paginationLimit.schema).to.have.property('type')
  34. expect(api.components.parameters.paginationLimit.schema.type).to.equal('integer')
  35. expect(api.components.parameters.paginationOffset).to.have.property('name')
  36. expect(api.components.parameters.paginationOffset.name).to.equal('offset')
  37. expect(api.components.parameters.paginationOffset).to.have.property('schema')
  38. expect(api.components.parameters.paginationOffset.schema).to.have.property('type')
  39. expect(api.components.parameters.paginationOffset.schema.type).to.equal('integer')
  40. // Defintiions
  41. expect(api.components).to.have.property('schemas')
  42. expect(api.components.schemas).to.have.property('PaginationInfo')
  43. expect(api.components.schemas.PaginationInfo).to.have.property('type')
  44. expect(api.components.schemas.PaginationInfo.type).to.equal('object')
  45. expect(api.components.schemas.PaginationInfo).to.have.property('properties')
  46. expect(api.components.schemas.PaginationInfo.properties)
  47. .to.be.an('object')
  48. .that.has.all.keys('self', 'next', 'prev', 'first', 'last')
  49. })
  50. })
  51. describe('paginate()', function () {
  52. it('should add pagination links to a response object', function () {
  53. const req = mockHttp.createRequest({
  54. method: 'GET',
  55. url: '/foo?limit=10',
  56. query: {
  57. limit: 10, // Mock is a little stupid, we have to explicitly set query
  58. },
  59. headers: {
  60. host: 'localhost',
  61. },
  62. protocol: 'http',
  63. })
  64. const res = pagination.paginate(req, {})
  65. expect(res).to.have.property('pagination').that.has.all.keys('self', 'first', 'next')
  66. expect(res.pagination.self).to.equal('http://localhost/foo?limit=10')
  67. expect(res.pagination.first).to.equal('http://localhost/foo?limit=10&offset=0')
  68. expect(res.pagination.next).to.equal('http://localhost/foo?limit=10&offset=10')
  69. })
  70. it('should add a last pagination link when requested', function () {
  71. const req = mockHttp.createRequest({
  72. method: 'GET',
  73. url: '/foo?limit=10&offset=15',
  74. query: {
  75. limit: 10, // Mock is a little stupid, we have to explicitly set query
  76. offset: 15,
  77. },
  78. headers: {
  79. host: 'localhost',
  80. },
  81. protocol: 'http',
  82. })
  83. const res = pagination.paginate(req, {}, 35)
  84. expect(res).to.have.property('pagination').that.has.all.keys('self', 'first', 'next', 'prev', 'last')
  85. expect(res.pagination.self).to.equal('http://localhost/foo?limit=10&offset=15')
  86. expect(res.pagination.first).to.equal('http://localhost/foo?limit=10&offset=0')
  87. expect(res.pagination.last).to.equal('http://localhost/foo?limit=10&offset=35')
  88. expect(res.pagination.prev).to.equal('http://localhost/foo?limit=10&offset=5')
  89. expect(res.pagination.next).to.equal('http://localhost/foo?limit=10&offset=25')
  90. })
  91. })
  92. })