123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- require 'rails_helper'
- RSpec.describe Validations::OutOfOfficeValidator do
- subject(:validator) { described_class.new }
- let(:record) { create(:agent, :ooo, ooo_agent: other_agent) }
- let(:other_agent) { create(:user) }
- before { record.out_of_office = out_of_office }
- context 'when out of office is set to true' do
- let(:out_of_office) { true }
- it 'requires start date' do
- record.out_of_office_start_at = nil
- validator.validate(record)
- expect(record.errors).to be_added(:out_of_office_start_at, :blank)
- end
- it 'requires end date' do
- record.out_of_office_end_at = nil
- validator.validate(record)
- expect(record.errors).to be_added(:out_of_office_end_at, :blank)
- end
- it 'requires logical date range' do
- record.out_of_office_start_at = Date.parse('2011-03-03')
- record.out_of_office_end_at = Date.parse('2011-02-03')
- validator.validate(record)
- expect(record.errors.full_messages).to include(%r{has to be earlier than or equal to})
- end
- it 'allows single day range' do
- record.out_of_office_start_at = Date.parse('2011-03-03')
- record.out_of_office_end_at = Date.parse('2011-03-03')
- validator.validate(record)
- expect(record.errors).to be_blank
- end
- it 'requires replacement agent' do
- record.out_of_office_replacement_id = nil
- validator.validate(record)
- expect(record.errors).to be_added(:out_of_office_replacement_id, :blank)
- end
- it 'requires valid replacement agent' do
- record.out_of_office_replacement_id = User.maximum(:id).next
- validator.validate(record)
- expect(record.errors).to be_added(:out_of_office_replacement_id, :invalid)
- end
- end
- context 'when out of office is set to false' do
- let(:out_of_office) { false }
- it 'does not require start date' do
- record.out_of_office_start_at = nil
- validator.validate(record)
- expect(record.errors).to be_blank
- end
- it 'does not require end date' do
- record.out_of_office_end_at = nil
- validator.validate(record)
- expect(record.errors).to be_blank
- end
- it 'does not check date range' do
- record.out_of_office_start_at = Date.parse('2011-02-03')
- record.out_of_office_end_at = Date.parse('2011-03-03')
- validator.validate(record)
- expect(record.errors).to be_blank
- end
- it 'does not require replacement agent' do
- record.out_of_office_replacement_id = nil
- validator.validate(record)
- expect(record.errors).to be_blank
- end
- it 'allows to set dates and replacement agent' do
- validator.validate(record)
- expect(record.errors).to be_blank
- end
- it 'prevents assigning non existant user' do
- record.out_of_office_replacement_id = User.maximum(:id).next
- validator.validate(record)
- expect(record.errors).to be_added(:out_of_office_replacement_id, :invalid)
- end
- end
- end
|