form_helpers_spec.rb 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Form helpers', app: :desktop_view, authenticated_as: :agent, db_strategy: :reset, type: :system do
  4. let(:group) { Group.find_by(name: 'Users') }
  5. let(:agent) { create(:agent, groups: [group]) }
  6. let(:object_name) { 'Ticket' }
  7. let(:screens) do
  8. {
  9. create_middle: {
  10. '-all-' => {
  11. shown: true,
  12. required: false,
  13. },
  14. },
  15. }
  16. end
  17. before do
  18. visit '/tickets/create'
  19. wait_for_form_to_settle('ticket-create')
  20. end
  21. context 'with single select field', authenticated_as: :authenticate do
  22. def authenticate
  23. create(:object_manager_attribute_select, object_name: object_name, name: 'single_select', display: 'Single Select', screens: screens, additional_data_options: { options: { '1' => 'Option 1', '2' => 'Option 2', '3' => 'Option 3' } })
  24. ObjectManager::Attribute.migration_execute
  25. agent
  26. end
  27. it 'provides test helpers' do
  28. el = find_select('Single Select')
  29. el.select_option('Option 1')
  30. expect(el).to have_selected_option('Option 1')
  31. el.clear_selection
  32. expect(el).to have_no_selected_option('Option 1')
  33. end
  34. end
  35. context 'with multi select field', authenticated_as: :authenticate do
  36. def authenticate
  37. create(:object_manager_attribute_multiselect, object_name: object_name, name: 'multi_select', display: 'Multi Select', screens: screens, additional_data_options: { options: { '1' => 'Option 1', '2' => 'Option 2', '3' => 'Option 3' } })
  38. ObjectManager::Attribute.migration_execute
  39. agent
  40. end
  41. it 'provides test helpers' do
  42. el = find_select('Multi Select')
  43. el.select_options(['Option 1', 'Option 2'])
  44. expect(el).to have_selected_options(['Option 1', 'Option 2'])
  45. el.clear_selection
  46. expect(el).to have_no_selected_options(['Option 1', 'Option 2'])
  47. end
  48. end
  49. context 'with tree select field', authenticated_as: :authenticate do
  50. let(:data_options) do
  51. {
  52. 'options' => [
  53. {
  54. 'name' => 'Parent 1',
  55. 'value' => '1',
  56. 'children' => [
  57. {
  58. 'name' => 'Option A',
  59. 'value' => '1::a',
  60. },
  61. {
  62. 'name' => 'Option B',
  63. 'value' => '1::b',
  64. },
  65. ],
  66. },
  67. {
  68. 'name' => 'Parent 2',
  69. 'value' => '2',
  70. 'children' => [
  71. {
  72. 'name' => 'Option C',
  73. 'value' => '2::c'
  74. },
  75. ],
  76. },
  77. {
  78. 'name' => 'Option 3',
  79. 'value' => '3'
  80. },
  81. ],
  82. 'default' => '',
  83. 'null' => true,
  84. 'relation' => '',
  85. 'maxlength' => 255,
  86. 'nulloption' => true,
  87. }
  88. end
  89. def authenticate
  90. create(:object_manager_attribute_tree_select, object_name: object_name, name: 'tree_select', display: 'Tree Select', screens: screens, additional_data_options: data_options)
  91. ObjectManager::Attribute.migration_execute
  92. agent
  93. end
  94. it 'provides test helpers' do
  95. el = find_treeselect('Tree Select')
  96. el.select_option('Parent 1::Option A')
  97. expect(el).to have_selected_option_with_parent('Parent 1::Option A')
  98. el.clear_selection
  99. expect(el).to have_no_selected_option_with_parent('Parent 1::Option A')
  100. el.search_for_option('Parent 2::Option C')
  101. expect(el).to have_selected_option_with_parent('Parent 2::Option C')
  102. el.clear_selection.search_for_option('Option C') # chained
  103. expect(el).to have_selected_option_with_parent('Parent 2::Option C')
  104. end
  105. end
  106. context 'with multi tree select field', authenticated_as: :authenticate do
  107. let(:data_options) do
  108. {
  109. 'options' => [
  110. {
  111. 'name' => 'Parent 1',
  112. 'value' => '1',
  113. 'children' => [
  114. {
  115. 'name' => 'Option A',
  116. 'value' => '1::a',
  117. },
  118. {
  119. 'name' => 'Option B',
  120. 'value' => '1::b',
  121. },
  122. ],
  123. },
  124. {
  125. 'name' => 'Parent 2',
  126. 'value' => '2',
  127. 'children' => [
  128. {
  129. 'name' => 'Option C',
  130. 'value' => '2::c'
  131. },
  132. ],
  133. },
  134. {
  135. 'name' => 'Option 3',
  136. 'value' => '3'
  137. },
  138. ],
  139. 'default' => '',
  140. 'null' => true,
  141. 'relation' => '',
  142. 'maxlength' => 255,
  143. 'nulloption' => true,
  144. }
  145. end
  146. def authenticate
  147. create(:object_manager_attribute_multi_tree_select, object_name: object_name, name: 'tree_select', display: 'Multi Tree Select', screens: screens, additional_data_options: data_options)
  148. ObjectManager::Attribute.migration_execute
  149. agent
  150. end
  151. it 'provides test helpers' do
  152. el = find_treeselect('Multi Tree Select')
  153. el.select_options(['Parent 1::Option A', 'Parent 2::Option C'])
  154. expect(el).to have_selected_options_with_parent(['Parent 1::Option A', 'Parent 2::Option C'])
  155. el.clear_selection
  156. expect(el).to have_no_selected_options_with_parent(['Parent 1::Option A', 'Parent 2::Option C'])
  157. end
  158. end
  159. context 'with customer and organization fields' do
  160. let(:organization) { create(:organization) }
  161. let(:secondary_organizations) { create_list(:organization, 5) }
  162. let!(:customer) { create(:customer, organization_id: organization.id, organization_ids: secondary_organizations.map(&:id)) }
  163. it 'provides test helpers' do
  164. el = find_autocomplete('Customer')
  165. el.search_for_option(customer.email, label: customer.fullname) # search for fullname does not work without ES
  166. expect(el).to have_selected_option(customer.fullname)
  167. el = find_autocomplete('Organization')
  168. el.select_option(secondary_organizations.last.name)
  169. expect(el).to have_selected_option(secondary_organizations.last.name)
  170. end
  171. end
  172. context 'with recipient field' do
  173. let(:email_address_1) { Faker::Internet.unique.email }
  174. let(:email_address_2) { Faker::Internet.unique.email }
  175. before do
  176. find('[role="tab"]', text: 'Send Email').click
  177. end
  178. it 'provides test helpers' do
  179. within_form(form_updater_gql_number: 2) do
  180. el = find_autocomplete('CC')
  181. el.search_for_options([email_address_1, email_address_2], use_action: true)
  182. expect(el).to have_selected_options([email_address_1, email_address_2])
  183. end
  184. end
  185. end
  186. context 'with tags field', authenticated_as: :authenticate do
  187. let(:tag_1) { Faker::Hacker.unique.noun }
  188. let(:tag_2) { Faker::Hacker.unique.noun }
  189. let(:tag_3) { Faker::Hacker.unique.noun }
  190. let(:tags) do
  191. [
  192. Tag::Item.lookup_by_name_and_create('foo'),
  193. ]
  194. end
  195. def authenticate
  196. tags
  197. agent
  198. end
  199. it 'provides test helpers' do
  200. within_form(form_updater_gql_number: 1) do
  201. el = find_autocomplete('Tags')
  202. el.select_option('foo').search_for_options([tag_1, tag_2, tag_3])
  203. expect(el).to have_selected_options(['foo', tag_1, tag_2, tag_3])
  204. end
  205. end
  206. end
  207. context 'with editor field' do
  208. let(:body) { Faker::Hacker.say_something_smart }
  209. it 'provides test helpers' do
  210. within_form(form_updater_gql_number: 1) do
  211. el = find_editor('Text')
  212. el.type(body)
  213. expect(el).to have_data_value(body)
  214. el.clear
  215. expect(el).to have_no_data_value(body)
  216. end
  217. end
  218. end
  219. context 'with date and datetime fields', authenticated_as: :authenticate, time_zone: 'Europe/London' do
  220. let(:date) { Date.parse('2022-09-07') }
  221. let(:datetime) { DateTime.parse('2023-09-07T08:00:00.000Z') }
  222. def authenticate
  223. create(:object_manager_attribute_date, object_name: object_name, name: 'date', display: 'Date', screens: screens)
  224. create(:object_manager_attribute_datetime, object_name: object_name, name: 'datetime', display: 'Date Time', screens: screens)
  225. ObjectManager::Attribute.migration_execute
  226. agent
  227. end
  228. it 'provides test helpers' do
  229. el = find_datepicker(nil, exact_text: 'Date')
  230. el.select_date(date)
  231. expect(el).to have_date(date)
  232. el.clear
  233. expect(el).to have_no_date(date)
  234. el.type_date(date)
  235. expect(el).to have_date(date)
  236. el = find_datepicker('Date Time')
  237. el.select_datetime(datetime)
  238. expect(el).to have_datetime(datetime)
  239. el.clear
  240. expect(el).to have_no_datetime(datetime)
  241. el.type_datetime(datetime)
  242. expect(el).to have_datetime(datetime)
  243. end
  244. end
  245. context 'with boolean field', authenticated_as: :authenticate do
  246. def authenticate
  247. create(:object_manager_attribute_boolean, object_name: object_name, name: 'boolean', display: 'Boolean', screens: screens)
  248. ObjectManager::Attribute.migration_execute
  249. agent
  250. end
  251. it 'provides test helpers' do
  252. el = find_toggle('Boolean')
  253. el.toggle
  254. expect(el).to be_toggled_on
  255. el.toggle_off
  256. expect(el).to be_toggled_off
  257. el.toggle_on
  258. expect(el).to be_toggled_on
  259. end
  260. end
  261. end