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