overview_spec.rb 2.9 KB

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