123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- 'use strict'
- const expect = require('chai').expect
- const mockHttp = require('node-mocks-http')
- const pagination = require('@joystream/storage-utils/pagination')
- describe('util/pagination', function () {
- describe('openapi()', function () {
- it('should add parameters and definitions to an API spec', function () {
- const api = pagination.openapi({})
-
- expect(api).to.have.property('components')
- expect(api.components).to.have.property('parameters')
- expect(api.components.parameters).to.have.property('paginationLimit')
- expect(api.components.parameters.paginationLimit).to.have.property('name')
- expect(api.components.parameters.paginationLimit.name).to.equal('limit')
- expect(api.components.parameters.paginationLimit).to.have.property('schema')
- expect(api.components.parameters.paginationLimit.schema).to.have.property('type')
- expect(api.components.parameters.paginationLimit.schema.type).to.equal('integer')
- expect(api.components.parameters.paginationOffset).to.have.property('name')
- expect(api.components.parameters.paginationOffset.name).to.equal('offset')
- expect(api.components.parameters.paginationOffset).to.have.property('schema')
- expect(api.components.parameters.paginationOffset.schema).to.have.property('type')
- expect(api.components.parameters.paginationOffset.schema.type).to.equal('integer')
-
- expect(api.components).to.have.property('schemas')
- expect(api.components.schemas).to.have.property('PaginationInfo')
- expect(api.components.schemas.PaginationInfo).to.have.property('type')
- expect(api.components.schemas.PaginationInfo.type).to.equal('object')
- expect(api.components.schemas.PaginationInfo).to.have.property('properties')
- expect(api.components.schemas.PaginationInfo.properties)
- .to.be.an('object')
- .that.has.all.keys('self', 'next', 'prev', 'first', 'last')
- })
- })
- describe('paginate()', function () {
- it('should add pagination links to a response object', function () {
- const req = mockHttp.createRequest({
- method: 'GET',
- url: '/foo?limit=10',
- query: {
- limit: 10,
- },
- headers: {
- host: 'localhost',
- },
- protocol: 'http',
- })
- const res = pagination.paginate(req, {})
- expect(res).to.have.property('pagination').that.has.all.keys('self', 'first', 'next')
- expect(res.pagination.self).to.equal('http://localhost/foo?limit=10')
- expect(res.pagination.first).to.equal('http://localhost/foo?limit=10&offset=0')
- expect(res.pagination.next).to.equal('http://localhost/foo?limit=10&offset=10')
- })
- it('should add a last pagination link when requested', function () {
- const req = mockHttp.createRequest({
- method: 'GET',
- url: '/foo?limit=10&offset=15',
- query: {
- limit: 10,
- offset: 15,
- },
- headers: {
- host: 'localhost',
- },
- protocol: 'http',
- })
- const res = pagination.paginate(req, {}, 35)
- expect(res).to.have.property('pagination').that.has.all.keys('self', 'first', 'next', 'prev', 'last')
- expect(res.pagination.self).to.equal('http://localhost/foo?limit=10&offset=15')
- expect(res.pagination.first).to.equal('http://localhost/foo?limit=10&offset=0')
- expect(res.pagination.last).to.equal('http://localhost/foo?limit=10&offset=35')
- expect(res.pagination.prev).to.equal('http://localhost/foo?limit=10&offset=5')
- expect(res.pagination.next).to.equal('http://localhost/foo?limit=10&offset=25')
- })
- })
- })
|