Open History to see the last commit
1// cspell:ignore ZONEFUL zoneful
2import {
3 isSignal,
4 provideZoneChangeDetection,
5 provideZonelessChangeDetection,
6 untracked,
7} from '@angular/core'
8import { TestBed } from '@angular/core/testing'
9import { expect, vi } from 'vitest'
10import { provideTanStackQuery } from '..'
11import type { QueryClient } from '@tanstack/query-core'
12import type { EnvironmentProviders, Provider, Signal } from '@angular/core'
13β
14/**
15 * Use the same change-detection mode as the test runner. The default runner
16 * is zoneless; the zoneful runner sets ANGULAR_QUERY_ZONEFUL before loading
17 * tests and installs the real Zone.js testing patches.
18 */
19export function provideAngularQueryChangeDetection(): EnvironmentProviders {
20 return process.env.ANGULAR_QUERY_ZONEFUL === 'true'
21 ? provideZoneChangeDetection()
22 : provideZonelessChangeDetection()
23}
24β
25function evaluateSignals<T extends Record<string, any>>(
26 obj: T,
27): { [K in keyof T]: ReturnType<T[K]> } {
28 const result: Partial<{ [K in keyof T]: ReturnType<T[K]> }> = {}
29β
30 untracked(() => {
31 for (const key in obj) {
32 if (
33 Object.prototype.hasOwnProperty.call(obj, key) &&
34 isSignal(obj[key])
35 ) {
36 const func = obj[key]
37 result[key] = func()
38 }
39 }
40 })
41β
42 return result as { [K in keyof T]: ReturnType<T[K]> }
43}
44β
45export const expectSignals = <T extends Record<string, any>>(
46 obj: T,
47 expected: Partial<{
48 [K in keyof T]: T[K] extends Signal<any> ? ReturnType<T[K]> : never
49 }>,
50): void => {
51 expect(evaluateSignals(obj)).toMatchObject(expected)
52}
53β
54/**
55 * Reset Angular's TestBed and configure the standard TanStack Query providers for tests.
56 * Pass additional providers (including EnvironmentProviders) via the options argument.
57 */
58export function setupTanStackQueryTestBed(
59 queryClient: QueryClient,
60 options: { providers?: Array<Provider | EnvironmentProviders> } = {},
61) {
62 TestBed.resetTestingModule()
63 TestBed.configureTestingModule({
64 providers: [
65 provideAngularQueryChangeDetection(),
66 provideTanStackQuery(() => queryClient),
67 ...(options.providers ?? []),
68 ],
69 })
70}
71β
72/**
73 * Advances zero-delay timers and their promise continuations in fake-timer tests.
74 * Angular rendering may require a subsequent change-detection pass.
75 */
76export async function flushQueryUpdates() {
77 await vi.advanceTimersByTimeAsync(0)
78}