Skip to content

Commit

Permalink
Check expected arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
eyeseast committed Apr 16, 2024
1 parent 4d98251 commit f772b1e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
54 changes: 40 additions & 14 deletions src/lib/api/documents.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import type { Document, DocumentUpload, Sizes } from "./types";

import {
vi,
test as base,
describe,
it,
expect,
beforeEach,
afterEach,
} from "vitest";
import { APP_URL, IMAGE_WIDTHS_ENTRIES } from "@/config/config.js";
import { vi, test as base, describe, expect, afterEach } from "vitest";
import { APP_URL, DC_BASE, IMAGE_WIDTHS_ENTRIES } from "@/config/config.js";

import * as documents from "./documents";

Expand Down Expand Up @@ -61,14 +53,28 @@ describe("document uploads and processing", () => {
documents.create(docs, "token", mockFetch).then((d) => {
expect(d).toMatchObject(created);
});

expect(mockFetch).toHaveBeenCalledWith(
new URL("/api/documents/", DC_BASE),
{
body: JSON.stringify(docs),
credentials: "include",
headers: {
"Content-type": "application/json",
Referer: APP_URL,
"X-CSRFToken": "token",
},
method: "POST",
},
);
});

test("documents.upload", async ({ created }) => {
const mockFetch = vi.fn().mockImplementation(async () => ({
ok: true,
}));

created.forEach(async (doc) => {
const mockFetch = vi.fn().mockImplementation(async () => ({
ok: true,
}));

const file = new File(
["test content"],
"finalseasonal-allergies-pollen-and-mold-2023-en.pdf",
Expand All @@ -82,6 +88,13 @@ describe("document uploads and processing", () => {
);

expect(resp.ok).toBeTruthy();
expect(mockFetch).toHaveBeenCalledWith(new URL(doc.presigned_url), {
body: file,
headers: {
"Content-Type": file.type,
},
method: "PUT",
});
});
});

Expand All @@ -100,6 +113,19 @@ describe("document uploads and processing", () => {
);

expect(resp.ok).toBeTruthy();
expect(mockFetch).toHaveBeenCalledWith(
new URL("/api/documents/process/", DC_BASE),
{
body: JSON.stringify(created.map((d) => ({ id: d.id }))),
credentials: "include",
headers: {
"Content-type": "application/json",
Referer: APP_URL,
"X-CSRFToken": "csrf_token",
},
method: "POST",
},
);
});

test.todo("documents.cancel");
Expand Down
1 change: 0 additions & 1 deletion src/lib/api/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export async function create(
Referer: APP_URL,
},
body: JSON.stringify(documents),
referrerPolicy: "origin",
});

if (isErrorCode(resp.status)) {
Expand Down

0 comments on commit f772b1e

Please sign in to comment.