Add validation specs to `CustomFilter` model (#28600)

This commit is contained in:
Matt Jankowski 2024-01-05 10:13:59 -05:00 committed by GitHub
parent b3dab17b58
commit 12bed81187
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 1 deletions

View File

@ -143,6 +143,10 @@ class CustomFilter < ApplicationRecord
end end
def context_must_be_valid 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
end end

View 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