The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

148 lines
4.2 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. it 'does not match purely-numeric hashtags' do
  56. expect(subject.match('hello #0123456')).to be_nil
  57. end
  58. end
  59. describe '#to_param' do
  60. it 'returns name' do
  61. tag = Fabricate(:tag, name: 'foo')
  62. expect(tag.to_param).to eq 'foo'
  63. end
  64. end
  65. describe '.find_normalized' do
  66. it 'returns tag for a multibyte case-insensitive name' do
  67. upcase_string = 'abcABCabcABCやゆよ'
  68. downcase_string = 'abcabcabcabcやゆよ';
  69. tag = Fabricate(:tag, name: downcase_string)
  70. expect(Tag.find_normalized(upcase_string)).to eq tag
  71. end
  72. end
  73. describe '.matching_name' do
  74. it 'returns tags for multibyte case-insensitive names' do
  75. upcase_string = 'abcABCabcABCやゆよ'
  76. downcase_string = 'abcabcabcabcやゆよ';
  77. tag = Fabricate(:tag, name: downcase_string)
  78. expect(Tag.matching_name(upcase_string)).to eq [tag]
  79. end
  80. end
  81. describe '.find_or_create_by_names' do
  82. it 'runs a passed block once per tag regardless of duplicates' do
  83. upcase_string = 'abcABCabcABCやゆよ'
  84. downcase_string = 'abcabcabcabcやゆよ';
  85. count = 0
  86. Tag.find_or_create_by_names([upcase_string, downcase_string]) do |tag|
  87. count += 1
  88. end
  89. expect(count).to eq 1
  90. end
  91. end
  92. describe '.search_for' do
  93. it 'finds tag records with matching names' do
  94. tag = Fabricate(:tag, name: "match")
  95. _miss_tag = Fabricate(:tag, name: "miss")
  96. results = Tag.search_for("match")
  97. expect(results).to eq [tag]
  98. end
  99. it 'finds tag records in case insensitive' do
  100. tag = Fabricate(:tag, name: "MATCH")
  101. _miss_tag = Fabricate(:tag, name: "miss")
  102. results = Tag.search_for("match")
  103. expect(results).to eq [tag]
  104. end
  105. it 'finds the exact matching tag as the first item' do
  106. similar_tag = Fabricate(:tag, name: "matchlater")
  107. tag = Fabricate(:tag, name: "match")
  108. results = Tag.search_for("match")
  109. expect(results).to eq [tag, similar_tag]
  110. end
  111. end
  112. end