fix(attachments): discard reads from departed application contexts
This commit is contained in:
@@ -46,6 +46,34 @@ async function open(action: "rename" | "delete") {
|
||||
if (action === "rename") fireEvent.change(within(dialog).getByRole("textbox"), { target: { value: "New name.pdf" } });
|
||||
return { view, dialog };
|
||||
}
|
||||
|
||||
test.each(['resolve', 'reject'] as const)('old attachment read %s cannot replace the next application', async outcome => {
|
||||
const pending = deferred();
|
||||
const current = { ...file, id: 77, fileName: 'Current application.pdf' };
|
||||
mockedApi.get.mockReturnValueOnce(pending.promise).mockResolvedValue({ data: [current] } as any);
|
||||
const view = render(page());
|
||||
await act(async () => { view.rerender(page(77)); });
|
||||
expect(await screen.findByText(current.fileName)).toBeVisible();
|
||||
await act(async () => { if (outcome === 'resolve') pending.resolve({ data: [file] }); else pending.reject(new Error('obsolete initial read')); });
|
||||
expect(screen.getByText(current.fileName)).toBeVisible();
|
||||
expect(screen.queryByText(file.fileName)).not.toBeInTheDocument();
|
||||
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
test.each((['rename', 'delete'] as const).flatMap(action => (['resolve', 'reject'] as const).map(outcome => [action, outcome] as const)))('old post-%s read %s cannot replace the next application', async (action, outcome) => {
|
||||
const { view, dialog } = await open(action);
|
||||
const pending = deferred();
|
||||
const current = { ...file, id: 77, fileName: 'Current application.pdf' };
|
||||
mockedApi.get.mockImplementation((url: any) => url === '/attachments/42' ? pending.promise : Promise.resolve({ data: [current] }));
|
||||
fireEvent.click(within(dialog).getByRole('button', { name: action === 'rename' ? 'Rename' : 'Delete' }));
|
||||
await waitFor(() => expect(mockedApi.get.mock.calls.filter(([url]) => url === '/attachments/42')).toHaveLength(2));
|
||||
await act(async () => { view.rerender(page(77)); });
|
||||
expect(await screen.findByText(current.fileName)).toBeVisible();
|
||||
await act(async () => { if (outcome === 'resolve') pending.resolve({ data: [file] }); else pending.reject(new Error('obsolete refresh read')); });
|
||||
expect(screen.getByText(current.fileName)).toBeVisible();
|
||||
expect(screen.queryByText(file.fileName)).not.toBeInTheDocument();
|
||||
expect(screen.queryByText(translations.en.attachmentsLoadFailed)).not.toBeInTheDocument();
|
||||
});
|
||||
test.each([
|
||||
["rename", "unmount"], ["rename", "job change"], ["rename", "session"],
|
||||
["delete", "unmount"], ["delete", "job change"], ["delete", "session"],
|
||||
|
||||
@@ -110,7 +110,7 @@ export default function Attachments({ jobId }: { jobId: number }) {
|
||||
return () => { owner.mounted = false; owner.generation += 1; };
|
||||
}, [jobId]);
|
||||
|
||||
const captureAction = () => {
|
||||
const captureAction = useCallback(() => {
|
||||
checkSessionCookie();
|
||||
const owner = actionOwner.current;
|
||||
if (!owner.mounted || owner.jobId !== jobId || isSessionInvalidated()) return null;
|
||||
@@ -120,20 +120,23 @@ export default function Attachments({ jobId }: { jobId: number }) {
|
||||
checkSessionCookie();
|
||||
return owner.mounted && owner.jobId === jobId && owner.generation === generation && !isSessionInvalidated() && getSessionContext() === context;
|
||||
};
|
||||
};
|
||||
}, [jobId]);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
const active = captureAction();
|
||||
if (!active) return;
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await api.get<AttachmentItem[]>(`/attachments/${jobId}`);
|
||||
if (!active()) return;
|
||||
setItems(res.data);
|
||||
setLoadError(null);
|
||||
} catch (error) {
|
||||
setLoadError(getApiErrorMessage(error, currentT.current("attachmentsLoadFailed")));
|
||||
if (active()) setLoadError(getApiErrorMessage(error, currentT.current("attachmentsLoadFailed")));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
if (active()) setLoading(false);
|
||||
}
|
||||
}, [jobId]);
|
||||
}, [jobId, captureAction]);
|
||||
|
||||
useEffect(() => {
|
||||
void load();
|
||||
|
||||
Reference in New Issue
Block a user