future_past_spec.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. require 'rails_helper'
  2. require 'models/object_manager/attribute/validation/backend_examples'
  3. RSpec.describe ::ObjectManager::Attribute::Validation::FuturePast do
  4. subject do
  5. described_class.new(
  6. record: record,
  7. attribute: attribute
  8. )
  9. end
  10. let(:record) { build(:user) }
  11. let(:attribute) { build(:object_manager_attribute_datetime) }
  12. it_behaves_like 'validate backend'
  13. shared_examples 'data_option validator' do |data_option:, value:|
  14. context "for data_option '#{data_option}'" do
  15. let(:value) { value }
  16. context 'when data_option is set to true' do
  17. before { attribute.data_option[data_option] = true }
  18. it_behaves_like 'a validation without errors'
  19. end
  20. context 'when data_option is set to false' do
  21. before { attribute.data_option[data_option] = false }
  22. it_behaves_like 'a validation with errors'
  23. end
  24. end
  25. end
  26. it_behaves_like 'data_option validator', data_option: :future, value: Time.current.tomorrow.midnight
  27. it_behaves_like 'data_option validator', data_option: :past, value: Time.current.yesterday.midnight
  28. context 'when validation should not be performed' do
  29. context 'for blank value' do
  30. let(:value) { nil }
  31. it_behaves_like 'a validation without errors'
  32. end
  33. context 'for irrelevant attribute data_type' do
  34. let(:value) { 'some_value' }
  35. before { attribute.data_type = 'select' }
  36. it_behaves_like 'a validation without errors'
  37. end
  38. end
  39. end