From 387c305a2f94728a34472cfd8da2af969eb47e6a Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 18 Aug 2023 18:48:30 -0400 Subject: [PATCH] test(frontend): downloads through FileDetails --- frontend/src/components/FileDetails.tsx | 15 ++++++----- frontend/tests/FileDetails.test.tsx | 35 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/frontend/src/components/FileDetails.tsx b/frontend/src/components/FileDetails.tsx index c4c414a..81d0098 100644 --- a/frontend/src/components/FileDetails.tsx +++ b/frontend/src/components/FileDetails.tsx @@ -8,7 +8,11 @@ import MuiDownloadIcon from "@mui/icons-material/Download" import MuiIconButton from "@mui/material/IconButton" import { useLocationContext } from "../contexts/LocationContext" -import { useFileDetails, useFileMutations } from "../queries/files" +import { + useFileDetails, + useFileMutations, + useFileFetches, +} from "../queries/files" interface FileDetailsProps { itemId: string @@ -18,13 +22,10 @@ function FileDetails({ itemId }: FileDetailsProps) { const { isLoading, data } = useFileDetails(itemId) const { deleteFile } = useFileMutations() const { navigate } = useLocationContext() + const { downloadFile } = useFileFetches() if (isLoading || !data) return null - const handleDownloadClick = () => { - console.log("download click") - } - const currentFileDetails = data return ( @@ -45,7 +46,9 @@ function FileDetails({ itemId }: FileDetailsProps) { <> handleDownloadClick()} + onClick={async () => { + await downloadFile(itemId, currentFileDetails.filename) + }} > diff --git a/frontend/tests/FileDetails.test.tsx b/frontend/tests/FileDetails.test.tsx index 5c24480..077164d 100644 --- a/frontend/tests/FileDetails.test.tsx +++ b/frontend/tests/FileDetails.test.tsx @@ -16,6 +16,41 @@ describe("FileDetails", () => { size: 1, id: "b61bf93d-a9db-473e-822e-a65003b1b7e3", } + test("Clicking the download button trigger a file download", async () => { + // FIXME: Validating file downloads is ... tricky. The current interaction with dynamically created DOM + // elements is not visible by jest. + + const expectedUrlPattern = new RegExp(`/files/${mockItem.id}/content/$`) + + const axiosMock = getAxiosMockAdapter() + + axiosMock.onGet(expectedUrlPattern).reply(200, mockItem) + + jest + .spyOn(fileQueries, "useFileDetails") + .mockReturnValue({ data: mockItem, isLoading: false } as UseQueryResult< + FileData, + unknown + >) + const user = userEvent.setup() + + const { getByLabelText, debug, rerender } = render( + , + ) + + const downloadButton = getByLabelText("download item") + + await user.click(downloadButton) + + const downloadRequests = axiosMock.history.get + + expect(downloadRequests.length).toEqual(1) + + const downloadRequest = downloadRequests[0] + + expect(downloadRequest.url).toMatch(expectedUrlPattern) + }) + test("Clicking the delete button fires request to delete file", async () => { const expectedUrlPattern = new RegExp(`/files/${mockItem.id}/$`)