small validation fixes

This commit is contained in:
counterweight 2025-12-19 10:52:47 +01:00
parent bbc5625b2d
commit ead8a566d0
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
6 changed files with 201 additions and 102 deletions

View file

@ -305,84 +305,7 @@ describe("ProfilePage - Form Behavior", () => {
});
});
test("shows inline error for invalid telegram handle", async () => {
vi.spyOn(global, "fetch").mockResolvedValue({
ok: true,
json: () => Promise.resolve({
contact_email: null,
telegram: null,
signal: null,
nostr_npub: null,
}),
} as Response);
render(<ProfilePage />);
await waitFor(() => {
expect(screen.getByRole("heading", { name: "My Profile" })).toBeDefined();
});
const telegramInput = document.getElementById("telegram") as HTMLInputElement;
fireEvent.change(telegramInput, { target: { value: "noatsign" } });
await waitFor(() => {
expect(screen.getByText(/must start with @/i)).toBeDefined();
});
});
test("shows inline error for invalid npub", async () => {
vi.spyOn(global, "fetch").mockResolvedValue({
ok: true,
json: () => Promise.resolve({
contact_email: null,
telegram: null,
signal: null,
nostr_npub: null,
}),
} as Response);
render(<ProfilePage />);
await waitFor(() => {
expect(screen.getByRole("heading", { name: "My Profile" })).toBeDefined();
});
const npubInput = document.getElementById("nostr_npub") as HTMLInputElement;
fireEvent.change(npubInput, { target: { value: "invalidnpub" } });
await waitFor(() => {
expect(screen.getByText(/must start with 'npub1'/i)).toBeDefined();
});
});
test("submit button is disabled when form has validation errors", async () => {
vi.spyOn(global, "fetch").mockResolvedValue({
ok: true,
json: () => Promise.resolve({
contact_email: null,
telegram: null,
signal: null,
nostr_npub: null,
}),
} as Response);
render(<ProfilePage />);
await waitFor(() => {
expect(screen.getByRole("heading", { name: "My Profile" })).toBeDefined();
});
// Enter invalid telegram (no @)
const telegramInput = document.getElementById("telegram") as HTMLInputElement;
fireEvent.change(telegramInput, { target: { value: "noatsign" } });
await waitFor(() => {
const submitButton = screen.getByRole("button", { name: /save changes/i });
expect(submitButton).toHaveProperty("disabled", true);
});
});
test("clears error when field becomes valid", async () => {
test("auto-prepends @ to telegram when user starts with letter", async () => {
vi.spyOn(global, "fetch").mockResolvedValue({
ok: true,
json: () => Promise.resolve({
@ -401,17 +324,35 @@ describe("ProfilePage - Form Behavior", () => {
const telegramInput = document.getElementById("telegram") as HTMLInputElement;
// First enter invalid value
fireEvent.change(telegramInput, { target: { value: "noat" } });
// Type a letter without @ - should auto-prepend @
fireEvent.change(telegramInput, { target: { value: "myhandle" } });
expect(telegramInput.value).toBe("@myhandle");
});
test("does not auto-prepend @ if user types @ first", async () => {
vi.spyOn(global, "fetch").mockResolvedValue({
ok: true,
json: () => Promise.resolve({
contact_email: null,
telegram: null,
signal: null,
nostr_npub: null,
}),
} as Response);
render(<ProfilePage />);
await waitFor(() => {
expect(screen.getByText(/must start with @/i)).toBeDefined();
expect(screen.getByRole("heading", { name: "My Profile" })).toBeDefined();
});
// Then fix it
fireEvent.change(telegramInput, { target: { value: "@validhandle" } });
await waitFor(() => {
expect(screen.queryByText(/must start with @/i)).toBeNull();
});
const telegramInput = document.getElementById("telegram") as HTMLInputElement;
// User types @ first - no auto-prepend
fireEvent.change(telegramInput, { target: { value: "@myhandle" } });
expect(telegramInput.value).toBe("@myhandle");
});
});