You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.1 KiB

  1. require 'rails_helper'
  2. RSpec.describe Tag, type: :model do
  3. describe 'validations' do
  4. it 'invalid with #' do
  5. expect(Tag.new(name: '#hello_world')).to_not be_valid
  6. end
  7. it 'invalid with .' do
  8. expect(Tag.new(name: '.abcdef123')).to_not be_valid
  9. end
  10. it 'invalid with spaces' do
  11. expect(Tag.new(name: 'hello world')).to_not be_valid
  12. end
  13. it 'valid with aesthetic' do
  14. expect(Tag.new(name: 'aesthetic')).to be_valid
  15. end
  16. end
  17. describe 'HASHTAG_RE' do
  18. subject { Tag::HASHTAG_RE }
  19. it 'does not match URLs with anchors with non-hashtag characters' do
  20. expect(subject.match('Check this out https://medium.com/@alice/some-article#.abcdef123')).to be_nil
  21. end
  22. it 'does not match URLs with hashtag-like anchors' do
  23. expect(subject.match('https://en.wikipedia.org/wiki/Ghostbusters_(song)#Lawsuit')).to be_nil
  24. end
  25. it 'matches #aesthetic' do
  26. expect(subject.match('this is #aesthetic').to_s).to eq ' #aesthetic'
  27. end
  28. it 'matches digits at the start' do
  29. expect(subject.match('hello #3d').to_s).to eq ' #3d'
  30. end
  31. it 'matches digits in the middle' do
  32. expect(subject.match('hello #l33ts35k').to_s).to eq ' #l33ts35k'
  33. end
  34. it 'matches digits at the end' do
  35. expect(subject.match('hello #world2016').to_s).to eq ' #world2016'
  36. end
  37. it 'matches underscores at the beginning' do
  38. expect(subject.match('hello #_test').to_s).to eq ' #_test'
  39. end
  40. it 'matches underscores at the end' do
  41. expect(subject.match('hello #test_').to_s).to eq ' #test_'
  42. end
  43. it 'matches underscores in the middle' do
  44. expect(subject.match('hello #one_two_three').to_s).to eq ' #one_two_three'
  45. end
  46. it 'matches middle dots' do
  47. expect(subject.match('hello #one·two·three').to_s).to eq ' #one·two·three'
  48. end
  49. it 'does not match middle dots at the start' do
  50. expect(subject.match('hello #·one·two·three')).to be_nil
  51. end
  52. it 'does not match middle dots at the end' do
  53. expect(subject.match('hello #one·two·three·').to_s).to eq ' #one·two·three'
  54. end
  55. end
  56. describe '#to_param' do
  57. it 'returns name' do
  58. tag = Fabricate(:tag, name: 'foo')
  59. expect(tag.to_param).to eq 'foo'
  60. end
  61. end
  62. describe '.search_for' do
  63. it 'finds tag records with matching names' do
  64. tag = Fabricate(:tag, name: "match")
  65. _miss_tag = Fabricate(:tag, name: "miss")
  66. results = Tag.search_for("match")
  67. expect(results).to eq [tag]
  68. end
  69. it 'finds tag records in case insensitive' do
  70. tag = Fabricate(:tag, name: "MATCH")
  71. _miss_tag = Fabricate(:tag, name: "miss")
  72. results = Tag.search_for("match")
  73. expect(results).to eq [tag]
  74. end
  75. it 'finds the exact matching tag as the first item' do
  76. similar_tag = Fabricate(:tag, name: "matchlater")
  77. tag = Fabricate(:tag, name: "match")
  78. results = Tag.search_for("match")
  79. expect(results).to eq [tag, similar_tag]
  80. end
  81. end
  82. end