Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions frontend/src/__tests__/pages/records-request-checklist.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { StrictMode, type ReactNode } from 'react';
import { render, screen } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';

import { RecordsRequestChecklistClient } from '@/app/tools/records-request-checklist/components/RecordsRequestChecklistClient';

vi.mock('@/app/actions/submit-assessment', () => ({ submitAssessment: vi.fn() }));

// Exercise the results render path without entering any assessment or lead data.
vi.mock('@/components/fsma/FSMAToolShell', () => ({
FSMAToolShell: ({ renderResults }: { renderResults: (answers: Record<string, unknown>) => ReactNode }) => (
<>{renderResults({})}</>
Comment thread
PetrefiedThunder marked this conversation as resolved.
Resolved
),
}));

vi.mock('framer-motion', () => ({
useReducedMotion: () => true,
motion: {
div: ({ children }: { children?: ReactNode }) => <div>{children}</div>,
},
}));

describe('records request checklist results', () => {
it('renders and rerenders results without render-time state updates', () => {
const errors = vi.spyOn(console, 'error').mockImplementation((message) => {
throw new Error(String(message));
});
try {
const view = render(<StrictMode><RecordsRequestChecklistClient /></StrictMode>);
expect(screen.getByRole('heading', { name: 'What You Reported' })).toBeInTheDocument();
expect(() => view.rerender(<StrictMode><RecordsRequestChecklistClient /></StrictMode>)).not.toThrow();
expect(errors).not.toHaveBeenCalled();
} finally {
errors.mockRestore();
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ export function RecordsRequestChecklistClient() {
const [leadSubmitted, setLeadSubmitted] = useState(false);
const [submitting, setSubmitting] = useState(false);
const [submitError, setSubmitError] = useState('');
const [lastScore, setLastScore] = useState<{ score: number; grade: string }>({ score: 0, grade: 'F' });
const [lastAnswers, setLastAnswers] = useState<Record<string, any>>({});

function calcScore(answers: Record<string, any>) {
let totalScore = 0;
Expand All @@ -152,10 +150,11 @@ export function RecordsRequestChecklistClient() {
return { score: totalScore, grade };
}

const handleLeadSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
const handleLeadSubmit = async (e: React.FormEvent<HTMLFormElement>, answers: Record<string, any>) => {
e.preventDefault();
setSubmitting(true);
setSubmitError('');
const { score, grade } = calcScore(answers);
const formData = new FormData(e.currentTarget);
const attribution = getConversionAttribution();
const data: AssessmentFormData = {
Expand All @@ -170,9 +169,9 @@ export function RecordsRequestChecklistClient() {
utmCampaign: attribution.utm_campaign,
referrer: getReferrerHostname(),
toolInputs: {
score: lastScore.score,
grade: lastScore.grade,
answers: lastAnswers
score,
grade,
answers
}
};
try {
Expand All @@ -196,9 +195,7 @@ export function RecordsRequestChecklistClient() {
};

const renderResults = (answers: Record<string, any>) => {
const { score, grade } = calcScore(answers);
setLastScore({ score, grade });
setLastAnswers(answers);
const { score } = calcScore(answers);

return (
<motion.div
Expand Down Expand Up @@ -284,7 +281,7 @@ export function RecordsRequestChecklistClient() {
<div className="border-t border-re-border-subtle pt-8">
<div>
<h3 className="mb-4 text-center text-subheading text-re-text-primary">Want to Discuss Your Checklist?</h3>
<form onSubmit={handleLeadSubmit} className="mx-auto max-w-md space-y-4">
<form onSubmit={(e) => handleLeadSubmit(e, answers)} className="mx-auto max-w-md space-y-4">
<div>
<label htmlFor="rrc-name" className="sr-only">Full Name</label>
<Input
Expand Down
Loading