future_past_spec.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. before do
  13. allow(subject).to receive(:value).and_return(value)
  14. end
  15. shared_examples 'data_option validator' do |data_option:, value:|
  16. context "for data_option '#{data_option}'" do
  17. let(:value) { value }
  18. context 'when data_option is set to true' do
  19. before { attribute.data_option[data_option] = true }
  20. it_behaves_like 'a validation without errors'
  21. end
  22. context 'when data_option is set to false' do
  23. before { attribute.data_option[data_option] = false }
  24. it_behaves_like 'a validation with errors'
  25. end
  26. end
  27. end
  28. it_behaves_like 'data_option validator', data_option: :future, value: Time.current.tomorrow.midnight
  29. it_behaves_like 'data_option validator', data_option: :past, value: Time.current.yesterday.midnight
  30. context 'when validation should not be performed' do
  31. context 'for blank value' do
  32. let(:value) { nil }
  33. it_behaves_like 'a validation without errors'
  34. end
  35. context 'for irrelevant attribute data_type' do
  36. let(:value) { 'some_value' }
  37. before { attribute.data_type = 'select' }
  38. it_behaves_like 'a validation without errors'
  39. end
  40. end
  41. end