Add validation specs to CustomFilter
model (#28600)
This commit is contained in:
parent
b3dab17b58
commit
12bed81187
2 changed files with 40 additions and 1 deletions
|
@ -143,6 +143,10 @@ class CustomFilter < ApplicationRecord
|
|||
end
|
||||
|
||||
def context_must_be_valid
|
||||
errors.add(:context, I18n.t('filters.errors.invalid_context')) if context.empty? || context.any? { |c| !VALID_CONTEXTS.include?(c) }
|
||||
errors.add(:context, I18n.t('filters.errors.invalid_context')) if invalid_context_value?
|
||||
end
|
||||
|
||||
def invalid_context_value?
|
||||
context.blank? || context.difference(VALID_CONTEXTS).any?
|
||||
end
|
||||
end
|
||||
|
|
35
spec/models/custom_filter_spec.rb
Normal file
35
spec/models/custom_filter_spec.rb
Normal file
|
@ -0,0 +1,35 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe CustomFilter do
|
||||
describe 'Validations' do
|
||||
it 'requires presence of title' do
|
||||
record = described_class.new(title: '')
|
||||
record.valid?
|
||||
|
||||
expect(record).to model_have_error_on_field(:title)
|
||||
end
|
||||
|
||||
it 'requires presence of context' do
|
||||
record = described_class.new(context: nil)
|
||||
record.valid?
|
||||
|
||||
expect(record).to model_have_error_on_field(:context)
|
||||
end
|
||||
|
||||
it 'requires non-empty of context' do
|
||||
record = described_class.new(context: [])
|
||||
record.valid?
|
||||
|
||||
expect(record).to model_have_error_on_field(:context)
|
||||
end
|
||||
|
||||
it 'requires valid context value' do
|
||||
record = described_class.new(context: ['invalid'])
|
||||
record.valid?
|
||||
|
||||
expect(record).to model_have_error_on_field(:context)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue