update_order_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::User::Overview::UpdateOrder, current_user_id: 1 do
  4. subject(:service) { described_class.new(user, overviews) }
  5. let(:user) { create(:agent) }
  6. let(:overview_1) { create(:overview, prio: 1) }
  7. let(:overview_2) { create(:overview, prio: 2) }
  8. let(:overview_3) { create(:overview, prio: 3) }
  9. let(:overview_4) { create(:overview, prio: 4) }
  10. before do
  11. Overview.destroy_all
  12. [overview_1, overview_2, overview_3, overview_4].each do |elem|
  13. elem.users << user
  14. end
  15. end
  16. context 'when no custom sorting exists beforehand' do
  17. context 'when all overviews given' do
  18. let(:overviews) { [overview_3, overview_2, overview_1, overview_4] }
  19. it 'sets priorities' do
  20. service.execute
  21. expect(user.overview_sortings).to contain_exactly(
  22. have_attributes(overview: overview_1, prio: 2),
  23. have_attributes(overview: overview_2, prio: 1),
  24. have_attributes(overview: overview_3, prio: 0),
  25. have_attributes(overview: overview_4, prio: 3),
  26. )
  27. end
  28. end
  29. context 'when some overviews given' do
  30. let(:overviews) { [overview_3, overview_2, overview_1] }
  31. it 'sets given priorities' do
  32. service.execute
  33. expect(user.overview_sortings).to contain_exactly(
  34. have_attributes(overview: overview_1, prio: 2),
  35. have_attributes(overview: overview_2, prio: 1),
  36. have_attributes(overview: overview_3, prio: 0),
  37. )
  38. end
  39. it 'has no custom priority for item that was not given' do
  40. service.execute
  41. expect(user.overview_sortings.where(user:, overview: overview_4)).not_to be_exists
  42. end
  43. end
  44. end
  45. context 'when custom sorting exists beforehand' do
  46. before do
  47. create(:user_overview_sorting, overview: overview_1, prio: 4, user:)
  48. create(:user_overview_sorting, overview: overview_2, prio: 3, user:)
  49. create(:user_overview_sorting, overview: overview_3, prio: 2, user:)
  50. create(:user_overview_sorting, overview: overview_4, prio: 1, user:)
  51. end
  52. context 'when all overviews given' do
  53. let(:overviews) { [overview_3, overview_2, overview_1, overview_4] }
  54. it 'sets priorities' do
  55. service.execute
  56. expect(user.overview_sortings).to contain_exactly(
  57. have_attributes(overview: overview_1, prio: 2),
  58. have_attributes(overview: overview_2, prio: 1),
  59. have_attributes(overview: overview_3, prio: 0),
  60. have_attributes(overview: overview_4, prio: 3),
  61. )
  62. end
  63. end
  64. context 'when some overviews given' do
  65. let(:overviews) { [overview_3, overview_2, overview_1] }
  66. it 'sets given priorities' do
  67. service.execute
  68. expect(user.overview_sortings).to contain_exactly(
  69. have_attributes(overview: overview_1, prio: 2),
  70. have_attributes(overview: overview_2, prio: 1),
  71. have_attributes(overview: overview_3, prio: 0),
  72. )
  73. end
  74. it 'has no custom priority for item that was not given' do
  75. service.execute
  76. expect(user.overview_sortings.where(user:, overview: overview_4)).not_to be_exists
  77. end
  78. end
  79. end
  80. end