diff --git a/app/javascript/shared/components/__tests__/tags.test.jsx b/app/javascript/shared/components/__tests__/tags.test.jsx index ed5cc530e..8668ec2d9 100644 --- a/app/javascript/shared/components/__tests__/tags.test.jsx +++ b/app/javascript/shared/components/__tests__/tags.test.jsx @@ -37,13 +37,13 @@ describe('', () => { test('calls preventDefault on unused keyCode', () => { tags.find('#tag-input').simulate('keydown', createKeyDown('ยง')); - tags.find('#tag-input').simulate('keydown', createKeyDown('1')); tags.find('#tag-input').simulate('keydown', createKeyDown('\\')); - expect(preventDefaultMock).toHaveBeenCalledTimes(3); + expect(preventDefaultMock).toHaveBeenCalledTimes(2); }); test('does not call preventDefault on used keyCode', () => { tags.find('#tag-input').simulate('keypress', createKeyDown('a')); + tags.find('#tag-input').simulate('keydown', createKeyDown('1')); tags.find('#tag-input').simulate('keypress', createKeyDown(',')); tags.find('#tag-input').simulate('keypress', createKeyDown('Enter')); expect(preventDefaultMock).not.toHaveBeenCalled(); diff --git a/app/javascript/shared/components/tags.jsx b/app/javascript/shared/components/tags.jsx index 298bcfd54..aa980653d 100644 --- a/app/javascript/shared/components/tags.jsx +++ b/app/javascript/shared/components/tags.jsx @@ -20,7 +20,7 @@ const NAVIGATION_KEYS = [ KEYS.TAB, ]; -const LETTERS = /[a-z]/i; +const LETTERS_NUMBERS = /[a-z0-9]/i; /* TODO: Remove all instances of this.props.listing and refactor this component to be more generic */ @@ -203,7 +203,7 @@ class Tags extends Component { ) { this.clearSelectedSearchResult(); } - } else if (!LETTERS.test(e.key) && !NAVIGATION_KEYS.includes(e.key)) { + } else if (!LETTERS_NUMBERS.test(e.key) && !NAVIGATION_KEYS.includes(e.key)) { e.preventDefault(); } }; diff --git a/app/models/tag.rb b/app/models/tag.rb index 88f0d0993..bcb6346bd 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -23,6 +23,8 @@ class Tag < ActsAsTaggableOn::Tag mount_uploader :profile_image, ProfileImageUploader mount_uploader :social_image, ProfileImageUploader + validates :name, + format: /\A[A-Za-z0-9\s]+\z/, allow_nil: true validates :text_color_hex, format: /\A#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\z/, allow_nil: true validates :bg_color_hex, diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb index 93d128e47..c4ac6b303 100644 --- a/spec/models/tag_spec.rb +++ b/spec/models/tag_spec.rb @@ -3,29 +3,49 @@ require "rails_helper" RSpec.describe Tag, type: :model do let(:tag) { build(:tag) } - it "passes validations if bg_color_hex is valid" do - tag.bg_color_hex = "#000000" - expect(tag).to be_valid - end + describe "validations" do - it "fails validation if bg_color_hex is invalid" do - tag.bg_color_hex = "0000000" - expect(tag).not_to be_valid - end + describe "bg_color_hex" do + it "passes validations if bg_color_hex is valid" do + tag.bg_color_hex = "#000000" + expect(tag).to be_valid + end + + it "fails validation if bg_color_hex is invalid" do + tag.bg_color_hex = "0000000" + expect(tag).not_to be_valid + end + end - it "passes validations if text_color_hex is valid" do - tag.text_color_hex = "#000000" - expect(tag).to be_valid - end + describe "text_color_hex" do + it "passes validations if text_color_hex is valid" do + tag.text_color_hex = "#000000" + expect(tag).to be_valid + end + + it "fails validation if text_color_hex is invalid" do + tag.text_color_hex = "0000000" + expect(tag).not_to be_valid + end + end - it "fails validation if text_color_hex is invalid" do - tag.text_color_hex = "0000000" - expect(tag).not_to be_valid - end + describe "name" do + it "passes validations if name is alphanumeric" do + tag.name = "foobar123" + expect(tag).to be_valid + end + + it "fails validations if name is not alphanumeric" do + tag.name = "" + expect(tag).not_to be_valid + end + end + + it "fails validation if the alias does not refer to an existing tag" do + tag.alias_for = "hello" + expect(tag).not_to be_valid + end - it "fails validation if the alias does not refer to an existing tag" do - tag.alias_for = "hello" - expect(tag).not_to be_valid end it "turns markdown into HTML before saving" do