mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-10 23:07:47 +00:00
Prevent adding empty tag
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user