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