pagination_examples.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'pagination', authenticated_as: :authenticate do |model:, klass:, path:, sort_by: :name, create_params: {}|
  3. let(:create_params) { create_params }
  4. let(:model) { model }
  5. let(:klass) { klass }
  6. let(:indexable) { Models.indexable.include?(klass) }
  7. def authenticate
  8. create_list(model, 500, **create_params)
  9. true
  10. end
  11. def offset_first_of_page(page, entries_per_page)
  12. (entries_per_page * (page - 1)) + 1
  13. end
  14. before do
  15. visit path
  16. end
  17. it 'does paginate' do
  18. entries_per_page = page.all('.js-tableBody tr').count
  19. expect(page).to have_css('.js-pager')
  20. class_page1 = klass.reorder(sort_by => :asc, id: :asc).offset(offset_first_of_page(1, entries_per_page)).first
  21. expect(page).to have_text(class_page1.name)
  22. expect(page).to have_css('.js-page.btn--active', text: '1')
  23. expect(page).to have_no_css('.js-tableBody table-draggable')
  24. page.first('.js-page', text: '2').click
  25. class_page2 = klass.reorder(sort_by => :asc, id: :asc).offset(offset_first_of_page(2, entries_per_page)).first
  26. expect(page).to have_text(class_page2.name)
  27. expect(page).to have_css('.js-page.btn--active', text: '2')
  28. expect(page).to have_no_css('.js-tableBody table-draggable')
  29. page.first('.js-page', text: '3').click
  30. class_page3 = klass.reorder(sort_by => :asc, id: :asc).offset(offset_first_of_page(3, entries_per_page)).first
  31. expect(page).to have_text(class_page3.name)
  32. expect(page).to have_css('.js-page.btn--active', text: '3')
  33. expect(page).to have_no_css('.js-tableBody table-draggable')
  34. page.first('.js-page', text: '4').click
  35. class_page4 = klass.reorder(sort_by => :asc, id: :asc).offset(offset_first_of_page(4, entries_per_page)).first
  36. expect(page).to have_text(class_page4.name)
  37. expect(page).to have_css('.js-page.btn--active', text: '4')
  38. expect(page).to have_no_css('.js-tableBody table-draggable')
  39. page.first('.js-page', text: '1').click
  40. page.first('.js-tableHead[data-column-key=name]').click
  41. class_page1 = klass.reorder(name: :asc, id: :asc).offset(offset_first_of_page(1, entries_per_page)).first
  42. expect(page).to have_text(class_page1.name)
  43. expect(page).to have_css('.js-page.btn--active', text: '1')
  44. expect(page).to have_no_css('.js-tableBody table-draggable')
  45. page.first('.js-tableHead[data-column-key=name]').click
  46. class_last = klass.reorder(name: :desc, id: :asc).offset(offset_first_of_page(1, entries_per_page)).first
  47. expect(page).to have_text(class_last.name)
  48. end
  49. context 'when search is enabled' do
  50. before do
  51. skip 'No search field enabled' if !indexable || !page.has_css?('.page-content .searchfield .js-search')
  52. end
  53. it 'does filter results with the search bar' do
  54. page.find('.js-search').fill_in with: klass.last.try(:name) || klass.last.try(klass.columns.find { |row| row.type == :string }.name.to_sym)
  55. wait.until { page.all('.js-tableBody tr').count == 1 }
  56. # does stay after reload
  57. refresh
  58. wait.until { page.find('.js-search').present? && page.all('.js-tableBody tr').count == 1 }
  59. # remove filter
  60. page.find('.js-search').fill_in with: '', fill_options: { clear: :backspace }
  61. wait.until { page.all('.js-tableBody tr').count != 1 }
  62. end
  63. context 'when ES is enabled', authenticated_as: :authenticate, searchindex: true do
  64. def authenticate
  65. create_list(model, 500, **create_params)
  66. searchindex_model_reload([klass]) if indexable
  67. create(:admin)
  68. end
  69. it 'does only show 2 pages because of a search filter and paginate through it' do
  70. entries_per_page = page.all('.js-tableBody tr').count
  71. search_query = klass.limit(entries_per_page * 2).pluck(:id).map { |i| "id: #{i}" }.join(' OR ')
  72. page.find('.js-search').fill_in with: search_query, fill_options: { clear: :backspace }
  73. wait.until { page.first('.js-pager').all('.js-page').count == 4 }
  74. page.first('.js-page', text: '2').click
  75. expect(page).to have_css('.js-page.btn--active', text: '2')
  76. expect(page).to have_no_css('.js-tableBody table-draggable')
  77. wait.until { page.find('.js-search').present? && page.find('.js-search').value == search_query && page.first('.js-pager').all('.js-page').count == 4 }
  78. end
  79. end
  80. end
  81. end