providers-query-client-defaults.test.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { describe, expect, test } from "vitest";
  2. /**
  3. * Verify that the QueryClient created by AppProviders has the expected
  4. * memory-friendly default options (gcTime, staleTime, refetchIntervalInBackground, etc.)
  5. *
  6. * We import the module and inspect the QueryClient that useState would produce.
  7. * Because AppProviders wraps QueryClient creation in useState(() => new QueryClient(...)),
  8. * we test the configuration by constructing a QueryClient with the same options.
  9. */
  10. import { QUERY_CLIENT_DEFAULTS } from "@/app/providers";
  11. import { QueryClient } from "@tanstack/react-query";
  12. function createQueryClientWithDefaults(): QueryClient {
  13. return new QueryClient({
  14. defaultOptions: QUERY_CLIENT_DEFAULTS,
  15. });
  16. }
  17. describe("QueryClient global defaults", () => {
  18. test("gcTime is 2 minutes (120000ms)", () => {
  19. const qc = createQueryClientWithDefaults();
  20. expect(qc.getDefaultOptions().queries?.gcTime).toBe(2 * 60 * 1000);
  21. });
  22. test("staleTime is 30 seconds", () => {
  23. const qc = createQueryClientWithDefaults();
  24. expect(qc.getDefaultOptions().queries?.staleTime).toBe(30_000);
  25. });
  26. test("refetchOnWindowFocus is disabled", () => {
  27. const qc = createQueryClientWithDefaults();
  28. expect(qc.getDefaultOptions().queries?.refetchOnWindowFocus).toBe(false);
  29. });
  30. test("refetchIntervalInBackground is disabled", () => {
  31. const qc = createQueryClientWithDefaults();
  32. expect(qc.getDefaultOptions().queries?.refetchIntervalInBackground).toBe(false);
  33. });
  34. });