@@ -4,23 +4,27 @@ import {
44 MessageId ,
55 ThreadId ,
66} from "@t3tools/contracts" ;
7- import { useState , type ReactNode } from "react" ;
7+ import { useState , type MouseEventHandler , type ReactNode } from "react" ;
88import { describe , expect , it , vi } from "vite-plus/test" ;
99import { page , userEvent } from "vite-plus/test/browser" ;
1010
1111import type { AssistantCitationSourceAnchor } from "~/lib/assistantTextSelection" ;
1212import "~/index.css" ;
1313import { act , render } from "../../../test/render" ;
1414
15- const mocks = vi . hoisted ( ( ) => ( { observeSource : vi . fn ( ) , dispose : vi . fn ( ) } ) ) ;
15+ const mocks = vi . hoisted ( ( ) => ( { observeSource : vi . fn ( ) , dispose : vi . fn ( ) , navigate : vi . fn ( ) } ) ) ;
1616vi . mock ( import ( "./AssistantCitationSource" ) , async ( importOriginal ) => ( {
1717 ...( await importOriginal ( ) ) ,
1818 observeAssistantCitationCommentSource : mocks . observeSource ,
1919} ) ) ;
2020vi . mock ( import ( "@tanstack/react-router" ) , async ( importOriginal ) => ( {
2121 ...( await importOriginal ( ) ) ,
22- useNavigate : ( ) => vi . fn ( ) ,
23- Link : ( ( { children } : { children : ReactNode } ) => < a href = "#source" > { children } </ a > ) as never ,
22+ useNavigate : ( ) => mocks . navigate ,
23+ Link : ( ( { children, onClick } : { children : ReactNode ; onClick ?: MouseEventHandler } ) => (
24+ < a href = "#source" onClick = { onClick } >
25+ { children }
26+ </ a >
27+ ) ) as never ,
2428} ) ) ;
2529
2630import { AssistantCitationChip } from "./AssistantCitationChip" ;
@@ -41,6 +45,7 @@ const citation = {
4145function setup ( ) {
4246 mocks . observeSource . mockReset ( ) . mockReturnValue ( mocks . dispose ) ;
4347 mocks . dispose . mockClear ( ) ;
48+ mocks . navigate . mockClear ( ) ;
4449 const source = document . createElement ( "p" ) ;
4550 source . textContent = "hello" ;
4651 document . body . append ( source ) ;
@@ -214,7 +219,7 @@ describe("draft citation chip", () => {
214219 }
215220 await using _screen = await render ( < Composer /> ) ;
216221
217- await page . getByRole ( "button" , { name : " Edit citation comment" } ) . click ( ) ;
222+ await page . getByRole ( "button" , { name : / ^ E d i t c i t a t i o n c o m m e n t : / } ) . click ( ) ;
218223
219224 await expect . element ( page . getByRole ( "blockquote" ) ) . toHaveTextContent ( "hello" ) ;
220225 await expect . element ( page . getByText ( "Go to source" ) ) . toBeVisible ( ) ;
@@ -244,11 +249,36 @@ describe("draft citation chip", () => {
244249 }
245250 await using _screen = await render ( < Composer /> ) ;
246251
247- await page . getByRole ( "button" , { name : " Edit citation comment" } ) . click ( ) ;
252+ await page . getByRole ( "button" , { name : / ^ E d i t c i t a t i o n c o m m e n t : / } ) . click ( ) ;
248253
249254 await vi . waitFor ( ( ) => expect ( document . activeElement ) . toBe ( textarea ( ) ) ) ;
250255 await expect . element ( page . getByLabelText ( "Comment on selected text" ) ) . toBeInViewport ( ) ;
251256 const input = textarea ( ) ! ;
252257 expect ( input . scrollTop + input . clientHeight ) . toBeGreaterThanOrEqual ( input . scrollHeight - 1 ) ;
253258 } ) ;
259+
260+ it ( "stays on the page while the draft is too long to save" , async ( ) => {
261+ using _source = setup ( ) ;
262+ function Composer ( ) {
263+ const [ open , setOpen ] = useState ( false ) ;
264+ return (
265+ < AssistantCitationChip
266+ citation = { citation }
267+ commentEditor = { { open, onOpenChange : setOpen , onSave : ( ) => true } }
268+ />
269+ ) ;
270+ }
271+ await using _screen = await render ( < Composer /> ) ;
272+ await page . getByRole ( "button" , { name : / ^ A d d c o m m e n t t o c i t a t i o n : / } ) . click ( ) ;
273+ await vi . waitFor ( ( ) => expect ( textarea ( ) ) . not . toBeNull ( ) ) ;
274+
275+ await typeComment ( "x" . repeat ( ASSISTANT_CITATION_MAX_COMMENT_LENGTH + 1 ) ) ;
276+ await page . getByRole ( "link" , { name : "Go to source" } ) . click ( ) ;
277+ expect ( mocks . navigate ) . not . toHaveBeenCalled ( ) ;
278+ expect ( textarea ( ) ) . not . toBeNull ( ) ;
279+
280+ await typeComment ( "short" ) ;
281+ await page . getByRole ( "link" , { name : "Go to source" } ) . click ( ) ;
282+ expect ( mocks . navigate ) . toHaveBeenCalledTimes ( 1 ) ;
283+ } ) ;
254284} ) ;
0 commit comments