-
Notifications
You must be signed in to change notification settings - Fork 630
Expand file tree
/
Copy pathadmin-form-validation.test.js
More file actions
164 lines (144 loc) · 5.41 KB
/
admin-form-validation.test.js
File metadata and controls
164 lines (144 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Unit tests for setupFormValidation() name-field selection logic.
*
* Verifies that only technical name inputs receive blur validation,
* and that displayName / hidden name fields are correctly excluded.
*/
import {
describe,
test,
expect,
beforeAll,
beforeEach,
afterAll,
} from "vitest";
import { loadAdminJs, cleanupAdminJs } from "./helpers/admin-env.js";
let win;
let doc;
beforeAll(() => {
win = loadAdminJs();
win.MAX_NAME_LENGTH = 200;
doc = win.document;
});
afterAll(() => {
cleanupAdminJs();
});
beforeEach(() => {
doc.body.textContent = "";
});
/**
* Build a minimal form with the given input elements,
* call setupFormValidation(), and return the form.
*/
function buildForm(inputs) {
const form = doc.createElement("form");
inputs.forEach(({ name, type, value, id }) => {
const wrapper = doc.createElement("div");
const input = doc.createElement("input");
input.name = name;
if (type) input.type = type;
if (value !== undefined) input.value = value;
if (id) input.id = id;
wrapper.appendChild(input);
form.appendChild(wrapper);
});
doc.body.appendChild(form);
win.setupFormValidation();
return form;
}
/** Dispatch a blur event on the given element. */
function blur(el) {
el.dispatchEvent(new win.Event("blur"));
}
// ---------------------------------------------------------------------------
// setupFormValidation — name field selection
// ---------------------------------------------------------------------------
describe("setupFormValidation name-field selection", () => {
test("validates visible input[name='name']", () => {
const form = buildForm([{ name: "name", type: "text", value: "" }]);
const input = form.querySelector('input[name="name"]');
blur(input);
// Empty name triggers validation error
expect(input.validity.customError).toBe(true);
});
test("validates input[name='customName']", () => {
const form = buildForm([
{ name: "customName", type: "text", value: "" },
]);
const input = form.querySelector('input[name="customName"]');
blur(input);
expect(input.validity.customError).toBe(true);
});
test("does NOT validate input[name='displayName']", () => {
const form = buildForm([
{ name: "displayName", type: "text", value: "" },
]);
const input = form.querySelector('input[name="displayName"]');
blur(input);
// displayName is excluded — no custom validity set
expect(input.validity.customError).toBe(false);
});
test("does NOT validate input[name='display_name']", () => {
const form = buildForm([
{ name: "display_name", type: "text", value: "" },
]);
const input = form.querySelector('input[name="display_name"]');
blur(input);
expect(input.validity.customError).toBe(false);
});
test("does NOT validate hidden input[name='name']", () => {
const form = buildForm([{ name: "name", type: "hidden", value: "" }]);
const input = form.querySelector('input[name="name"]');
blur(input);
expect(input.validity.customError).toBe(false);
});
test("validates valid name and clears error styling", () => {
const form = buildForm([
{
name: "name",
type: "text",
value: "my-tool",
id: "tool-name",
},
]);
const input = form.querySelector('input[name="name"]');
blur(input);
expect(input.validity.customError).toBe(false);
expect(input.classList.contains("border-red-500")).toBe(false);
});
test("shows error styling on invalid name", () => {
const form = buildForm([{ name: "name", type: "text", value: "" }]);
const input = form.querySelector('input[name="name"]');
blur(input);
expect(input.classList.contains("border-red-500")).toBe(true);
});
test("form with both name and displayName only validates name", () => {
const form = buildForm([
{ name: "name", type: "text", value: "" },
{ name: "displayName", type: "text", value: "" },
]);
const nameInput = form.querySelector('input[name="name"]');
const displayInput = form.querySelector('input[name="displayName"]');
blur(nameInput);
blur(displayInput);
// Only the technical name field gets validation
expect(nameInput.validity.customError).toBe(true);
expect(displayInput.validity.customError).toBe(false);
});
test("edit form: hidden name excluded, customName validated", () => {
const form = buildForm([
{ name: "name", type: "hidden", value: "original-name" },
{ name: "customName", type: "text", value: "" },
{ name: "displayName", type: "text", value: "" },
]);
const hiddenName = form.querySelector('input[name="name"]');
const customName = form.querySelector('input[name="customName"]');
const displayName = form.querySelector('input[name="displayName"]');
blur(hiddenName);
blur(customName);
blur(displayName);
expect(hiddenName.validity.customError).toBe(false);
expect(customName.validity.customError).toBe(true);
expect(displayName.validity.customError).toBe(false);
});
});