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.

151 lines
4.4 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 'matches ZWNJ' do
  50. expect(subject.match('just add #نرم‌افزار and').to_s).to eq ' #نرم‌افزار'
  51. end
  52. it 'does not match middle dots at the start' do
  53. expect(subject.match('hello #·one·two·three')).to be_nil
  54. end
  55. it 'does not match middle dots at the end' do
  56. expect(subject.match('hello #one·two·three·').to_s).to eq ' #one·two·three'
  57. end
  58. it 'does not match purely-numeric hashtags' do
  59. expect(subject.match('hello #0123456')).to be_nil
  60. end
  61. end
  62. describe '#to_param' do
  63. it 'returns name' do
  64. tag = Fabricate(:tag, name: 'foo')
  65. expect(tag.to_param).to eq 'foo'
  66. end
  67. end
  68. describe '.find_normalized' do
  69. it 'returns tag for a multibyte case-insensitive name' do
  70. upcase_string = 'abcABCabcABCやゆよ'
  71. downcase_string = 'abcabcabcabcやゆよ';
  72. tag = Fabricate(:tag, name: downcase_string)
  73. expect(Tag.find_normalized(upcase_string)).to eq tag
  74. end
  75. end
  76. describe '.matching_name' do
  77. it 'returns tags for multibyte case-insensitive names' do
  78. upcase_string = 'abcABCabcABCやゆよ'
  79. downcase_string = 'abcabcabcabcやゆよ';
  80. tag = Fabricate(:tag, name: downcase_string)
  81. expect(Tag.matching_name(upcase_string)).to eq [tag]
  82. end
  83. end
  84. describe '.find_or_create_by_names' do
  85. it 'runs a passed block once per tag regardless of duplicates' do
  86. upcase_string = 'abcABCabcABCやゆよ'
  87. downcase_string = 'abcabcabcabcやゆよ';
  88. count = 0
  89. Tag.find_or_create_by_names([upcase_string, downcase_string]) do |tag|
  90. count += 1
  91. end
  92. expect(count).to eq 1
  93. end
  94. end
  95. describe '.search_for' do
  96. it 'finds tag records with matching names' do
  97. tag = Fabricate(:tag, name: "match")
  98. _miss_tag = Fabricate(:tag, name: "miss")
  99. results = Tag.search_for("match")
  100. expect(results).to eq [tag]
  101. end
  102. it 'finds tag records in case insensitive' do
  103. tag = Fabricate(:tag, name: "MATCH")
  104. _miss_tag = Fabricate(:tag, name: "miss")
  105. results = Tag.search_for("match")
  106. expect(results).to eq [tag]
  107. end
  108. it 'finds the exact matching tag as the first item' do
  109. similar_tag = Fabricate(:tag, name: "matchlater", reviewed_at: Time.now.utc)
  110. tag = Fabricate(:tag, name: "match", reviewed_at: Time.now.utc)
  111. results = Tag.search_for("match")
  112. expect(results).to eq [tag, similar_tag]
  113. end
  114. end
  115. end