fix(interviews): retain answers and live errors after failed saves

This commit is contained in:
cesnimda
2026-11-17 18:43:56 +01:00
parent 7cb6682699
commit 5aa204b677
2 changed files with 36 additions and 4 deletions
@@ -45,7 +45,7 @@ function Shell({ title, subtitle, loading, error, children, onRetry, keepContent
export function ApplicationInterviewPrep({ jobId }: { jobId: number }) {
const { t } = useI18n();
const [board, setBoard] = useState<InterviewPrepBoard | null>(null);
const [error, setError] = useState<string | { cause: unknown; fallback: "interviewPrepLoadFailed" | "interviewPrepUpdateFailed" } | null>(null);
const [error, setError] = useState<{ cause: unknown; fallback: "interviewPrepLoadFailed" | "interviewPrepUpdateFailed" | "interviewPrepAnswerSaveFailed" } | null>(null);
const [loading, setLoading] = useState(true);
const [busy, setBusy] = useState(false);
const [title, setTitle] = useState("");
@@ -101,7 +101,8 @@ export function ApplicationInterviewPrep({ jobId }: { jobId: number }) {
title={t("interviewPrepTitle")}
subtitle={t("interviewPrepSubtitle")}
loading={loading}
error={typeof error === "string" ? error : error ? getApiErrorMessage(error.cause, t(error.fallback)) : null}
error={error ? getApiErrorMessage(error.cause, t(error.fallback)) : null}
keepContentOnError={error?.fallback === "interviewPrepAnswerSaveFailed"}
>
<Stack spacing={2}>
{board && !board.isInterviewStage && (
@@ -317,7 +318,7 @@ function PrepRow({ jobId, item, busy, onChanged, onError }: {
item: InterviewPrepItem;
busy: boolean;
onChanged: () => void;
onError: (message: string) => void;
onError: (error: { cause: unknown; fallback: "interviewPrepAnswerSaveFailed" }) => void;
}) {
const { t } = useI18n();
const { confirm } = useConfirm();
@@ -333,7 +334,7 @@ function PrepRow({ jobId, item, busy, onChanged, onError }: {
setDraft(null);
onChanged();
} catch (err) {
onError(getApiErrorMessage(err, t("interviewPrepAnswerSaveFailed")));
onError({ cause: err, fallback: "interviewPrepAnswerSaveFailed" });
} finally {
setSaving(false);
}
@@ -137,6 +137,37 @@ describe.each(['en', 'nb'])('%s unknown interview categories', language => {
});
});
test.each((['en', 'nb'] as const).flatMap(from => (['pending', 'visible'] as const).map(timing => [from, timing] as const)))('answer save failure follows %s / %s and retains the draft for explicit retry', async (from, timing) => {
localStorage.setItem('uiLanguage', from);
const to = from === 'en' ? 'nb' : 'en';
mockedApi.get.mockReset(); routeGet();
mockedApi.patch.mockReset();
let reject!: (cause: unknown) => void;
const pending = new Promise((_, no) => { reject = no; });
mockedApi.patch.mockReturnValueOnce(pending as any).mockResolvedValueOnce({ data: { ...board.groups[1].items[0], content: 'My STAR answer Øresund' } } as any);
render(<ApplicationInterviewPrep jobId={7} />);
const answerName = (language: 'en' | 'nb') => translations[language].interviewPrepAnswerFor.replace('{title}', 'Tell me about a conflict');
fireEvent.change(await screen.findByRole('textbox', { name: answerName(from) }), { target: { value: 'My STAR answer Øresund' } });
fireEvent.click(screen.getByRole('button', { name: translations[from].interviewPrepSaveAnswer }));
await waitFor(() => expect(mockedApi.patch).toHaveBeenCalledTimes(1));
if (timing === 'pending') await act(async () => window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: to })));
await act(async () => reject(new Error('synthetic answer save failure')));
if (timing === 'visible') {
expect(await screen.findByText(translations[from].interviewPrepAnswerSaveFailed)).toBeVisible();
await act(async () => window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: to })));
}
expect(await screen.findByText(translations[to].interviewPrepAnswerSaveFailed)).toBeVisible();
expect(screen.getByRole('textbox', { name: answerName(to) })).toHaveValue('My STAR answer Øresund');
expect(mockedApi.get.mock.calls.filter(([url]) => String(url).endsWith('/interview-prep'))).toHaveLength(1);
expect(mockedApi.patch).toHaveBeenCalledTimes(1);
routeGet({ board: { ...board, groups: board.groups.map(group => ({ ...group, items: group.items.map(item => item.id === 2 ? { ...item, content: 'My STAR answer Øresund' } : item) })) } });
fireEvent.click(screen.getByRole('button', { name: translations[to].interviewPrepSaveAnswer }));
await waitFor(() => expect(mockedApi.patch).toHaveBeenCalledTimes(2));
await waitFor(() => expect(screen.queryByText(translations[to].interviewPrepAnswerSaveFailed)).not.toBeInTheDocument());
expect(await screen.findByRole('textbox', { name: answerName(to) })).toHaveValue('My STAR answer Øresund');
expect(mockedApi.patch.mock.calls).toEqual(Array(2).fill(['/jobapplications/7/interview-prep/2', { content: 'My STAR answer Øresund' }]));
});
test("localizes interview categories while preserving user-authored preparation", async () => {
window.localStorage.setItem("uiLanguage", "nb");
routeGet();