time_accounting_spec.rb 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Ticket::TimeAccounting, type: :model do
  4. subject(:time_accounting) { create(:'ticket/time_accounting') }
  5. describe 'Associations:' do
  6. describe '#ticket_article' do
  7. subject!(:time_accounting) { create(:'ticket/time_accounting', :for_article) }
  8. context 'when destroyed' do
  9. it 'destroys self' do
  10. expect { time_accounting.ticket_article.destroy }
  11. .to change(time_accounting, :persisted?).to(false)
  12. .and change(described_class, :count).by(-1)
  13. end
  14. it 'does not destroy other TimeAccountings for same ticket' do
  15. create(:'ticket/time_accounting', ticket: time_accounting.ticket)
  16. create(:'ticket/time_accounting', :for_article, ticket: time_accounting.ticket)
  17. expect { time_accounting.ticket_article.destroy }
  18. .to change(described_class, :count).by(-1)
  19. end
  20. end
  21. context 'when recalculating articles' do
  22. let(:ticket) { create(:ticket) }
  23. let(:article1) { create(:ticket_article, ticket: ticket) }
  24. let(:article2) { create(:ticket_article, ticket: ticket) }
  25. it 'one article' do
  26. time_accounting = create(:'ticket/time_accounting', ticket: ticket, ticket_article: article1)
  27. expect(ticket.reload.time_unit).to eq(time_accounting.time_unit)
  28. end
  29. it 'multiple article' do
  30. time_accounting1 = create(:'ticket/time_accounting', ticket: ticket, ticket_article: article1, time_unit: 5.5)
  31. time_accounting2 = create(:'ticket/time_accounting', ticket: ticket, ticket_article: article2, time_unit: 10.5)
  32. expect(ticket.reload.time_unit).to eq(time_accounting1.time_unit + time_accounting2.time_unit)
  33. end
  34. it 'destroy article' do
  35. time_accounting1 = create(:'ticket/time_accounting', ticket: ticket, ticket_article: article1, time_unit: 5.5)
  36. create(:'ticket/time_accounting', ticket: ticket, ticket_article: article2, time_unit: 10.5)
  37. article2.destroy
  38. expect(ticket.reload.time_unit).to eq(time_accounting1.time_unit)
  39. end
  40. it 'destroy all articles' do
  41. create(:'ticket/time_accounting', ticket: ticket, ticket_article: article1, time_unit: 5.5)
  42. create(:'ticket/time_accounting', ticket: ticket, ticket_article: article2, time_unit: 10.5)
  43. article1.destroy
  44. article2.destroy
  45. expect(ticket.reload.time_unit).to eq(0)
  46. end
  47. end
  48. end
  49. end
  50. end