applies_taskbar_state_examples.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'FormUpdater::AppliesTaskbarState' do |taskbar_key:, taskbar_callback:, apply_state_group_keys:|
  3. context 'when applying taskbar state' do
  4. let(:taskbar) { create(:taskbar, key: taskbar_key, callback: taskbar_callback, user_id: user.id, state: taskbar_state) }
  5. let(:taskbar_state) { { 'title' => 'test' } }
  6. let(:field_name) { 'title' }
  7. let(:field_result) { { value: 'test' } }
  8. let(:additional_data) { { 'taskbarId' => Gql::ZammadSchema.id_from_object(taskbar), 'applyTaskbarState' => true } }
  9. let(:meta) { { additional_data: } }
  10. shared_examples 'skips the field' do
  11. it 'skips the field' do
  12. expect(resolved_result.resolve[:fields][field_name]).not_to have_key(:value)
  13. end
  14. end
  15. shared_examples 'applies the form value of the field' do
  16. it 'applies the form value of the field' do
  17. expect(resolved_result.resolve[:fields][field_name]).to include(field_result)
  18. end
  19. end
  20. context 'without an associated taskbar' do
  21. let(:additional_data) { {} }
  22. include_examples 'skips the field'
  23. end
  24. context 'with an associated taskbar' do
  25. context 'with simple field' do
  26. include_examples 'applies the form value of the field'
  27. end
  28. context 'with formSenderType field' do
  29. let(:taskbar_state) { { 'formSenderType' => 'phone-in' } }
  30. let(:field_name) { 'articleSenderType' }
  31. let(:field_result) { { value: 'phone-in' } }
  32. include_examples 'applies the form value of the field'
  33. end
  34. context 'with cc field' do
  35. let(:taskbar_state) { { 'cc' => 'recipient1@example.org, recipient2@example.org' } }
  36. let(:field_name) { 'cc' }
  37. let(:field_result) { { value: %w[recipient1@example.org recipient2@example.org] } }
  38. include_examples 'applies the form value of the field'
  39. end
  40. context 'with tags field' do
  41. let(:taskbar_state) { { 'tags' => 'tag1, tag2, tag3' } }
  42. let(:field_name) { 'tags' }
  43. let(:field_result) { { value: %w[tag1 tag2 tag3] } }
  44. include_examples 'applies the form value of the field'
  45. end
  46. context 'with blank field' do
  47. let(:taskbar_state) { { 'title' => '' } }
  48. let(:field_name) { 'title' }
  49. let(:field_result) { { value: '' } }
  50. include_examples 'applies the form value of the field'
  51. end
  52. context 'with group which should be flatten' do
  53. let(:taskbar_state) do
  54. flat_state = { 'priority_id' => 1 }
  55. if apply_state_group_keys.present?
  56. return { apply_state_group_keys.first => flat_state }
  57. end
  58. flat_state
  59. end
  60. let(:field_name) { 'priority_id' }
  61. let(:field_result) { { value: 1 } }
  62. include_examples 'applies the form value of the field'
  63. end
  64. context 'with formId + attachments field' do
  65. let(:form_id) { SecureRandom.uuid }
  66. let(:taskbar_state) do
  67. if taskbar_key.start_with?('TicketCreate')
  68. {
  69. 'form_id' => form_id,
  70. }
  71. elsif taskbar_key.start_with?('TicketZoom')
  72. {
  73. 'ticket' => {},
  74. 'article' => {
  75. 'form_id' => form_id,
  76. },
  77. }
  78. end
  79. end
  80. let(:field_name) { 'attachments' }
  81. let(:field_result) do
  82. {
  83. value: [
  84. {
  85. id: Gql::ZammadSchema.id_from_object(Store.last),
  86. name: 'some_file.pdf',
  87. size: '12',
  88. type: 'application/pdf',
  89. }
  90. ]
  91. }
  92. end
  93. before do
  94. create(:store,
  95. object: 'UploadCache',
  96. o_id: form_id,
  97. data: 'dGVzdCAxMjM=',
  98. filename: 'some_file.pdf',
  99. preferences: {
  100. 'Content-Type': 'application/pdf',
  101. 'Content-ID': 'application/pdf@01CAB192.K8H512Y9',
  102. })
  103. end
  104. include_examples 'applies the form value of the field'
  105. end
  106. end
  107. end
  108. end