form_configurable_agent_presenter_spec.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. require 'rails_helper'
  2. describe FormConfigurableAgentPresenter do
  3. include RSpecHtmlMatchers
  4. class FormConfigurableAgentPresenterAgent < Agent
  5. include FormConfigurable
  6. form_configurable :string, roles: :validatable
  7. form_configurable :number, type: :number, html_options: { min: 0 }
  8. form_configurable :text, type: :text, roles: :completable
  9. form_configurable :boolean, type: :boolean
  10. form_configurable :array, type: :array, values: [1, 2, 3]
  11. form_configurable :json, type: :json
  12. end
  13. before(:all) do
  14. @presenter = FormConfigurableAgentPresenter.new(FormConfigurableAgentPresenterAgent.new,
  15. ActionController::Base.new.view_context)
  16. end
  17. it "works for the type :string" do
  18. expect(@presenter.option_field_for(:string)).to(
  19. have_tag(
  20. 'input',
  21. with: {
  22. 'data-attribute': 'string',
  23. role: 'validatable form-configurable',
  24. type: 'text',
  25. name: 'agent[options][string]'
  26. }
  27. )
  28. )
  29. end
  30. it "works for the type :number" do
  31. expect(@presenter.option_field_for(:number)).to(
  32. have_tag(
  33. 'input',
  34. with: {
  35. 'data-attribute': 'number',
  36. role: 'form-configurable',
  37. type: 'number',
  38. name: 'agent[options][number]',
  39. min: '0',
  40. }
  41. )
  42. )
  43. end
  44. it "works for the type :text" do
  45. expect(@presenter.option_field_for(:text)).to(
  46. have_tag(
  47. 'textarea',
  48. with: {
  49. 'data-attribute': 'text',
  50. role: 'completable form-configurable',
  51. name: 'agent[options][text]'
  52. }
  53. )
  54. )
  55. end
  56. it "works for the type :boolean" do
  57. expect(@presenter.option_field_for(:boolean)).to(
  58. have_tag(
  59. 'input',
  60. with: {
  61. 'data-attribute': 'boolean',
  62. role: 'form-configurable',
  63. name: 'agent[options][boolean_radio]',
  64. type: 'radio'
  65. }
  66. )
  67. )
  68. end
  69. it "works for the type :array" do
  70. expect(@presenter.option_field_for(:array)).to(
  71. have_tag(
  72. 'select',
  73. with: {
  74. 'data-attribute': 'array',
  75. role: 'completable form-configurable',
  76. name: 'agent[options][array]'
  77. }
  78. )
  79. )
  80. end
  81. it "works for the type :json" do
  82. expect(@presenter.option_field_for(:json)).to(
  83. have_tag(
  84. 'textarea',
  85. with: {
  86. 'data-attribute': 'json',
  87. role: 'form-configurable',
  88. name: 'agent[options][json]',
  89. class: 'live-json-editor',
  90. }
  91. )
  92. )
  93. end
  94. end