Skip to content

Commit

Permalink
test for documents.create
Browse files Browse the repository at this point in the history
  • Loading branch information
eyeseast committed Apr 15, 2024
1 parent 1d38231 commit 41ebbfd
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 15 deletions.
47 changes: 44 additions & 3 deletions src/lib/api/documents.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { describe, test as base, expect } from "vitest";
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 * as documents from "./documents";
import type { Document, Sizes } from "./types";

type Use<T> = (value: T) => Promise<void>;

Expand All @@ -14,6 +23,12 @@ const test = base.extend({

await use(document as unknown as Document);
},

created: async ({}, use: Use<Document>) => {
const created = await import("./fixtures/documents/create.json");

await use(created as unknown as Document);
},
});

describe("document fetching", () => {
Expand All @@ -22,9 +37,35 @@ describe("document fetching", () => {
});

describe("document uploads and processing", () => {
test.todo("documents.create");
afterEach(() => {
vi.restoreAllMocks();
});

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

const doc: DocumentUpload = {
title: created.title,
access: created.access,
language: created.language,
original_extension: created.original_extension,
};

documents.create([doc], "token", mockFetch).then((d) => {
expect(d).toMatchObject(created);
});
});

test.todo("documents.upload");

test.todo("documents.process");

test.todo("documents.cancel");
});

describe("document helper methods", () => {
Expand Down
32 changes: 32 additions & 0 deletions src/lib/api/fixtures/documents/create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"id": 20000031,
"access": "private",
"admin_noindex": false,
"asset_url": "https://api.dev.documentcloud.org/files/",
"canonical_url": "https://www.dev.documentcloud.org/documents/20000031-finalseasonal-allergies-pollen-and-mold-2023-en",
"created_at": "2024-04-15T01:22:45.191803Z",
"data": {},
"description": "",
"edit_access": true,
"file_hash": "",
"noindex": false,
"language": "eng",
"organization": 10001,
"original_extension": "pdf",
"page_count": 0,
"page_spec": "",
"presigned_url": "https://documentcloud-dev-test.s3.amazonaws.com/documents/20000031/finalseasonal-allergies-pollen-and-mold-2023-en.pdf",
"projects": [],
"publish_at": null,
"published_url": "",
"related_article": "",
"revision_control": false,
"slug": "finalseasonal-allergies-pollen-and-mold-2023-en",
"source": "",
"status": "nofile",
"title": "FINALSeasonal allergies pollen and mold 2023 EN",
"updated_at": "2024-04-15T01:22:45.192143Z",
"user": 100000
}
]
2 changes: 0 additions & 2 deletions src/lib/api/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export interface DocumentUpload {
access: Access;
data?: Data;
language?: string;
ocr_engine?: string;
original_extension?: string;
noindex?: boolean;
projects?: number[]; // project ids only
Expand All @@ -98,7 +97,6 @@ export interface Document {
file_hash?: string;
noindex?: boolean;
language: string;
ocr_engine: string;
organization: number | Org;
original_extension: string;
page_count: number;
Expand Down
10 changes: 6 additions & 4 deletions src/lib/components/forms/DocumentUpload.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<script context="module" lang="ts">
import { error, type ActionResult } from "@sveltejs/kit";
import type { ActionResult } from "@sveltejs/kit";
import { DEFAULT_LANGUAGE } from "@/config/config.js";
/**
* Collect form data into documents and do three-step upload.
* Exported here for testing and reuse.
Expand All @@ -15,7 +17,7 @@
const filenames = form.getAll("filename") as string[];
// one per batch
const access = form.get("access") as Access;
const access = (form.get("access") || "private") as Access;
// value is a JSON string
const ocr_engine: OCREngine = unwrap(form.get("ocr_engine") as string);
Expand Down Expand Up @@ -124,7 +126,7 @@
import Text from "../inputs/Text.svelte";
import * as documents from "$lib/api/documents";
import { DEFAULT_LANGUAGE, DOCUMENT_TYPES } from "@/config/config.js";
import { DOCUMENT_TYPES } from "@/config/config.js";
import { isSupported } from "@/lib/utils/validateFiles";
let files: File[] = [];
Expand Down Expand Up @@ -174,7 +176,7 @@
function filenameToTitle(filename: string): string {
const [name, ...ext] = filename.split(".");
return name.replace(/_/g, " ");
return name.replace(/_+/g, " ").replace(/\s+/, " ").trim();
}
// handle uploads client side instead of going through the server
Expand Down
7 changes: 1 addition & 6 deletions src/test/fixtures/documents.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type { Document, Page } from "../../api/types";

/*
*/
import type { Document, Page } from "@/api/types";

export const document: Document = {
id: 24002098,
Expand Down

0 comments on commit 41ebbfd

Please sign in to comment.