pagination_spec.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe CanPaginate::Pagination do
  4. describe '#limit' do
  5. it 'returns as set in params' do
  6. instance = described_class.new({ per_page: 123 })
  7. expect(instance.limit).to be 123
  8. end
  9. it 'ensures that per_page is an integer' do
  10. instance = described_class.new({ per_page: '123' })
  11. expect(instance.limit).to be 123
  12. end
  13. it 'when missing, returns as set in limit attribute' do
  14. instance = described_class.new({ limit: 123 })
  15. expect(instance.limit).to be 123
  16. end
  17. it 'falls back to default' do
  18. instance = described_class.new({})
  19. expect(instance.limit).to be 100
  20. end
  21. it 'falls back to custom default' do
  22. instance = described_class.new({}, default: 222)
  23. expect(instance.limit).to be 222
  24. end
  25. it 'per_page attribute preferred over limit' do
  26. instance = described_class.new({ per_page: 123, limit: 321 })
  27. expect(instance.limit).to be 123
  28. end
  29. it 'capped by limit' do
  30. instance = described_class.new({ per_page: 9999 })
  31. expect(instance.limit).to be 1000
  32. end
  33. it 'capped by custom default' do
  34. instance = described_class.new({ per_page: 9999 }, max: 10)
  35. expect(instance.limit).to be 10
  36. end
  37. end
  38. describe '#page' do
  39. it 'returns page number' do
  40. instance = described_class.new({ page: 123 })
  41. expect(instance.page).to be 123
  42. end
  43. it 'defaults to 1 when missing' do
  44. instance = described_class.new({})
  45. expect(instance.page).to be 1
  46. end
  47. it 'ensures that page is an integer' do
  48. instance = described_class.new({ page: '123' })
  49. expect(instance.page).to be 123
  50. end
  51. end
  52. describe '#offset' do
  53. it 'returns 0 when no page given' do
  54. instance = described_class.new({})
  55. expect(instance.offset).to be 0
  56. end
  57. it 'returns offset for page' do
  58. instance = described_class.new({ page: 3 })
  59. expect(instance.offset).to be 200
  60. end
  61. it 'returns offset based on custom per_page value' do
  62. instance = described_class.new({ page: 3, per_page: 15 })
  63. expect(instance.offset).to be 30
  64. end
  65. end
  66. end