|
| 1 | +/** |
| 2 | + * Regression tests for DocumentAnnotationIndex deep-linking behavior. |
| 3 | + * |
| 4 | + * Bug context: |
| 5 | + * The corpus home page renders a Table of Contents whose leaves are |
| 6 | + * structural annotations (e.g. "Subchapter I. Formation, p. 2"). Clicking |
| 7 | + * one is supposed to deep-link into the corresponding document with the |
| 8 | + * annotation pre-selected. |
| 9 | + * |
| 10 | + * The component originally overloaded a single `embedded` prop for two |
| 11 | + * meanings: "render without an outer container" AND "we are already on |
| 12 | + * the document page". The corpus-home call site needed the visual flavor |
| 13 | + * but absolutely was NOT on a document page. The click handler took the |
| 14 | + * wrong branch and rewrote `?ann=<id>` onto the corpus URL, so nothing |
| 15 | + * happened. These tests pin the new contract: visual layout (`embedded`) |
| 16 | + * and click routing (`onDocumentPage`) are independent. |
| 17 | + */ |
| 18 | +import React from "react"; |
| 19 | +import { render, screen, waitFor } from "@testing-library/react"; |
| 20 | +import userEvent from "@testing-library/user-event"; |
| 21 | +import { MemoryRouter } from "react-router-dom"; |
| 22 | +import { MockedProvider } from "@apollo/client/testing"; |
| 23 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 24 | + |
| 25 | +import { DocumentAnnotationIndex } from "../DocumentAnnotationIndex"; |
| 26 | +import { |
| 27 | + GET_DOCUMENT_ANNOTATION_INDEX, |
| 28 | + GetDocumentAnnotationIndexInput, |
| 29 | +} from "../../../graphql/queries"; |
| 30 | +import { |
| 31 | + DOCUMENT_ANNOTATION_INDEX_LIMIT, |
| 32 | + OC_SECTION_LABEL, |
| 33 | +} from "../../../assets/configurations/constants"; |
| 34 | +import { openedCorpus } from "../../../graphql/cache"; |
| 35 | + |
| 36 | +// Mock react-router-dom's useNavigate so we can assert on navigation intent. |
| 37 | +const mockNavigate = vi.fn(); |
| 38 | +vi.mock("react-router-dom", async () => { |
| 39 | + const actual = await vi.importActual<typeof import("react-router-dom")>( |
| 40 | + "react-router-dom" |
| 41 | + ); |
| 42 | + return { |
| 43 | + ...actual, |
| 44 | + useNavigate: () => mockNavigate, |
| 45 | + }; |
| 46 | +}); |
| 47 | + |
| 48 | +const DOC_ID = "doc-1"; |
| 49 | +const DOC_SLUG = "test-document"; |
| 50 | +const CORPUS_ID = "corpus-1"; |
| 51 | +const SECTION_ID = "ann-section-1"; |
| 52 | +const SECTION_TITLE = "Subchapter I. Formation"; |
| 53 | + |
| 54 | +const corpusForVar = { |
| 55 | + id: CORPUS_ID, |
| 56 | + slug: "test-corpus", |
| 57 | + creator: { id: "u1", slug: "test-user" }, |
| 58 | +} as Parameters<typeof openedCorpus>[0]; |
| 59 | + |
| 60 | +const variables: GetDocumentAnnotationIndexInput = { |
| 61 | + documentId: DOC_ID, |
| 62 | + corpusId: CORPUS_ID, |
| 63 | + labelText: OC_SECTION_LABEL, |
| 64 | + first: DOCUMENT_ANNOTATION_INDEX_LIMIT, |
| 65 | +}; |
| 66 | + |
| 67 | +const indexMock = { |
| 68 | + request: { query: GET_DOCUMENT_ANNOTATION_INDEX, variables }, |
| 69 | + result: { |
| 70 | + data: { |
| 71 | + annotations: { |
| 72 | + totalCount: 1, |
| 73 | + edges: [ |
| 74 | + { |
| 75 | + node: { |
| 76 | + id: SECTION_ID, |
| 77 | + rawText: SECTION_TITLE, |
| 78 | + longDescription: null, |
| 79 | + page: 2, |
| 80 | + parent: null, |
| 81 | + }, |
| 82 | + }, |
| 83 | + ], |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | +}; |
| 88 | + |
| 89 | +const renderIndex = (props: { |
| 90 | + onDocumentPage?: boolean; |
| 91 | + initialEntry: string; |
| 92 | +}) => { |
| 93 | + return render( |
| 94 | + <MockedProvider mocks={[indexMock]} addTypename={false}> |
| 95 | + <MemoryRouter initialEntries={[props.initialEntry]}> |
| 96 | + <DocumentAnnotationIndex |
| 97 | + documentId={DOC_ID} |
| 98 | + documentSlug={DOC_SLUG} |
| 99 | + corpusId={CORPUS_ID} |
| 100 | + embedded |
| 101 | + onDocumentPage={props.onDocumentPage} |
| 102 | + /> |
| 103 | + </MemoryRouter> |
| 104 | + </MockedProvider> |
| 105 | + ); |
| 106 | +}; |
| 107 | + |
| 108 | +describe("DocumentAnnotationIndex — section click deep-link routing", () => { |
| 109 | + beforeEach(() => { |
| 110 | + mockNavigate.mockReset(); |
| 111 | + openedCorpus(corpusForVar); |
| 112 | + }); |
| 113 | + |
| 114 | + it("navigates to the document URL with ?ann=<id> when clicked from the corpus home (default)", async () => { |
| 115 | + renderIndex({ initialEntry: "/c/test-user/test-corpus" }); |
| 116 | + |
| 117 | + // Wait for the section to render (data resolved) |
| 118 | + const item = await screen.findByRole("treeitem", { |
| 119 | + name: new RegExp(SECTION_TITLE, "i"), |
| 120 | + }); |
| 121 | + |
| 122 | + await userEvent.click(item); |
| 123 | + |
| 124 | + // Should have triggered a full navigation to the document page. |
| 125 | + // navigate(targetPath) is called with a string starting with /d/... |
| 126 | + await waitFor(() => { |
| 127 | + expect(mockNavigate).toHaveBeenCalledTimes(1); |
| 128 | + }); |
| 129 | + |
| 130 | + const navArg = mockNavigate.mock.calls[0][0]; |
| 131 | + expect(typeof navArg).toBe("string"); |
| 132 | + expect(navArg).toMatch(/^\/d\/test-user\/test-corpus\/test-document/); |
| 133 | + expect(navArg).toContain(`ann=${SECTION_ID}`); |
| 134 | + }); |
| 135 | + |
| 136 | + it("only updates ?ann= on the current URL when onDocumentPage is true", async () => { |
| 137 | + renderIndex({ |
| 138 | + onDocumentPage: true, |
| 139 | + initialEntry: "/d/test-user/test-corpus/test-document", |
| 140 | + }); |
| 141 | + |
| 142 | + const item = await screen.findByRole("treeitem", { |
| 143 | + name: new RegExp(SECTION_TITLE, "i"), |
| 144 | + }); |
| 145 | + |
| 146 | + await userEvent.click(item); |
| 147 | + |
| 148 | + await waitFor(() => { |
| 149 | + expect(mockNavigate).toHaveBeenCalledTimes(1); |
| 150 | + }); |
| 151 | + |
| 152 | + // Search-only navigation: navigate({ search: "..." }, { replace: true }) |
| 153 | + const [navArg, navOpts] = mockNavigate.mock.calls[0]; |
| 154 | + expect(navArg).toEqual({ |
| 155 | + search: expect.stringContaining(`ann=${SECTION_ID}`), |
| 156 | + }); |
| 157 | + expect(navOpts).toEqual({ replace: true }); |
| 158 | + }); |
| 159 | + |
| 160 | + it("does NOT update ?ann= on the current URL when on the corpus home (regression: embedded must not imply onDocumentPage)", async () => { |
| 161 | + renderIndex({ |
| 162 | + // embedded=true is set by renderIndex, but onDocumentPage is unset |
| 163 | + initialEntry: "/c/test-user/test-corpus", |
| 164 | + }); |
| 165 | + |
| 166 | + const item = await screen.findByRole("treeitem", { |
| 167 | + name: new RegExp(SECTION_TITLE, "i"), |
| 168 | + }); |
| 169 | + |
| 170 | + await userEvent.click(item); |
| 171 | + |
| 172 | + await waitFor(() => { |
| 173 | + expect(mockNavigate).toHaveBeenCalledTimes(1); |
| 174 | + }); |
| 175 | + |
| 176 | + // The single navigate call must be a full URL string (not a {search} |
| 177 | + // object), proving we did NOT take the "already on the document page" |
| 178 | + // branch that originally swallowed the deep link. |
| 179 | + const navArg = mockNavigate.mock.calls[0][0]; |
| 180 | + expect(typeof navArg).toBe("string"); |
| 181 | + expect(navArg).not.toMatch(/^\/c\//); // must leave the corpus URL |
| 182 | + expect(navArg).toMatch(/^\/d\//); |
| 183 | + }); |
| 184 | +}); |
0 commit comments