docbrown/spec/validators/emoji_only_validator_spec.rb
Daniel Uber 4b93684dab
Don't raise error when empty community emoji setting submitted (#15723)
* Don't raise NoMethodError when validating emoji settings

Cargo culted the unique cross model slug validator setup (which is why
value is called name here in the validatable).

Don't raise an error when validating a nil emoji, and assert nil and
empty string are permitted values.

This expects no non-emoji characters (so an empty string would be
permitted), and nil should be permitted.

I believe this might have been introduced by the string settings
cleaner (exchanging "" for nil in form inputs).

* Update app/validators/emoji_only_validator.rb

guard for value, and then validate value
2021-12-17 09:29:55 -06:00

60 lines
1.2 KiB
Ruby

require "rails_helper"
RSpec.describe EmojiOnlyValidator do
subject(:record) { validatable.new.tap { |m| m.name = name } }
let(:validatable) do
Class.new do
def self.name
"👍"
end
include ActiveModel::Validations
attr_accessor :name
def initialize
@enforce_validation = true
end
attr_accessor :enforce_validation
alias_method :enforce_validation?, :enforce_validation
validates :name, emoji_only: true, if: :enforce_validation?
end
end
context "when if option is false" do
let(:name) { "not-an-emoji" }
before { record.enforce_validation = false }
it { is_expected.to be_valid }
end
context "when name includes non-emoji" do
let(:name) { "not-an-emoji" }
it { is_expected.not_to be_valid }
end
context "when name is an emoji" do
let(:name) { "🍀" }
it { is_expected.to be_valid }
end
context "when name is nil" do
let(:name) { nil }
it "does not raise no method error" do
expect { record.valid? }.not_to raise_error
end
it { is_expected.to be_valid }
end
context "when name is empty" do
let(:name) { "" }
it { is_expected.to be_valid }
end
end