mirror of
https://github.com/glitch-soc/mastodon.git
synced 2026-03-29 03:00:33 +02:00
Use validation matchers for NoteLengthValidator spec (#37891)
This commit is contained in:
@@ -3,61 +3,61 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe NoteLengthValidator do
|
||||
subject { described_class.new(attributes: { note: true }, maximum: 500) }
|
||||
subject { record_class.new }
|
||||
|
||||
describe '#validate' do
|
||||
it 'adds an error when text is over configured character limit' do
|
||||
text = 'a' * 520
|
||||
account = instance_double(Account, note: text, errors: activemodel_errors)
|
||||
let(:record_class) do
|
||||
Class.new do
|
||||
include ActiveModel::Validations
|
||||
|
||||
subject.validate_each(account, 'note', text)
|
||||
expect(account.errors).to have_received(:add)
|
||||
end
|
||||
def self.name = 'Record'
|
||||
|
||||
it 'reduces calculated length of auto-linkable space-separated URLs' do
|
||||
text = [starting_string, example_link].join(' ')
|
||||
account = instance_double(Account, note: text, errors: activemodel_errors)
|
||||
attr_accessor :note
|
||||
|
||||
subject.validate_each(account, 'note', text)
|
||||
expect(account.errors).to_not have_received(:add)
|
||||
end
|
||||
|
||||
it 'does not reduce calculated length of non-autolinkable URLs' do
|
||||
text = [starting_string, example_link].join
|
||||
account = instance_double(Account, note: text, errors: activemodel_errors)
|
||||
|
||||
subject.validate_each(account, 'note', text)
|
||||
expect(account.errors).to have_received(:add)
|
||||
end
|
||||
|
||||
it 'counts multi byte emoji as single character' do
|
||||
text = '✨' * 500
|
||||
account = instance_double(Account, note: text, errors: activemodel_errors)
|
||||
|
||||
subject.validate_each(account, 'note', text)
|
||||
expect(account.errors).to_not have_received(:add)
|
||||
end
|
||||
|
||||
it 'counts ZWJ sequence emoji as single character' do
|
||||
text = '🏳️⚧️' * 500
|
||||
account = instance_double(Account, note: text, errors: activemodel_errors)
|
||||
|
||||
subject.validate_each(account, 'note', text)
|
||||
expect(account.errors).to_not have_received(:add)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def starting_string
|
||||
'a' * 476
|
||||
end
|
||||
|
||||
def example_link
|
||||
"http://#{'b' * 30}.com/example"
|
||||
end
|
||||
|
||||
def activemodel_errors
|
||||
instance_double(ActiveModel::Errors, add: nil)
|
||||
validates :note, note_length: { maximum: 100 }
|
||||
end
|
||||
end
|
||||
|
||||
context 'when note is too long' do
|
||||
let(:too_long) { 'a' * 200 }
|
||||
|
||||
it { is_expected.to_not allow_value(too_long).for(:note).with_message(too_long_message) }
|
||||
end
|
||||
|
||||
context 'when note has space separated linkable URLs' do
|
||||
let(:text) { [starting_string, example_link].join(' ') }
|
||||
|
||||
it { is_expected.to allow_value(text).for(:note) }
|
||||
end
|
||||
|
||||
context 'when note has non-separated URLs' do
|
||||
let(:text) { [starting_string, example_link].join }
|
||||
|
||||
it { is_expected.to_not allow_value(text).for(:note).with_message(too_long_message) }
|
||||
end
|
||||
|
||||
context 'with multi-byte emoji' do
|
||||
let(:text) { '✨' * 100 }
|
||||
|
||||
it { is_expected.to allow_value(text).for(:note) }
|
||||
end
|
||||
|
||||
context 'with ZWJ sequence emoji' do
|
||||
let(:text) { '🏳️⚧️' * 100 }
|
||||
|
||||
it { is_expected.to allow_value(text).for(:note) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def too_long_message
|
||||
I18n.t('statuses.over_character_limit', max: 100)
|
||||
end
|
||||
|
||||
def starting_string
|
||||
'a' * 76
|
||||
end
|
||||
|
||||
def example_link
|
||||
"http://#{'b' * 30}.com/example"
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user