2021-03-03 23:12:26 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2024-09-04 05:12:25 +00:00
|
|
|
RSpec.describe NoteLengthValidator do
|
2023-06-06 11:58:33 +00:00
|
|
|
subject { described_class.new(attributes: { note: true }, maximum: 500) }
|
2021-03-03 23:12:26 +00:00
|
|
|
|
|
|
|
describe '#validate' do
|
2024-07-22 08:02:31 +00: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 12:55:22 +00: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 08:02:31 +00:00
|
|
|
it 'reduces calculated length of auto-linkable space-separated URLs' do
|
|
|
|
text = [starting_string, example_link].join(' ')
|
2023-06-22 12:55:22 +00: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 08:02:31 +00:00
|
|
|
it 'does not reduce calculated length of non-autolinkable URLs' do
|
|
|
|
text = [starting_string, example_link].join
|
2023-06-22 12:55:22 +00: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 12:55:22 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-07-22 08:02:31 +00:00
|
|
|
def starting_string
|
|
|
|
'a' * 476
|
|
|
|
end
|
|
|
|
|
|
|
|
def example_link
|
|
|
|
"http://#{'b' * 30}.com/example"
|
|
|
|
end
|
|
|
|
|
2023-06-22 12:55:22 +00:00
|
|
|
def activemodel_errors
|
|
|
|
instance_double(ActiveModel::Errors, add: nil)
|
|
|
|
end
|
2021-03-03 23:12:26 +00:00
|
|
|
end
|
|
|
|
end
|