checklist_spec.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Checklist, :aggregate_failures, type: :model do
  4. describe 'validations' do
  5. let(:attributes) do
  6. {
  7. name: 'Checklist',
  8. ticket_id: 1,
  9. created_by_id: 1,
  10. updated_by_id: 1
  11. }
  12. end
  13. context 'when required attributes are missing' do
  14. {
  15. name: ActiveRecord::NotNullViolation,
  16. ticket_id: ActiveRecord::RecordInvalid,
  17. created_by_id: ActiveRecord::RecordInvalid,
  18. updated_by_id: ActiveRecord::RecordInvalid
  19. }.each do |attribute, error|
  20. it "fails validation with missing #{attribute}" do
  21. attributes.delete(attribute)
  22. expect { described_class.create!(attributes) }.to raise_error(error)
  23. end
  24. end
  25. end
  26. context 'when referenced ticket does not exist' do
  27. it 'fails validation with an error' do
  28. attributes[:ticket_id] = 2
  29. expect { described_class.create!(attributes) }.to raise_error(ActiveRecord::InvalidForeignKey)
  30. end
  31. end
  32. context 'with valid attributes' do
  33. it 'succeeds creation' do
  34. expect(described_class.create!(attributes)).to be_valid
  35. end
  36. end
  37. context 'when limits are reached' do
  38. it 'does not allow more than 100 items' do
  39. checklist = create(:checklist, item_count: 100)
  40. expect { checklist.items.create!(text: 'new', created_by_id: 1, updated_by_id: 1) }.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Checklist items are limited to 100 items per checklist.')
  41. end
  42. end
  43. end
  44. describe 'complete status' do
  45. let(:checklist) do
  46. list = create(:checklist, item_count: 3)
  47. list.items.first.update!(checked: true)
  48. list
  49. end
  50. context 'when any item is incomplete' do
  51. it '#completed? returns false' do
  52. expect(checklist.completed?).to be false
  53. end
  54. it '#incomplete returns the count of incomplete items' do
  55. expect(checklist.incomplete).to eq 2
  56. end
  57. end
  58. context 'when all items are complete' do
  59. before do
  60. checklist.items.each { |item| item.update!(checked: true) }
  61. end
  62. it '#completed? returns true' do
  63. expect(checklist.completed?).to be true
  64. end
  65. it '#incomplete returns the count of incomplete items' do
  66. expect(checklist.incomplete).to eq 0
  67. end
  68. end
  69. context 'when no items are present' do
  70. before do
  71. checklist.items.destroy_all
  72. end
  73. it '#completed? returns true' do
  74. expect(checklist.completed?).to be true
  75. end
  76. it '#incomplete returns the count of incomplete items' do
  77. expect(checklist.incomplete).to eq 0
  78. end
  79. end
  80. end
  81. end