pagination_examples.rb 4.1 KB

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