note_length_validator_spec.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe NoteLengthValidator do
  4. subject { described_class.new(attributes: { note: true }, maximum: 500) }
  5. describe '#validate' do
  6. it 'adds an error when text is over configured character limit' do
  7. text = 'a' * 520
  8. account = instance_double(Account, note: text, errors: activemodel_errors)
  9. subject.validate_each(account, 'note', text)
  10. expect(account.errors).to have_received(:add)
  11. end
  12. it 'reduces calculated length of auto-linkable space-separated URLs' do
  13. text = [starting_string, example_link].join(' ')
  14. account = instance_double(Account, note: text, errors: activemodel_errors)
  15. subject.validate_each(account, 'note', text)
  16. expect(account.errors).to_not have_received(:add)
  17. end
  18. it 'does not reduce calculated length of non-autolinkable URLs' do
  19. text = [starting_string, example_link].join
  20. account = instance_double(Account, note: text, errors: activemodel_errors)
  21. subject.validate_each(account, 'note', text)
  22. expect(account.errors).to have_received(:add)
  23. end
  24. private
  25. def starting_string
  26. 'a' * 476
  27. end
  28. def example_link
  29. "http://#{'b' * 30}.com/example"
  30. end
  31. def activemodel_errors
  32. instance_double(ActiveModel::Errors, add: nil)
  33. end
  34. end
  35. end