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