form_helpers_spec.rb 9.4 KB

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