@@ -5,7 +5,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
55import { renderHook , act , waitFor } from '@testing-library/react' ;
66import { QueryClient , QueryClientProvider } from '@tanstack/react-query' ;
77import React from 'react' ;
8- import { useSearchResults , useConsolidatedPolling } from '../useCaseSearch' ;
8+ import { useSearchResults , useConsolidatedPolling , useRemoveCase } from '../useCaseSearch' ;
99
1010vi . mock ( '../../aws-exports' , ( ) => ( {
1111 API_URL : 'http://test-api.example.com' ,
@@ -167,3 +167,129 @@ describe('useConsolidatedPolling - state management', () => {
167167 } ) ;
168168 } ) ;
169169} ) ;
170+
171+ describe ( 'useRemoveCase' , ( ) => {
172+ beforeEach ( ( ) => {
173+ vi . resetAllMocks ( ) ;
174+ } ) ;
175+
176+ afterEach ( ( ) => {
177+ vi . resetAllMocks ( ) ;
178+ } ) ;
179+
180+ it ( 'should remove a case from results and batches' , ( ) => {
181+ const testData = {
182+ results : {
183+ case123 : {
184+ zipCase : {
185+ caseNumber : 'case123' ,
186+ fetchStatus : { status : 'complete' } ,
187+ } ,
188+ } ,
189+ case456 : {
190+ zipCase : {
191+ caseNumber : 'case456' ,
192+ fetchStatus : { status : 'complete' } ,
193+ } ,
194+ } ,
195+ } ,
196+ searchBatches : [ [ 'case123' , 'case456' ] ] ,
197+ } ;
198+
199+ const queryClient = createTestQueryClient ( ) ;
200+ queryClient . setQueryData ( [ 'searchResults' ] , testData ) ;
201+ const wrapper = createWrapper ( queryClient ) ;
202+
203+ const { result } = renderHook ( ( ) => useRemoveCase ( ) , { wrapper } ) ;
204+
205+ // Remove case123
206+ act ( ( ) => {
207+ result . current ( 'case123' ) ;
208+ } ) ;
209+
210+ // Check that the case was removed
211+ const updatedData = queryClient . getQueryData ( [ 'searchResults' ] ) ;
212+ expect ( updatedData ) . toEqual ( {
213+ results : {
214+ case456 : {
215+ zipCase : {
216+ caseNumber : 'case456' ,
217+ fetchStatus : { status : 'complete' } ,
218+ } ,
219+ } ,
220+ } ,
221+ searchBatches : [ [ 'case456' ] ] ,
222+ } ) ;
223+ } ) ;
224+
225+ it ( 'should remove empty batches after removing all cases' , ( ) => {
226+ const testData = {
227+ results : {
228+ case123 : {
229+ zipCase : {
230+ caseNumber : 'case123' ,
231+ fetchStatus : { status : 'complete' } ,
232+ } ,
233+ } ,
234+ } ,
235+ searchBatches : [ [ 'case123' ] ] ,
236+ } ;
237+
238+ const queryClient = createTestQueryClient ( ) ;
239+ queryClient . setQueryData ( [ 'searchResults' ] , testData ) ;
240+ const wrapper = createWrapper ( queryClient ) ;
241+
242+ const { result } = renderHook ( ( ) => useRemoveCase ( ) , { wrapper } ) ;
243+
244+ // Remove case123
245+ act ( ( ) => {
246+ result . current ( 'case123' ) ;
247+ } ) ;
248+
249+ // Check that the batch was removed too
250+ const updatedData = queryClient . getQueryData ( [ 'searchResults' ] ) ;
251+ expect ( updatedData ) . toEqual ( {
252+ results : { } ,
253+ searchBatches : [ ] ,
254+ } ) ;
255+ } ) ;
256+
257+ it ( 'should handle removing a non-existent case gracefully' , ( ) => {
258+ const testData = {
259+ results : {
260+ case123 : {
261+ zipCase : {
262+ caseNumber : 'case123' ,
263+ fetchStatus : { status : 'complete' } ,
264+ } ,
265+ } ,
266+ } ,
267+ searchBatches : [ [ 'case123' ] ] ,
268+ } ;
269+
270+ const queryClient = createTestQueryClient ( ) ;
271+ queryClient . setQueryData ( [ 'searchResults' ] , testData ) ;
272+ const wrapper = createWrapper ( queryClient ) ;
273+
274+ const { result } = renderHook ( ( ) => useRemoveCase ( ) , { wrapper } ) ;
275+
276+ // Remove non-existent case
277+ act ( ( ) => {
278+ result . current ( 'case999' ) ;
279+ } ) ;
280+
281+ // Check that the state remains the same
282+ const updatedData = queryClient . getQueryData ( [ 'searchResults' ] ) ;
283+ expect ( updatedData ) . toEqual ( {
284+ results : {
285+ case123 : {
286+ zipCase : {
287+ caseNumber : 'case123' ,
288+ fetchStatus : { status : 'complete' } ,
289+ } ,
290+ } ,
291+ } ,
292+ searchBatches : [ [ 'case123' ] ] ,
293+ } ) ;
294+ } ) ;
295+ } ) ;
0 commit comments