fix(interviews): preserve preparation drafts across language changes
This commit is contained in:
@@ -43,7 +43,7 @@ function Shell({ title, subtitle, loading, error, children }: {
|
|||||||
export function ApplicationInterviewPrep({ jobId }: { jobId: number }) {
|
export function ApplicationInterviewPrep({ jobId }: { jobId: number }) {
|
||||||
const { t } = useI18n();
|
const { t } = useI18n();
|
||||||
const [board, setBoard] = useState<InterviewPrepBoard | null>(null);
|
const [board, setBoard] = useState<InterviewPrepBoard | null>(null);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | { cause: unknown; fallback: "interviewPrepLoadFailed" | "interviewPrepUpdateFailed" } | null>(null);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [busy, setBusy] = useState(false);
|
const [busy, setBusy] = useState(false);
|
||||||
const [title, setTitle] = useState("");
|
const [title, setTitle] = useState("");
|
||||||
@@ -55,11 +55,11 @@ export function ApplicationInterviewPrep({ jobId }: { jobId: number }) {
|
|||||||
setBoard(await interviewPrepApi.get(jobId));
|
setBoard(await interviewPrepApi.get(jobId));
|
||||||
setError(null);
|
setError(null);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(getApiErrorMessage(err, t("interviewPrepLoadFailed")));
|
setError({ cause: err, fallback: "interviewPrepLoadFailed" });
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
}, [jobId, t]);
|
}, [jobId]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
load();
|
load();
|
||||||
@@ -71,7 +71,7 @@ export function ApplicationInterviewPrep({ jobId }: { jobId: number }) {
|
|||||||
await run();
|
await run();
|
||||||
await load();
|
await load();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(getApiErrorMessage(err, t("interviewPrepUpdateFailed")));
|
setError({ cause: err, fallback: "interviewPrepUpdateFailed" });
|
||||||
} finally {
|
} finally {
|
||||||
setBusy(false);
|
setBusy(false);
|
||||||
}
|
}
|
||||||
@@ -99,7 +99,7 @@ export function ApplicationInterviewPrep({ jobId }: { jobId: number }) {
|
|||||||
title={t("interviewPrepTitle")}
|
title={t("interviewPrepTitle")}
|
||||||
subtitle={t("interviewPrepSubtitle")}
|
subtitle={t("interviewPrepSubtitle")}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
error={error}
|
error={typeof error === "string" ? error : error ? getApiErrorMessage(error.cause, t(error.fallback)) : null}
|
||||||
>
|
>
|
||||||
<Stack spacing={2}>
|
<Stack spacing={2}>
|
||||||
{board && !board.isInterviewStage && (
|
{board && !board.isInterviewStage && (
|
||||||
@@ -389,9 +389,9 @@ function PrepRow({ jobId, item, busy, onChanged, onError }: {
|
|||||||
aria-label={t("interviewPrepDeleteLabel", { title: item.title })}
|
aria-label={t("interviewPrepDeleteLabel", { title: item.title })}
|
||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
const accepted = await confirm({
|
const accepted = await confirm({
|
||||||
title: t("interviewPrepDeleteTitle"),
|
title: { key: "interviewPrepDeleteTitle" },
|
||||||
message: t("interviewPrepDeleteMessage", { title: item.title }),
|
message: { key: "interviewPrepDeleteMessage", params: { title: item.title } },
|
||||||
confirmLabel: t("interviewPrepDelete"),
|
confirmLabel: { key: "interviewPrepDelete" },
|
||||||
destructive: true,
|
destructive: true,
|
||||||
});
|
});
|
||||||
if (accepted) await run(() => interviewPrepApi.remove(jobId, item.id));
|
if (accepted) await run(() => interviewPrepApi.remove(jobId, item.id));
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import "@testing-library/jest-dom";
|
import "@testing-library/jest-dom";
|
||||||
import { fireEvent, render as rtlRender, screen, waitFor } from "@testing-library/react";
|
import { act, fireEvent, render as rtlRender, screen, waitFor, within } from "@testing-library/react";
|
||||||
|
|
||||||
import { ApplicationFollowUp, ApplicationInterviewPrep } from "./components/InterviewPrep";
|
import { ApplicationFollowUp, ApplicationInterviewPrep } from "./components/InterviewPrep";
|
||||||
import { api } from "./api";
|
import { api } from "./api";
|
||||||
import { I18nProvider } from "./i18n/I18nProvider";
|
import { I18nProvider } from "./i18n/I18nProvider";
|
||||||
import { ConfirmProvider } from "./confirm";
|
import { ConfirmProvider } from "./confirm";
|
||||||
|
import { translations } from './i18n/translations';
|
||||||
|
|
||||||
const render = (ui: React.ReactElement) => rtlRender(<I18nProvider><ConfirmProvider>{ui}</ConfirmProvider></I18nProvider>);
|
const render = (ui: React.ReactElement) => rtlRender(<I18nProvider><ConfirmProvider>{ui}</ConfirmProvider></I18nProvider>);
|
||||||
|
|
||||||
@@ -62,6 +63,70 @@ beforeEach(() => {
|
|||||||
window.localStorage.setItem("uiLanguage", "en");
|
window.localStorage.setItem("uiLanguage", "en");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test.each(['en', 'nb'] as const)('pending preparation deletion follows %s without a preparation reload or answer loss', async from => {
|
||||||
|
localStorage.setItem('uiLanguage', from);
|
||||||
|
const to = from === 'en' ? 'nb' : 'en';
|
||||||
|
mockedApi.get.mockReset(); routeGet();
|
||||||
|
mockedApi.delete.mockResolvedValue({ data: null } as any);
|
||||||
|
render(<ApplicationInterviewPrep jobId={7} />);
|
||||||
|
const opener = await screen.findByRole('button', { name: translations[from].interviewPrepDeleteLabel.replace('{title}', 'Funding history') });
|
||||||
|
const answerName = (language: 'en' | 'nb') => translations[language].interviewPrepAnswerFor.replace('{title}', 'Tell me about a conflict');
|
||||||
|
fireEvent.change(screen.getByRole('textbox', { name: answerName(from) }), { target: { value: 'My local STAR draft' } });
|
||||||
|
act(() => opener.focus()); fireEvent.click(opener);
|
||||||
|
const dialog = await screen.findByRole('dialog', { name: translations[from].interviewPrepDeleteTitle });
|
||||||
|
await act(async () => window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: to })));
|
||||||
|
expect(screen.getByRole('dialog', { name: translations[to].interviewPrepDeleteTitle })).toBe(dialog);
|
||||||
|
expect(dialog).toHaveAccessibleDescription(translations[to].interviewPrepDeleteMessage.replace('{title}', 'Funding history'));
|
||||||
|
expect(mockedApi.get.mock.calls.filter(([url]) => String(url).endsWith('/interview-prep'))).toHaveLength(1);
|
||||||
|
expect(mockedApi.delete).not.toHaveBeenCalled();
|
||||||
|
expect(mockedApi.patch).not.toHaveBeenCalled();
|
||||||
|
fireEvent.click(within(dialog).getByRole('button', { name: translations[to].cancel }));
|
||||||
|
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument());
|
||||||
|
await waitFor(() => expect(opener).toHaveFocus());
|
||||||
|
expect(screen.getByRole('textbox', { name: answerName(to) })).toHaveValue('My local STAR draft');
|
||||||
|
fireEvent.click(opener);
|
||||||
|
fireEvent.click(within(await screen.findByRole('dialog')).getByRole('button', { name: translations[to].interviewPrepDelete }));
|
||||||
|
await waitFor(() => expect(mockedApi.delete).toHaveBeenCalledTimes(1));
|
||||||
|
expect(mockedApi.delete).toHaveBeenCalledWith('/jobapplications/7/interview-prep/1');
|
||||||
|
expect(mockedApi.patch).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test.each(['en', 'nb'] as const)('visible preparation read error follows %s without another preparation request', async from => {
|
||||||
|
localStorage.setItem('uiLanguage', from);
|
||||||
|
const to = from === 'en' ? 'nb' : 'en';
|
||||||
|
mockedApi.get.mockReset(); routeGet();
|
||||||
|
const read = mockedApi.get.getMockImplementation()!;
|
||||||
|
mockedApi.get.mockImplementation((url: string) => url.endsWith('/interview-prep') ? Promise.reject(new Error('synthetic preparation read failure')) : read(url));
|
||||||
|
render(<ApplicationInterviewPrep jobId={7} />);
|
||||||
|
expect(await screen.findByText(translations[from].interviewPrepLoadFailed)).toBeVisible();
|
||||||
|
await act(async () => window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: to })));
|
||||||
|
expect(await screen.findByText(translations[to].interviewPrepLoadFailed)).toBeVisible();
|
||||||
|
expect(mockedApi.get.mock.calls.filter(([url]) => String(url).endsWith('/interview-prep'))).toHaveLength(1);
|
||||||
|
expect(mockedApi.post).not.toHaveBeenCalled();
|
||||||
|
expect(mockedApi.patch).not.toHaveBeenCalled();
|
||||||
|
expect(mockedApi.delete).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test.each(['en', 'nb'] as const)('visible preparation mutation error follows %s without clearing the failure through a reload', async from => {
|
||||||
|
localStorage.setItem('uiLanguage', from);
|
||||||
|
const to = from === 'en' ? 'nb' : 'en';
|
||||||
|
mockedApi.get.mockReset(); routeGet();
|
||||||
|
mockedApi.post.mockReset().mockRejectedValue(new Error('synthetic preparation write failure'));
|
||||||
|
try {
|
||||||
|
render(<ApplicationInterviewPrep jobId={7} />);
|
||||||
|
await screen.findByText('Funding history');
|
||||||
|
fireEvent.change(screen.getByLabelText(translations[from].interviewPrepAddLabel), { target: { value: 'Research the role' } });
|
||||||
|
fireEvent.click(screen.getByRole('button', { name: translations[from].interviewPrepAdd }));
|
||||||
|
expect(await screen.findByText(translations[from].interviewPrepUpdateFailed)).toBeVisible();
|
||||||
|
await act(async () => window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: to })));
|
||||||
|
expect(await screen.findByText(translations[to].interviewPrepUpdateFailed)).toBeVisible();
|
||||||
|
expect(mockedApi.get.mock.calls.filter(([url]) => String(url).endsWith('/interview-prep'))).toHaveLength(1);
|
||||||
|
expect(mockedApi.post).toHaveBeenCalledTimes(1);
|
||||||
|
expect(mockedApi.patch).not.toHaveBeenCalled();
|
||||||
|
expect(mockedApi.delete).not.toHaveBeenCalled();
|
||||||
|
} finally { mockedApi.post.mockReset(); }
|
||||||
|
});
|
||||||
|
|
||||||
describe.each(['en', 'nb'])('%s unknown interview categories', language => {
|
describe.each(['en', 'nb'])('%s unknown interview categories', language => {
|
||||||
test.each(['constructor', 'toString', '__proto__'])('preserves %s as a category label', async category => {
|
test.each(['constructor', 'toString', '__proto__'])('preserves %s as a category label', async category => {
|
||||||
window.localStorage.setItem('uiLanguage', language);
|
window.localStorage.setItem('uiLanguage', language);
|
||||||
|
|||||||
Reference in New Issue
Block a user