2021-03-03 15:12:26 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe NoteLengthValidator do
|
2023-06-06 04:58:33 -07:00
|
|
|
subject { described_class.new(attributes: { note: true }, maximum: 500) }
|
2021-03-03 15:12:26 -08:00
|
|
|
|
|
|
|
describe '#validate' do
|
2024-07-22 01:02:31 -07:00
|
|
|
it 'adds an error when text is over configured character limit' do
|
2021-03-03 15:12:26 -08:00
|
|
|
text = 'a' * 520
|
2023-06-22 05:55:22 -07:00
|
|
|
account = instance_double(Account, note: text, errors: activemodel_errors)
|
2021-03-03 15:12:26 -08:00
|
|
|
|
|
|
|
subject.validate_each(account, 'note', text)
|
|
|
|
expect(account.errors).to have_received(:add)
|
|
|
|
end
|
|
|
|
|
2024-07-22 01:02:31 -07:00
|
|
|
it 'reduces calculated length of auto-linkable space-separated URLs' do
|
|
|
|
text = [starting_string, example_link].join(' ')
|
2023-06-22 05:55:22 -07:00
|
|
|
account = instance_double(Account, note: text, errors: activemodel_errors)
|
2021-03-03 15:12:26 -08:00
|
|
|
|
|
|
|
subject.validate_each(account, 'note', text)
|
|
|
|
expect(account.errors).to_not have_received(:add)
|
|
|
|
end
|
|
|
|
|
2024-07-22 01:02:31 -07:00
|
|
|
it 'does not reduce calculated length of non-autolinkable URLs' do
|
|
|
|
text = [starting_string, example_link].join
|
2023-06-22 05:55:22 -07:00
|
|
|
account = instance_double(Account, note: text, errors: activemodel_errors)
|
2021-03-03 15:12:26 -08:00
|
|
|
|
|
|
|
subject.validate_each(account, 'note', text)
|
|
|
|
expect(account.errors).to have_received(:add)
|
|
|
|
end
|
2023-06-22 05:55:22 -07:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-07-22 01:02:31 -07:00
|
|
|
def starting_string
|
|
|
|
'a' * 476
|
|
|
|
end
|
|
|
|
|
|
|
|
def example_link
|
|
|
|
"http://#{'b' * 30}.com/example"
|
|
|
|
end
|
|
|
|
|
2023-06-22 05:55:22 -07:00
|
|
|
def activemodel_errors
|
|
|
|
instance_double(ActiveModel::Errors, add: nil)
|
|
|
|
end
|
2021-03-03 15:12:26 -08:00
|
|
|
end
|
|
|
|
end
|