Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TestBed } from '@angular/core/testing'
import { describe, expect, it } from 'vitest'
import { beforeEach, describe, expect, it } from 'vitest'
import { Injector, provideZonelessChangeDetection, signal } from '@angular/core'
import {
QueryClient,
Expand All @@ -11,16 +11,17 @@ import {
describe('injectIsRestoring', () => {
let queryClient: QueryClient

it('returns false by default when provideIsRestoring is not used', () => {
beforeEach(() => {
queryClient = new QueryClient()

TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
})
})

it('returns false by default when provideIsRestoring is not used', () => {
const isRestoring = TestBed.runInInjectionContext(() => {
return injectIsRestoring()
})
Expand All @@ -29,15 +30,10 @@ describe('injectIsRestoring', () => {
})

it('returns provided signal value when provideIsRestoring is used', () => {
queryClient = new QueryClient()
const restoringSignal = signal(true)

TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
provideIsRestoring(restoringSignal.asReadonly()),
],
providers: [provideIsRestoring(restoringSignal.asReadonly())],
})

const isRestoring = TestBed.runInInjectionContext(() => {
Expand All @@ -48,15 +44,6 @@ describe('injectIsRestoring', () => {
})

it('can be used outside injection context when passing an injector', () => {
queryClient = new QueryClient()

TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
})

const isRestoring = injectIsRestoring({
injector: TestBed.inject(Injector),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,14 +536,6 @@ describe('injectMutation', () => {
})

it('should handle synchronous mutation with retry', async () => {
TestBed.resetTestingModule()
TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
})

const app = TestBed.inject(ApplicationRef)
let attemptCount = 0

Expand Down Expand Up @@ -586,14 +578,6 @@ describe('injectMutation', () => {
})

it('should handle multiple synchronous mutations on same key', async () => {
TestBed.resetTestingModule()
TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
})

const app = TestBed.inject(ApplicationRef)
let callCount = 0

Expand Down Expand Up @@ -640,14 +624,6 @@ describe('injectMutation', () => {
})

it('should handle synchronous mutation with optimistic updates', async () => {
TestBed.resetTestingModule()
TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
})

const app = TestBed.inject(ApplicationRef)
const testQueryKey = queryKey()
let onMutateCalled = false
Expand Down Expand Up @@ -692,14 +668,6 @@ describe('injectMutation', () => {
})

it('should handle synchronous mutation cancellation', async () => {
TestBed.resetTestingModule()
TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
})

const app = TestBed.inject(ApplicationRef)

const key = queryKey()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ import { queryKey, sleep } from '@tanstack/query-test-utils'
import { QueryClient, provideIsRestoring, provideTanStackQuery } from '..'
import { injectQueries } from '../inject-queries'

let queryClient: QueryClient

beforeEach(() => {
vi.useFakeTimers()
queryClient = new QueryClient()
TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
describe('injectQueries', () => {
let queryClient: QueryClient

beforeEach(() => {
vi.useFakeTimers()
queryClient = new QueryClient()
TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
})
})
})

afterEach(() => {
vi.useRealTimers()
})
afterEach(() => {
vi.useRealTimers()
})

describe('injectQueries', () => {
it('should return the correct states', async () => {
const key1 = queryKey()
const key2 = queryKey()
Expand Down Expand Up @@ -143,13 +143,8 @@ describe('injectQueries', () => {
const queryFn1 = vi.fn().mockImplementation(() => sleep(10).then(() => 1))
const queryFn2 = vi.fn().mockImplementation(() => sleep(10).then(() => 2))

TestBed.resetTestingModule()
TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
provideIsRestoring(signal(true).asReadonly()),
],
providers: [provideIsRestoring(signal(true).asReadonly())],
})

const queries = TestBed.runInInjectionContext(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import type { CreateQueryOptions, OmitKeyof, QueryFunction } from '..'
describe('injectQuery', () => {
let queryCache: QueryCache
let queryClient: QueryClient

beforeEach(() => {
vi.useFakeTimers()
queryCache = new QueryCache()
Expand Down Expand Up @@ -566,13 +567,8 @@ describe('injectQuery', () => {
.fn()
.mockImplementation(() => sleep(10).then(() => 'data'))

TestBed.resetTestingModule()
TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
provideIsRestoring(signal(true).asReadonly()),
],
providers: [provideIsRestoring(signal(true).asReadonly())],
})

const query = TestBed.runInInjectionContext(() =>
Expand Down Expand Up @@ -645,14 +641,8 @@ describe('injectQuery', () => {
})

it('should complete HttpClient-based queries before whenStable() resolves', async () => {
TestBed.resetTestingModule()
TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
provideHttpClient(),
provideHttpClientTesting(),
],
providers: [provideHttpClient(), provideHttpClientTesting()],
})

const app = TestBed.inject(ApplicationRef)
Expand Down Expand Up @@ -691,14 +681,6 @@ describe('injectQuery', () => {
})

it('should handle synchronous queryFn with staleTime', async () => {
TestBed.resetTestingModule()
TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
})

const app = TestBed.inject(ApplicationRef)
let callCount = 0

Expand Down Expand Up @@ -735,14 +717,6 @@ describe('injectQuery', () => {
})

it('should handle enabled/disabled transitions with synchronous queryFn', async () => {
TestBed.resetTestingModule()
TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
})

const app = TestBed.inject(ApplicationRef)
const enabledSignal = signal(false)
let callCount = 0
Expand Down Expand Up @@ -777,14 +751,6 @@ describe('injectQuery', () => {
})

it('should handle query invalidation with synchronous data', async () => {
TestBed.resetTestingModule()
TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
})

const app = TestBed.inject(ApplicationRef)
const testKey = queryKey()
let callCount = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,8 @@ describe('PendingTasks Integration', () => {

describe('HttpClient Integration', () => {
beforeEach(() => {
TestBed.resetTestingModule()
TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
provideHttpClient(),
provideHttpClientTesting(),
],
providers: [provideHttpClient(), provideHttpClientTesting()],
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ import type {
Persister,
} from '@tanstack/query-persist-client-core'

beforeEach(() => {
vi.useFakeTimers()
})

afterEach(() => {
vi.useRealTimers()
})

const createMockPersister = (): Persister => {
let storedState: PersistedClient | undefined

Expand Down Expand Up @@ -62,6 +54,14 @@ const createMockErrorPersister = (
}

describe('withPersistQueryClient', () => {
beforeEach(() => {
vi.useFakeTimers()
})

afterEach(() => {
vi.useRealTimers()
})

it('restores cache from persister', async () => {
const key = queryKey()
const states: Array<{
Expand Down
Loading