Support form with client validation and server error handling
Build the “Contact support” form of a help center. It checks the fields in the browser, sends the ticket to a JSON API, and handles every answer the server can give: success, field errors, and failures.
The starter code includes the API endpoint and the rules:
const SUBMIT_URL = 'https://api.example.com/support/tickets';
const TOPICS = ['Billing', 'Bug report', 'Feature request'];
const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const MIN_MESSAGE_LENGTH = 10;
Render a <form> with these labelled fields, in this order, and a Send message submit button:
<select> whose first option is a placeholder (Select a topic, value ""), followed by one option per entry in TOPICS. Each option’s value is the topic text.<textarea>.On submit, trim every field, then check all of them and show every error at once, next to its field:
Name is requiredEMAIL_PATTERN: Enter a valid emailChoose a topicMIN_MESSAGE_LENGTH characters: Message must be at least 10 charactersIf any field is invalid, don’t send anything. Each submit replaces the previous errors, so fixed fields lose their message.
POST SUBMIT_URL with the header Content-Type: application/json and the trimmed values as a JSON body: { "name", "email", "topic", "message" }, with no other keys.Sending…. Ignore any further submit (pressing Enter still submits a form whose button is disabled).{ "ticketId": "T-1042" }. Replace the whole form with Thanks, {name}! Your ticket number is {ticketId}.{ "errors": { "email": "This email is blocked" } }, with keys from name, email, topic, message. Show each message next to its field, the same way as the browser checks.fetch rejects): show Something went wrong. Please try again.Send message). The next submit clears the old messages.Submit the empty form all four errors, no request
Name " Grace Hopper ", Email "grace@navy.mil", Topic "Bug report",
Message "The export button does nothing."
Submit POST {"name":"Grace Hopper","email":"grace@navy.mil",
"topic":"Bug report","message":"The export button does nothing."}
button: "Sending…" (disabled)
Server: 201 {"ticketId":"T-1042"}
"Thanks, Grace Hopper! Your ticket number is T-1042."
Server: 422 {"errors":{"email":"This email is blocked"}}
"This email is blocked" next to Email, fields kept
Server: 500 "Something went wrong. Please try again."
" Fix login! " is exactly 10 characters.response.json() only when you need it. A broken 422 body counts as a failure.The tests replace window.fetch with a stub that records each call and answers with an object that has ok, status and json(). They type into your fields, click Send message, and read the screen like a user would. You don’t need test ids or specific class names. They check that:
aria-label, placeholder, or name) starting with Name, Email, Topic and Message. The topic is chosen by setting the select’s value and firing input and change.Content-Type header, and JSON body are as described, and fetch is called exactly once per valid submit.Capitalization and extra spacing don’t matter.