Skip to content

Commit b79bb92

Browse files
authored
test(angular-query*): simplify 'TestBed' setup by removing redundant 'resetTestingModule' and leveraging provider merging (#10546)
1 parent ad3bc84 commit b79bb92

6 files changed

Lines changed: 33 additions & 123 deletions

File tree

packages/angular-query-experimental/src/__tests__/inject-is-restoring.test.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TestBed } from '@angular/core/testing'
2-
import { describe, expect, it } from 'vitest'
2+
import { beforeEach, describe, expect, it } from 'vitest'
33
import { Injector, provideZonelessChangeDetection, signal } from '@angular/core'
44
import {
55
QueryClient,
@@ -11,16 +11,17 @@ import {
1111
describe('injectIsRestoring', () => {
1212
let queryClient: QueryClient
1313

14-
it('returns false by default when provideIsRestoring is not used', () => {
14+
beforeEach(() => {
1515
queryClient = new QueryClient()
16-
1716
TestBed.configureTestingModule({
1817
providers: [
1918
provideZonelessChangeDetection(),
2019
provideTanStackQuery(queryClient),
2120
],
2221
})
22+
})
2323

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

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

3535
TestBed.configureTestingModule({
36-
providers: [
37-
provideZonelessChangeDetection(),
38-
provideTanStackQuery(queryClient),
39-
provideIsRestoring(restoringSignal.asReadonly()),
40-
],
36+
providers: [provideIsRestoring(restoringSignal.asReadonly())],
4137
})
4238

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

5046
it('can be used outside injection context when passing an injector', () => {
51-
queryClient = new QueryClient()
52-
53-
TestBed.configureTestingModule({
54-
providers: [
55-
provideZonelessChangeDetection(),
56-
provideTanStackQuery(queryClient),
57-
],
58-
})
59-
6047
const isRestoring = injectIsRestoring({
6148
injector: TestBed.inject(Injector),
6249
})

packages/angular-query-experimental/src/__tests__/inject-mutation.test.ts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -536,14 +536,6 @@ describe('injectMutation', () => {
536536
})
537537

538538
it('should handle synchronous mutation with retry', async () => {
539-
TestBed.resetTestingModule()
540-
TestBed.configureTestingModule({
541-
providers: [
542-
provideZonelessChangeDetection(),
543-
provideTanStackQuery(queryClient),
544-
],
545-
})
546-
547539
const app = TestBed.inject(ApplicationRef)
548540
let attemptCount = 0
549541

@@ -586,14 +578,6 @@ describe('injectMutation', () => {
586578
})
587579

588580
it('should handle multiple synchronous mutations on same key', async () => {
589-
TestBed.resetTestingModule()
590-
TestBed.configureTestingModule({
591-
providers: [
592-
provideZonelessChangeDetection(),
593-
provideTanStackQuery(queryClient),
594-
],
595-
})
596-
597581
const app = TestBed.inject(ApplicationRef)
598582
let callCount = 0
599583

@@ -640,14 +624,6 @@ describe('injectMutation', () => {
640624
})
641625

642626
it('should handle synchronous mutation with optimistic updates', async () => {
643-
TestBed.resetTestingModule()
644-
TestBed.configureTestingModule({
645-
providers: [
646-
provideZonelessChangeDetection(),
647-
provideTanStackQuery(queryClient),
648-
],
649-
})
650-
651627
const app = TestBed.inject(ApplicationRef)
652628
const testQueryKey = queryKey()
653629
let onMutateCalled = false
@@ -692,14 +668,6 @@ describe('injectMutation', () => {
692668
})
693669

694670
it('should handle synchronous mutation cancellation', async () => {
695-
TestBed.resetTestingModule()
696-
TestBed.configureTestingModule({
697-
providers: [
698-
provideZonelessChangeDetection(),
699-
provideTanStackQuery(queryClient),
700-
],
701-
})
702-
703671
const app = TestBed.inject(ApplicationRef)
704672

705673
const key = queryKey()

packages/angular-query-experimental/src/__tests__/inject-queries.test.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ import { queryKey, sleep } from '@tanstack/query-test-utils'
1111
import { QueryClient, provideIsRestoring, provideTanStackQuery } from '..'
1212
import { injectQueries } from '../inject-queries'
1313

14-
let queryClient: QueryClient
15-
16-
beforeEach(() => {
17-
vi.useFakeTimers()
18-
queryClient = new QueryClient()
19-
TestBed.configureTestingModule({
20-
providers: [
21-
provideZonelessChangeDetection(),
22-
provideTanStackQuery(queryClient),
23-
],
14+
describe('injectQueries', () => {
15+
let queryClient: QueryClient
16+
17+
beforeEach(() => {
18+
vi.useFakeTimers()
19+
queryClient = new QueryClient()
20+
TestBed.configureTestingModule({
21+
providers: [
22+
provideZonelessChangeDetection(),
23+
provideTanStackQuery(queryClient),
24+
],
25+
})
2426
})
25-
})
2627

27-
afterEach(() => {
28-
vi.useRealTimers()
29-
})
28+
afterEach(() => {
29+
vi.useRealTimers()
30+
})
3031

31-
describe('injectQueries', () => {
3232
it('should return the correct states', async () => {
3333
const key1 = queryKey()
3434
const key2 = queryKey()
@@ -143,13 +143,8 @@ describe('injectQueries', () => {
143143
const queryFn1 = vi.fn().mockImplementation(() => sleep(10).then(() => 1))
144144
const queryFn2 = vi.fn().mockImplementation(() => sleep(10).then(() => 2))
145145

146-
TestBed.resetTestingModule()
147146
TestBed.configureTestingModule({
148-
providers: [
149-
provideZonelessChangeDetection(),
150-
provideTanStackQuery(queryClient),
151-
provideIsRestoring(signal(true).asReadonly()),
152-
],
147+
providers: [provideIsRestoring(signal(true).asReadonly())],
153148
})
154149

155150
const queries = TestBed.runInInjectionContext(() =>

packages/angular-query-experimental/src/__tests__/inject-query.test.ts

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import type { CreateQueryOptions, OmitKeyof, QueryFunction } from '..'
3838
describe('injectQuery', () => {
3939
let queryCache: QueryCache
4040
let queryClient: QueryClient
41+
4142
beforeEach(() => {
4243
vi.useFakeTimers()
4344
queryCache = new QueryCache()
@@ -566,13 +567,8 @@ describe('injectQuery', () => {
566567
.fn()
567568
.mockImplementation(() => sleep(10).then(() => 'data'))
568569

569-
TestBed.resetTestingModule()
570570
TestBed.configureTestingModule({
571-
providers: [
572-
provideZonelessChangeDetection(),
573-
provideTanStackQuery(queryClient),
574-
provideIsRestoring(signal(true).asReadonly()),
575-
],
571+
providers: [provideIsRestoring(signal(true).asReadonly())],
576572
})
577573

578574
const query = TestBed.runInInjectionContext(() =>
@@ -645,14 +641,8 @@ describe('injectQuery', () => {
645641
})
646642

647643
it('should complete HttpClient-based queries before whenStable() resolves', async () => {
648-
TestBed.resetTestingModule()
649644
TestBed.configureTestingModule({
650-
providers: [
651-
provideZonelessChangeDetection(),
652-
provideTanStackQuery(queryClient),
653-
provideHttpClient(),
654-
provideHttpClientTesting(),
655-
],
645+
providers: [provideHttpClient(), provideHttpClientTesting()],
656646
})
657647

658648
const app = TestBed.inject(ApplicationRef)
@@ -691,14 +681,6 @@ describe('injectQuery', () => {
691681
})
692682

693683
it('should handle synchronous queryFn with staleTime', async () => {
694-
TestBed.resetTestingModule()
695-
TestBed.configureTestingModule({
696-
providers: [
697-
provideZonelessChangeDetection(),
698-
provideTanStackQuery(queryClient),
699-
],
700-
})
701-
702684
const app = TestBed.inject(ApplicationRef)
703685
let callCount = 0
704686

@@ -735,14 +717,6 @@ describe('injectQuery', () => {
735717
})
736718

737719
it('should handle enabled/disabled transitions with synchronous queryFn', async () => {
738-
TestBed.resetTestingModule()
739-
TestBed.configureTestingModule({
740-
providers: [
741-
provideZonelessChangeDetection(),
742-
provideTanStackQuery(queryClient),
743-
],
744-
})
745-
746720
const app = TestBed.inject(ApplicationRef)
747721
const enabledSignal = signal(false)
748722
let callCount = 0
@@ -777,14 +751,6 @@ describe('injectQuery', () => {
777751
})
778752

779753
it('should handle query invalidation with synchronous data', async () => {
780-
TestBed.resetTestingModule()
781-
TestBed.configureTestingModule({
782-
providers: [
783-
provideZonelessChangeDetection(),
784-
provideTanStackQuery(queryClient),
785-
],
786-
})
787-
788754
const app = TestBed.inject(ApplicationRef)
789755
const testKey = queryKey()
790756
let callCount = 0

packages/angular-query-experimental/src/__tests__/pending-tasks.test.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -458,14 +458,8 @@ describe('PendingTasks Integration', () => {
458458

459459
describe('HttpClient Integration', () => {
460460
beforeEach(() => {
461-
TestBed.resetTestingModule()
462461
TestBed.configureTestingModule({
463-
providers: [
464-
provideZonelessChangeDetection(),
465-
provideTanStackQuery(queryClient),
466-
provideHttpClient(),
467-
provideHttpClientTesting(),
468-
],
462+
providers: [provideHttpClient(), provideHttpClientTesting()],
469463
})
470464
})
471465

packages/angular-query-persist-client/src/__tests__/with-persist-query-client.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@ import type {
1818
Persister,
1919
} from '@tanstack/query-persist-client-core'
2020

21-
beforeEach(() => {
22-
vi.useFakeTimers()
23-
})
24-
25-
afterEach(() => {
26-
vi.useRealTimers()
27-
})
28-
2921
const createMockPersister = (): Persister => {
3022
let storedState: PersistedClient | undefined
3123

@@ -62,6 +54,14 @@ const createMockErrorPersister = (
6254
}
6355

6456
describe('withPersistQueryClient', () => {
57+
beforeEach(() => {
58+
vi.useFakeTimers()
59+
})
60+
61+
afterEach(() => {
62+
vi.useRealTimers()
63+
})
64+
6565
it('restores cache from persister', async () => {
6666
const key = queryKey()
6767
const states: Array<{

0 commit comments

Comments
 (0)