Sha256: 4d20920defc0c23ca6c53998fc13f113726b05a2094753caa640209313012eb7

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

require 'spec_helper'
require 'json'

class PaginatedAPI < Grape::API
  include Grape::Kaminari

  params do
    use :pagination
  end
  get '' do
    paginate(Kaminari.paginate_array((1..10).to_a))
  end

  params do
    use :pagination, offset: false
  end
  get 'no-offset' do
    paginate(Kaminari.paginate_array((1..10).to_a))
  end

  resource :sub do
    params do
      use :pagination, per_page: 2
    end
    get '/' do
      paginate(Kaminari.paginate_array((1..10).to_a))
    end
  end
end

describe Grape::Kaminari do
  subject { PaginatedAPI.new }
  let(:app)  { subject }
  let(:json) { JSON.parse(last_response.body) }
  let(:header) { last_response.header }

  describe 'paginated helper' do
    it 'returns the first page' do
      get '/', page: 1, per_page: 3
      expect(json).to eq [1, 2, 3]
    end

    it 'returns the second page' do
      get '/', page: 2, per_page: 3
      expect(json).to eq [4, 5, 6]
    end

    # This is here to ensure that Kaminari can handle `padding(false)`
    # and still do the right thing.
    it 'works when offset is false' do
      get '/no-offset', page: 1, per_page: 3
      expect(json).to eq [1, 2, 3]
    end

    it 'sets headers' do
      get '/', page: 3, per_page: 2, offset: 1
      expect(header['X-Total']).to eq '10'
      expect(header['X-Total-Pages']).to eq '5'
      expect(header['X-Per-Page']).to eq '2'
      expect(header['X-Page']).to eq '3'
      expect(header['X-Next-Page']).to eq '4'
      expect(header['X-Prev-Page']).to eq '2'
      expect(header['X-Offset']).to eq '1'
    end

    it 'can be inherited' do
      get '/sub', page: 1
      expect(json).to eq [1, 2]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grape-kaminari-0.4.0 spec/paginate_helper_spec.rb