Skip to content

Commit

Permalink
fix(frontend): correctly display document information in explore view…
Browse files Browse the repository at this point in the history
… details (#781)

* stringify json values and render within pre tags so react can render them

* run prettier

* add tests for first document

* prettier

* return 'Not Available' instead of 'null' string

* don't render objects in document details; add tests

* remove newlines from imports
  • Loading branch information
cpsoinos committed Jul 31, 2023
1 parent 56f254a commit 87c5e58
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
9 changes: 5 additions & 4 deletions frontend/app/explore/DocumentItem/DocumentData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ const DocumentData = ({ documentName }: DocumentDataProps): JSX.Element => {

<div className="flex flex-col">
{documents[0] &&
Object.keys(documents[0]).map((doc) => {
Object.entries(documents[0]).map(([key, value]) => {
if (value && typeof value === "object") return;
return (
<div className="grid grid-cols-2 py-2 border-b" key={doc}>
<div className="grid grid-cols-2 py-2 border-b" key={key}>
<p className="capitalize font-bold break-words">
{doc.replaceAll("_", " ")}
{key.replaceAll("_", " ")}
</p>
<span className="break-words my-auto">
{documents[0][doc] || "Not Available"}
{String(value || "Not Available")}
</span>
</div>
);
Expand Down
77 changes: 77 additions & 0 deletions frontend/app/explore/DocumentItem/__tests__/DocumentData.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { render, screen, waitFor } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { BrainConfigProvider } from "@/lib/context/BrainConfigProvider";
import DocumentData from "../DocumentData";

const useSupabaseMock = vi.fn(() => ({
session: { user: {} },
}));

vi.mock("@/lib/context/SupabaseProvider", () => ({
useSupabase: () => useSupabaseMock(),
}));

vi.mock("@/lib/hooks", async () => {
const actual = await vi.importActual<typeof import("@/lib/hooks")>(
"@/lib/hooks"
);

return {
...actual,
useAxios: () => ({
axiosInstance: {
get: vi.fn().mockResolvedValue({
data: {
documents: [
{
file_name: "mock_file_name.pdf",
file_size: "0",
file_extension: null,
file_url: null,
content: "foo,bar\nbaz,bat",
brains_vectors: [
{
brain_id: "mock_brain_id",
vector_id: "mock_vector_id_1",
},
],
},
{
file_name: "mock_file_name.pdf",
file_size: "0",
file_extension: null,
file_url: null,
content: "foo,bar\nbaz,bat",
brains_vectors: [
{
brain_id: "mock_brain_id",
vector_id: "mock_vector_id_2",
},
],
},
],
},
}),
},
}),
};
});

describe("DocumentData", () => {
it("should render document data", async () => {
const documentName = "Test document";
render(
<BrainConfigProvider>
<DocumentData documentName={documentName} />
</BrainConfigProvider>
);

expect(screen.getByText(documentName)).toBeDefined();

await waitFor(() => {
expect(screen.getByText("content")).toBeDefined();
expect(screen.getByText("foo,bar", { exact: false })).toBeDefined();
expect(screen.getByText("baz,bat", { exact: false })).toBeDefined();
});
});
});

0 comments on commit 87c5e58

Please sign in to comment.