overview_spec.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/application_model_examples'
  4. RSpec.describe Overview, type: :model do
  5. it_behaves_like 'ApplicationModel', can_assets: { associations: :users, selectors: :condition }
  6. context 'link generation' do
  7. it 'generates from name' do
  8. overview = create(:overview, name: 'Not Shown Admin 2')
  9. expect(overview.link).to eq('not_shown_admin_2')
  10. end
  11. it 'ensures uniquenes' do
  12. overview1, overview2, overview3 = create_list(:overview, 3, name: 'Übersicht')
  13. expect(overview1.link).not_to eq(overview2.link)
  14. expect(overview1.link).not_to eq(overview3.link)
  15. expect(overview2.link).not_to eq(overview3.link)
  16. end
  17. context 'given link' do
  18. it 'keeps on create' do
  19. overview = create(:overview, name: 'Übersicht', link: 'my_overview')
  20. expect(overview.link).to eq('my_overview')
  21. end
  22. it 'keeps on update' do
  23. overview = create(:overview, name: 'Übersicht')
  24. overview.update!(link: 'my_overview_2')
  25. expect(overview.link).to eq('my_overview_2')
  26. end
  27. end
  28. context 'URL save' do
  29. it 'handles umlauts' do
  30. overview = create(:overview, name: 'Übersicht')
  31. expect(overview.link).to eq('ubersicht')
  32. end
  33. it 'handles spaces' do
  34. overview = create(:overview, name: " Meine Übersicht \n")
  35. expect(overview.link).to eq('meine_ubersicht')
  36. end
  37. it 'handles special chars' do
  38. overview = create(:overview, name: 'Д дФ ф')
  39. expect(overview.link).to match(%r{^[a-z0-9-]{36}$})
  40. end
  41. it 'removes special char fallback if possible' do
  42. overview = create(:overview, name: ' Д дФ ф abc ')
  43. expect(overview.link).to eq('abc')
  44. end
  45. end
  46. end
  47. describe '#rearrangement' do
  48. it 'rearranges prio of other overviews on prio change' do
  49. overview1 = create(:overview, prio: 1)
  50. overview2 = create(:overview, prio: 2)
  51. overview3 = create(:overview, prio: 3)
  52. overview2.update!(prio: 3)
  53. overviews = described_class.all.order(prio: :asc).pluck(:id)
  54. expect(overviews.first).to eq(overview1.id)
  55. expect(overviews.second).to eq(overview3.id)
  56. expect(overviews.third).to eq(overview2.id)
  57. end
  58. end
  59. describe '#fill_prio' do
  60. before do
  61. described_class.destroy_all
  62. end
  63. it 'fill an empty prio with the maximum prio plus one' do
  64. overview1 = create(:overview, prio: 1)
  65. overview2 = create(:overview, prio: 200)
  66. overview3 = create(:overview, prio: nil)
  67. overviews = described_class.all.order(prio: :asc).pluck(:id)
  68. expect(overviews).to eq [overview1.id, overview2.id, overview3.id]
  69. end
  70. it 'sets first Overview priority as 0' do
  71. overview = create :overview, prio: nil
  72. expect(overview.prio).to be 0
  73. end
  74. it 'sets new Overview priority as +1' do
  75. create :overview, prio: 123
  76. overview_next = create :overview, prio: nil
  77. expect(overview_next.prio).to be 124
  78. end
  79. end
  80. end