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

th-downstream
Matt Jankowski 10 months ago committed by GitHub
parent b3dab17b58
commit 12bed81187
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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

@ -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…
Cancel
Save