11import { act , renderHook } from '@testing-library/react'
22import { beforeEach , describe , expect , it } from 'vitest'
33import { useBackgroundJobs } from './useBackgroundJobs'
4+ import {
5+ BACKGROUND_JOBS_STORAGE_KEY ,
6+ useBackgroundJobsStore ,
7+ } from './useBackgroundJobsStore'
48import type { BackgroundJob } from '../lib/schemas'
59
610describe ( 'useBackgroundJobs' , ( ) => {
7- const STORAGE_KEY = 'background-jobs'
8-
911 beforeEach ( ( ) => {
10- localStorage . clear ( )
12+ const { result } = renderHook ( ( ) => useBackgroundJobs ( ) )
13+ act ( ( ) => {
14+ result . current . clearAllJobs ( )
15+ } )
1116 } )
1217
1318 const createMockJob = (
@@ -21,6 +26,19 @@ describe('useBackgroundJobs', () => {
2126 } )
2227
2328 describe ( 'initialization and persistence' , ( ) => {
29+ it ( 'should reset jobs using resetJobs()' , ( ) => {
30+ const { result } = renderHook ( ( ) => useBackgroundJobs ( ) )
31+ const job = createMockJob ( { id : 'reset-job' } )
32+ act ( ( ) => {
33+ result . current . addJob ( job )
34+ } )
35+ expect ( result . current . jobs ) . toHaveLength ( 1 )
36+ act ( ( ) => {
37+ result . current . clearAllJobs ( )
38+ } )
39+ expect ( result . current . jobs ) . toHaveLength ( 0 )
40+ expect ( result . current . jobsMap ) . toEqual ( { } )
41+ } )
2442 it ( 'should start with empty jobs when no data in localStorage' , ( ) => {
2543 const { result } = renderHook ( ( ) => useBackgroundJobs ( ) )
2644
@@ -33,7 +51,8 @@ describe('useBackgroundJobs', () => {
3351 const job2 = createMockJob ( { id : 'job2' , status : 'completed' } )
3452 const storedData = { job1, job2 }
3553
36- localStorage . setItem ( STORAGE_KEY , JSON . stringify ( storedData ) )
54+ // Initialize store with existing data using proper API
55+ useBackgroundJobsStore . getState ( ) . initializeJobs ( storedData )
3756
3857 const { result } = renderHook ( ( ) => useBackgroundJobs ( ) )
3958
@@ -229,8 +248,10 @@ describe('useBackgroundJobs', () => {
229248 expect ( finalJob ?. response ) . toBe ( 'Job completed successfully' )
230249 expect ( finalJob ?. completedAt ) . toBe ( '2025-01-01T01:00:00Z' )
231250
232- const storedData = JSON . parse ( localStorage . getItem ( STORAGE_KEY ) || '{}' )
233- expect ( storedData [ job . id ] ) . toEqual ( finalJob )
251+ const storedData = JSON . parse (
252+ localStorage . getItem ( BACKGROUND_JOBS_STORAGE_KEY ) || '{}' ,
253+ )
254+ expect ( storedData . state . jobs [ job . id ] ) . toEqual ( finalJob )
234255 } )
235256
236257 it ( 'should handle concurrent job operations' , ( ) => {
@@ -252,7 +273,11 @@ describe('useBackgroundJobs', () => {
252273 } )
253274
254275 expect ( result . current . jobs ) . toHaveLength ( 0 )
255- expect ( localStorage . getItem ( STORAGE_KEY ) ) . toBeNull ( )
276+ // Zustand persist middleware always creates localStorage entry, so check it has empty jobs
277+ const storedData = JSON . parse (
278+ localStorage . getItem ( BACKGROUND_JOBS_STORAGE_KEY ) || '{}' ,
279+ )
280+ expect ( storedData . state ?. jobs ) . toEqual ( { } )
256281
257282 act ( ( ) => {
258283 result . current . addJob ( runningJob )
@@ -305,7 +330,11 @@ describe('useBackgroundJobs', () => {
305330 const firstSessionResult = renderHook ( ( ) => useBackgroundJobs ( ) )
306331
307332 expect ( firstSessionResult . result . current . jobs ) . toHaveLength ( 0 )
308- expect ( localStorage . getItem ( STORAGE_KEY ) ) . toBeNull ( )
333+ // Zustand persist middleware always creates localStorage entry, so check it has empty jobs
334+ const initialStoredData = JSON . parse (
335+ localStorage . getItem ( BACKGROUND_JOBS_STORAGE_KEY ) || '{}' ,
336+ )
337+ expect ( initialStoredData . state ?. jobs ) . toEqual ( { } )
309338
310339 act ( ( ) => {
311340 firstSessionResult . result . current . addJob ( job1 )
0 commit comments