diff --git a/app/components/tag_list_input_component/tag_list_input_controller.js b/app/components/tag_list_input_component/tag_list_input_controller.js index 3d0ca779aa..f0e9e3555b 100644 --- a/app/components/tag_list_input_component/tag_list_input_controller.js +++ b/app/components/tag_list_input_component/tag_list_input_controller.js @@ -13,7 +13,11 @@ export default class extends Controller { addTag() { // Check if tag already exist - const newTagName = this.newTagTarget.value; + const newTagName = this.newTagTarget.value.trim(); + if (newTagName.length == 0) { + return; + } + const tags = this.tagListTarget.value.split(","); const index = tags.indexOf(newTagName); if (index != -1) { diff --git a/spec/javascripts/stimulus/tag_list_input_controller_test.js b/spec/javascripts/stimulus/tag_list_input_controller_test.js index 7ebfdadcb4..a0c9f89eb8 100644 --- a/spec/javascripts/stimulus/tag_list_input_controller_test.js +++ b/spec/javascripts/stimulus/tag_list_input_controller_test.js @@ -103,6 +103,18 @@ describe("TagListInputController", () => { expect(variant_add_tag.value).toBe(""); }); + describe("with an empty new tag", () => { + it("doesn't add the tag", () => { + variant_add_tag.value = " "; + variant_add_tag.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter" })); + + const tagList = document.getElementsByClassName("tag-list")[0]; + + // 1 template + 3 tags + new tag (added in the beforeEach) + expect(tagList.childElementCount).toBe(5); + }); + }); + describe("when tag already exist", () => { beforeEach(() => { // Trying to add an existing tag