import { getQueryClient } from '../get-query-client'; import { PageClientType } from '../../types/page-client-type'; import { PageClientStore } from '../page-client.store'; import { QueryClientStore } from '../query-client.store'; import type { QueryClientConfig } from '@tanstack/query-core'; describe('getQueryClient', () => { it('should return PageClientStore for PageClientType.Page', () => { const result = getQueryClient(PageClientType.Page); expect(result).toEqual({ provide: QueryClientStore, useClass: PageClientStore, }); }); it('should return PageClientStore for "page" string', () => { const result = getQueryClient('page'); expect(result).toEqual({ provide: QueryClientStore, useClass: PageClientStore, }); }); it('should return custom class when provided', () => { class CustomStore extends QueryClientStore {} const result = getQueryClient(CustomStore); expect(result).toEqual({ provide: QueryClientStore, useClass: CustomStore, }); }); it('should return QueryClientStore for PageClientType.None', () => { const result = getQueryClient(PageClientType.None); expect(result).toBe(QueryClientStore); }); it('should return QueryClientStore when no clientStore is provided', () => { const result = getQueryClient(); expect(result).toBe(QueryClientStore); }); it('should return QueryClientStore when undefined is provided', () => { const result = getQueryClient(undefined); expect(result).toBe(QueryClientStore); }); describe('custom config', () => { const customConfig: QueryClientConfig = { defaultOptions: { queries: { staleTime: 0, retry: false }, }, }; it('should return a dynamic subclass for local client with config', () => { const result = getQueryClient(customConfig) as { provide: typeof QueryClientStore; useClass: typeof QueryClientStore; }; expect(result.provide).toBe(QueryClientStore); expect(result.useClass).not.toBe(QueryClientStore); expect(result.useClass.prototype).toBeInstanceOf(QueryClientStore); }); it('should apply custom config to the created instance', () => { const result = getQueryClient(customConfig) as { provide: typeof QueryClientStore; useClass: new () => QueryClientStore; }; const instance = new result.useClass(); expect(instance.queryClient.getDefaultOptions().queries?.staleTime).toBe(0); expect(instance.queryClient.getDefaultOptions().queries?.retry).toBe(false); }); it('should preserve "QueryClientStore" name on dynamic class', () => { const result = getQueryClient(customConfig) as { provide: typeof QueryClientStore; useClass: typeof QueryClientStore; }; expect(result.useClass.name).toBe('QueryClientStore'); }); }); });