pagination.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 mocha = require('mocha');
  20. const expect = require('chai').expect;
  21. const mock_http = require('node-mocks-http');
  22. const pagination = require('@joystream/storage-utils/pagination');
  23. describe('util/pagination', function()
  24. {
  25. describe('openapi()', function()
  26. {
  27. it('should add parameters and definitions to an API spec', function()
  28. {
  29. var api = pagination.openapi({});
  30. // Parameters
  31. expect(api).to.have.property('components');
  32. expect(api.components).to.have.property('parameters');
  33. expect(api.components.parameters).to.have.property('paginationLimit');
  34. expect(api.components.parameters.paginationLimit).to.have.property('name');
  35. expect(api.components.parameters.paginationLimit.name).to.equal('limit');
  36. expect(api.components.parameters.paginationLimit).to.have.property('schema');
  37. expect(api.components.parameters.paginationLimit.schema).to.have.property('type');
  38. expect(api.components.parameters.paginationLimit.schema.type).to.equal('integer');
  39. expect(api.components.parameters.paginationOffset).to.have.property('name');
  40. expect(api.components.parameters.paginationOffset.name).to.equal('offset');
  41. expect(api.components.parameters.paginationOffset).to.have.property('schema');
  42. expect(api.components.parameters.paginationOffset.schema).to.have.property('type');
  43. expect(api.components.parameters.paginationOffset.schema.type).to.equal('integer');
  44. // Defintiions
  45. expect(api.components).to.have.property('schemas');
  46. expect(api.components.schemas).to.have.property('PaginationInfo');
  47. expect(api.components.schemas.PaginationInfo).to.have.property('type');
  48. expect(api.components.schemas.PaginationInfo.type).to.equal('object');
  49. expect(api.components.schemas.PaginationInfo).to.have.property('properties');
  50. expect(api.components.schemas.PaginationInfo.properties)
  51. .to.be.an('object')
  52. .that.has.all.keys('self', 'next', 'prev', 'first', 'last');
  53. });
  54. });
  55. describe('paginate()', function()
  56. {
  57. it('should add pagination links to a response object', function()
  58. {
  59. var req = mock_http.createRequest({
  60. method: 'GET',
  61. url: '/foo?limit=10',
  62. query: {
  63. limit: 10, // Mock is a little stupid, we have to explicitly set query
  64. },
  65. headers: {
  66. host: 'localhost',
  67. },
  68. protocol: 'http',
  69. });
  70. var res = pagination.paginate(req, {});
  71. expect(res).to.have.property('pagination')
  72. .that.has.all.keys('self', 'first', 'next');
  73. expect(res.pagination.self).to.equal('http://localhost/foo?limit=10');
  74. expect(res.pagination.first).to.equal('http://localhost/foo?limit=10&offset=0');
  75. expect(res.pagination.next).to.equal('http://localhost/foo?limit=10&offset=10');
  76. });
  77. it('should add a last pagination link when requested', function()
  78. {
  79. var req = mock_http.createRequest({
  80. method: 'GET',
  81. url: '/foo?limit=10&offset=15',
  82. query: {
  83. limit: 10, // Mock is a little stupid, we have to explicitly set query
  84. offset: 15,
  85. },
  86. headers: {
  87. host: 'localhost',
  88. },
  89. protocol: 'http',
  90. });
  91. var res = pagination.paginate(req, {}, 35);
  92. expect(res).to.have.property('pagination')
  93. .that.has.all.keys('self', 'first', 'next', 'prev', 'last');
  94. expect(res.pagination.self).to.equal('http://localhost/foo?limit=10&offset=15');
  95. expect(res.pagination.first).to.equal('http://localhost/foo?limit=10&offset=0');
  96. expect(res.pagination.last).to.equal('http://localhost/foo?limit=10&offset=35');
  97. expect(res.pagination.prev).to.equal('http://localhost/foo?limit=10&offset=5');
  98. expect(res.pagination.next).to.equal('http://localhost/foo?limit=10&offset=25');
  99. });
  100. });
  101. });