|
| 1 | +import { QueryClient } from '@tanstack/query-core'; |
| 2 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { |
| 5 | + __createClerkTestQueryClient, |
| 6 | + __resetClerkQueryClientForTest, |
| 7 | + __setClerkQueryClientForTest, |
| 8 | + getClerkQueryClient, |
| 9 | +} from '../clerk-query-client'; |
| 10 | + |
| 11 | +afterEach(() => { |
| 12 | + vi.unstubAllGlobals(); |
| 13 | + __resetClerkQueryClientForTest(); |
| 14 | +}); |
| 15 | + |
| 16 | +describe('getClerkQueryClient', () => { |
| 17 | + it('returns undefined when window is not defined (SSR)', () => { |
| 18 | + vi.stubGlobal('window', undefined); |
| 19 | + |
| 20 | + expect(getClerkQueryClient()).toBeUndefined(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('does not cache the SSR undefined — a later browser call still creates a client', () => { |
| 24 | + vi.stubGlobal('window', undefined); |
| 25 | + expect(getClerkQueryClient()).toBeUndefined(); |
| 26 | + |
| 27 | + vi.unstubAllGlobals(); |
| 28 | + const client = getClerkQueryClient(); |
| 29 | + expect(client).toBeInstanceOf(QueryClient); |
| 30 | + }); |
| 31 | + |
| 32 | + it('lazy-creates a singleton on the browser and returns the same instance on repeated calls', () => { |
| 33 | + const first = getClerkQueryClient(); |
| 34 | + const second = getClerkQueryClient(); |
| 35 | + |
| 36 | + expect(first).toBeInstanceOf(QueryClient); |
| 37 | + expect(second).toBe(first); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe('__resetClerkQueryClientForTest', () => { |
| 42 | + it('clears the singleton so the next read lazy-creates a fresh client', () => { |
| 43 | + const original = getClerkQueryClient(); |
| 44 | + expect(original).toBeInstanceOf(QueryClient); |
| 45 | + |
| 46 | + __resetClerkQueryClientForTest(); |
| 47 | + |
| 48 | + const next = getClerkQueryClient(); |
| 49 | + expect(next).toBeInstanceOf(QueryClient); |
| 50 | + expect(next).not.toBe(original); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe('__setClerkQueryClientForTest', () => { |
| 55 | + it('installs a caller-supplied client and returns it from getClerkQueryClient', () => { |
| 56 | + const custom = new QueryClient(); |
| 57 | + __setClerkQueryClientForTest(custom); |
| 58 | + |
| 59 | + expect(getClerkQueryClient()).toBe(custom); |
| 60 | + }); |
| 61 | + |
| 62 | + it('installs the "no client" state without triggering lazy creation on subsequent reads', () => { |
| 63 | + __setClerkQueryClientForTest(undefined); |
| 64 | + |
| 65 | + expect(getClerkQueryClient()).toBeUndefined(); |
| 66 | + expect(getClerkQueryClient()).toBeUndefined(); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe('__createClerkTestQueryClient', () => { |
| 71 | + it('returns a QueryClient with deterministic defaults and installs it as the singleton', () => { |
| 72 | + const client = __createClerkTestQueryClient(); |
| 73 | + |
| 74 | + expect(client).toBeInstanceOf(QueryClient); |
| 75 | + expect(getClerkQueryClient()).toBe(client); |
| 76 | + |
| 77 | + const defaults = client.getDefaultOptions().queries; |
| 78 | + expect(defaults?.retry).toBe(false); |
| 79 | + expect(defaults?.staleTime).toBe(Infinity); |
| 80 | + expect(defaults?.refetchOnWindowFocus).toBe(false); |
| 81 | + expect(defaults?.refetchOnReconnect).toBe(false); |
| 82 | + expect(defaults?.refetchOnMount).toBe(false); |
| 83 | + }); |
| 84 | +}); |
0 commit comments