Skip to content

Commit

Permalink
Form adds the number of milliseconds it took to fill it out when form…
Browse files Browse the repository at this point in the history
… is submitted
  • Loading branch information
lortimer committed Jan 26, 2024
1 parent 2c0d375 commit c97caee
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/components/form/Form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ import { FormField } from "./FormField";
import { Validator } from "../../hooks/form-validation/useValidation";
import { RadioButtonGroup, RadioOption } from "./RadioButtonGroup";
import { USER_EVENT_KEYS_FOR_TESTING_ONLY } from "../../../test/user-event-keys";
import { now } from "../../utilities/date-utilities";
import mocked = jest.mocked;

const submitEndpoint = "form-submit-endpoint";
const formLabelText = "Short information about the form";

const successMessage = "Your issue has been reported, thank you!";
const pageIdentifier = "test-page";

jest.mock("../../utilities/date-utilities");

describe(Form.name, () => {
let capturedFormData: AccessibilityFormData;
let user: UserEvent;
Expand Down Expand Up @@ -141,7 +145,14 @@ describe(Form.name, () => {
let honeypotInput: HTMLInputElement;
const honeypotValue = "I'm a robot";

const timeFormWasRendered = 42892453216246;
const timeFormWasSubmitted = 42892453641411;

beforeEach(async () => {
mocked(now)
.mockName("now")
.mockReturnValueOnce(timeFormWasRendered)
.mockReturnValueOnce(timeFormWasSubmitted);
render(<FormWithInputs/>);

form = screen.getByRole("form");
Expand Down Expand Up @@ -197,7 +208,13 @@ describe(Form.name, () => {
});

test("the form's data is sent to the server", async () => {
expect(capturedFormData).toEqual({ name, age, color, honeypotValue });
expect(capturedFormData).toEqual({
name,
age,
color,
honeypotValue,
timeToFillForm: `${timeFormWasSubmitted - timeFormWasRendered} milliseconds`
});
});

describe("when form submission succeeds", () => {
Expand Down
5 changes: 4 additions & 1 deletion src/components/form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { FormProvider } from "../../contexts/FormContext";
import { Observable } from "../../utilities/observable";
import { SubmitLoadingIcon } from "./SubmitLoadingIcon";
import { useLiveRegionText } from "../../hooks/useLiveRegionText";
import { now } from "../../utilities/date-utilities";

type FormProps = ChildrenProps & {
ariaLabelledBy: string
Expand All @@ -23,6 +24,7 @@ export const Form = ({ children, ariaLabelledBy, submitEndpoint, successMessage
const [showSuccessMessage, setShowSuccessMessage] = useState<boolean>(false);
const [errorMessage, setErrorMessage] = useState<string>("");
const [liveRegionText, setLiveRegionText] = useLiveRegionText("");
const [timeBeganFillingForm, setTimeBeganFillingForm] = useState(now());

const postFormData = usePOST(submitEndpoint);

Expand Down Expand Up @@ -55,6 +57,7 @@ export const Form = ({ children, ariaLabelledBy, submitEndpoint, successMessage
setLiveRegionText("submitting");

const formData = extractFormData(form);
formData.timeToFillForm = `${now() - timeBeganFillingForm} milliseconds`;

postFormData(formData, (ok, _, error) => {
setSubmitting(false);
Expand All @@ -68,7 +71,7 @@ export const Form = ({ children, ariaLabelledBy, submitEndpoint, successMessage
}
});
}
}, [onSubmitObservable, setLiveRegionText, postFormData, onClearObservable]);
}, [onSubmitObservable, setLiveRegionText, postFormData, onClearObservable, timeBeganFillingForm]);

return (<div className={"form-container"}>
{showSuccessMessage &&
Expand Down
12 changes: 12 additions & 0 deletions src/utilities/date-utilities.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { now } from "./date-utilities";

describe("date utilities", () => {
test("now returns current time in milliseconds since the epoch", () => {
const timeFromDate = Date.now();
const time = now();
const fiveSeconds = 5000;

expect(time).toBeGreaterThanOrEqual(timeFromDate);
expect(time).toBeLessThanOrEqual(timeFromDate + fiveSeconds);
});
});
1 change: 1 addition & 0 deletions src/utilities/date-utilities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const now = (): number => Date.now();

0 comments on commit c97caee

Please sign in to comment.