11import { render , screen } from '@testing-library/react' ;
22import { describe , expect , it , vi } from 'vitest' ;
33import '@testing-library/jest-dom' ;
4+ import userEvent from '@testing-library/user-event' ;
45import SearchResult from '../SearchResult' ;
56import { SearchResult as SearchResultType , ZipCase } from '../../../../../shared/types' ;
67import { QueryClient , QueryClientProvider } from '@tanstack/react-query' ;
@@ -19,9 +20,7 @@ const createTestQueryClient = () => {
1920
2021const createWrapper = ( queryClient ?: QueryClient ) => {
2122 const testQueryClient = queryClient || createTestQueryClient ( ) ;
22- return ( { children } : { children : React . ReactNode } ) => (
23- < QueryClientProvider client = { testQueryClient } > { children } </ QueryClientProvider >
24- ) ;
23+ return ( { children } : { children : React . ReactNode } ) => < QueryClientProvider client = { testQueryClient } > { children } </ QueryClientProvider > ;
2524} ;
2625
2726// Mock SearchStatus component
@@ -33,6 +32,11 @@ vi.mock('../SearchStatus', () => ({
3332 ) ) ,
3433} ) ) ;
3534
35+ // Mock useCaseSearch hook
36+ vi . mock ( '../../../hooks/useCaseSearch' , ( ) => ( {
37+ useRemoveCase : ( ) => vi . fn ( ) ,
38+ } ) ) ;
39+
3640// Mock constants from aws-exports
3741vi . mock ( '../../../aws-exports' , ( ) => ( {
3842 API_URL : 'https://api.example.com' ,
@@ -325,4 +329,78 @@ describe('SearchResult component', () => {
325329 expect ( removeButton ) . toBeInTheDocument ( ) ;
326330 expect ( removeButton ) . toHaveAttribute ( 'title' , 'Remove case from results' ) ;
327331 } ) ;
332+
333+ it ( 'displays copy button when caseId is present' , ( ) => {
334+ const testCase = createTestCase ( ) ;
335+ render ( < SearchResult searchResult = { testCase } /> , { wrapper : createWrapper ( ) } ) ;
336+
337+ // Check that copy button is rendered
338+ const copyButton = screen . getByTitle ( 'Copy case number to clipboard' ) ;
339+ expect ( copyButton ) . toBeInTheDocument ( ) ;
340+ } ) ;
341+
342+ it ( 'displays copy button when caseId is not present' , ( ) => {
343+ const testCase = createTestCase ( {
344+ zipCase : {
345+ caseNumber : '22CR123456-789' ,
346+ caseId : undefined ,
347+ fetchStatus : {
348+ status : 'processing' ,
349+ } ,
350+ } ,
351+ } ) ;
352+ render ( < SearchResult searchResult = { testCase } /> , { wrapper : createWrapper ( ) } ) ;
353+
354+ // Check that copy button is rendered
355+ const copyButton = screen . getByTitle ( 'Copy case number to clipboard' ) ;
356+ expect ( copyButton ) . toBeInTheDocument ( ) ;
357+ } ) ;
358+
359+ it ( 'copies case number to clipboard when copy button is clicked' , async ( ) => {
360+ const user = userEvent . setup ( ) ;
361+
362+ // Mock clipboard API
363+ const writeTextMock = vi . fn ( ) . mockResolvedValue ( undefined ) ;
364+ Object . defineProperty ( navigator , 'clipboard' , {
365+ value : {
366+ writeText : writeTextMock ,
367+ } ,
368+ writable : true ,
369+ configurable : true ,
370+ } ) ;
371+
372+ const testCase = createTestCase ( ) ;
373+ render ( < SearchResult searchResult = { testCase } /> , { wrapper : createWrapper ( ) } ) ;
374+
375+ // Click the copy button
376+ const copyButton = screen . getByTitle ( 'Copy case number to clipboard' ) ;
377+ await user . click ( copyButton ) ;
378+
379+ // Verify that clipboard.writeText was called with the correct case number
380+ expect ( writeTextMock ) . toHaveBeenCalledWith ( '22CR123456-789' ) ;
381+ } ) ;
382+
383+ it ( 'shows visual feedback when case number is copied' , async ( ) => {
384+ const user = userEvent . setup ( ) ;
385+
386+ // Mock clipboard API
387+ const writeTextMock = vi . fn ( ) . mockResolvedValue ( undefined ) ;
388+ Object . defineProperty ( navigator , 'clipboard' , {
389+ value : {
390+ writeText : writeTextMock ,
391+ } ,
392+ writable : true ,
393+ configurable : true ,
394+ } ) ;
395+
396+ const testCase = createTestCase ( ) ;
397+ render ( < SearchResult searchResult = { testCase } /> , { wrapper : createWrapper ( ) } ) ;
398+
399+ // Click the copy button
400+ const copyButton = screen . getByTitle ( 'Copy case number to clipboard' ) ;
401+ await user . click ( copyButton ) ;
402+
403+ // Check that the title changes to indicate success
404+ expect ( screen . getByTitle ( 'Copied!' ) ) . toBeInTheDocument ( ) ;
405+ } ) ;
328406} ) ;
0 commit comments