Skip to content

Commit 99b6d01

Browse files
committed
Ensure unit tests are run using component effect scheduling
1 parent c534ca2 commit 99b6d01

File tree

6 files changed

+548
-316
lines changed

6 files changed

+548
-316
lines changed

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

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { TestBed } from '@angular/core/testing'
22
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
3-
import { Injector, provideZonelessChangeDetection } from '@angular/core'
3+
import {
4+
ChangeDetectionStrategy,
5+
Component,
6+
Injector,
7+
provideZonelessChangeDetection,
8+
} from '@angular/core'
49
import { sleep } from '@tanstack/query-test-utils'
5-
import { QueryClient, injectInfiniteQuery, provideTanStackQuery } from '..'
10+
import { injectInfiniteQuery, provideTanStackQuery, QueryClient } from '..'
611
import { expectSignals } from './test-utils'
712

813
describe('injectInfiniteQuery', () => {
@@ -24,15 +29,25 @@ describe('injectInfiniteQuery', () => {
2429
})
2530

2631
test('should properly execute infinite query', async () => {
27-
const query = TestBed.runInInjectionContext(() => {
28-
return injectInfiniteQuery(() => ({
32+
@Component({
33+
selector: 'app-test',
34+
template: '',
35+
standalone: true,
36+
changeDetection: ChangeDetectionStrategy.OnPush,
37+
})
38+
class TestComponent {
39+
query = injectInfiniteQuery(() => ({
2940
queryKey: ['infiniteQuery'],
3041
queryFn: ({ pageParam }) =>
3142
sleep(10).then(() => 'data on page ' + pageParam),
3243
initialPageParam: 0,
3344
getNextPageParam: () => 12,
3445
}))
35-
})
46+
}
47+
48+
const fixture = TestBed.createComponent(TestComponent)
49+
fixture.detectChanges()
50+
const query = fixture.componentInstance.query
3651

3752
expectSignals(query, {
3853
data: undefined,
@@ -76,18 +91,32 @@ describe('injectInfiniteQuery', () => {
7691
})
7792

7893
test('can be used outside injection context when passing an injector', () => {
79-
const query = injectInfiniteQuery(
80-
() => ({
81-
queryKey: ['manualInjector'],
82-
queryFn: ({ pageParam }) =>
83-
sleep(0).then(() => 'data on page ' + pageParam),
84-
initialPageParam: 0,
85-
getNextPageParam: () => 12,
86-
}),
87-
{
88-
injector: TestBed.inject(Injector),
89-
},
90-
)
94+
const injector = TestBed.inject(Injector)
95+
96+
@Component({
97+
selector: 'app-test',
98+
template: '',
99+
standalone: true,
100+
changeDetection: ChangeDetectionStrategy.OnPush,
101+
})
102+
class TestComponent {
103+
query = injectInfiniteQuery(
104+
() => ({
105+
queryKey: ['manualInjector'],
106+
queryFn: ({ pageParam }) =>
107+
sleep(0).then(() => 'data on page ' + pageParam),
108+
initialPageParam: 0,
109+
getNextPageParam: () => 12,
110+
}),
111+
{
112+
injector: injector,
113+
},
114+
)
115+
}
116+
117+
const fixture = TestBed.createComponent(TestComponent)
118+
fixture.detectChanges()
119+
const query = fixture.componentInstance.query
91120

92121
expect(query.status()).toBe('pending')
93122
})

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ChangeDetectionStrategy,
23
Component,
34
Injector,
45
input,
@@ -145,6 +146,7 @@ describe('injectMutationState', () => {
145146
<span>{{ mutation.status }}</span>
146147
}
147148
`,
149+
changeDetection: ChangeDetectionStrategy.OnPush,
148150
})
149151
class FakeComponent {
150152
name = input.required<string>()

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
ApplicationRef,
3+
ChangeDetectionStrategy,
34
Component,
45
Injector,
56
input,
@@ -307,6 +308,7 @@ describe('injectMutation', () => {
307308
<button (click)="mutate()"></button>
308309
<span>{{ mutation.data() }}</span>
309310
`,
311+
changeDetection: ChangeDetectionStrategy.OnPush,
310312
})
311313
class FakeComponent {
312314
name = input.required<string>()
@@ -347,6 +349,7 @@ describe('injectMutation', () => {
347349
<button (click)="mutate()"></button>
348350
<span>{{ mutation.data() }}</span>
349351
`,
352+
changeDetection: ChangeDetectionStrategy.OnPush,
350353
})
351354
class FakeComponent {
352355
name = input.required<string>()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { beforeEach, describe, expect, it } from 'vitest'
22
import { render } from '@testing-library/angular'
33
import {
4+
ChangeDetectionStrategy,
45
Component,
56
effect,
67
provideZonelessChangeDetection,
@@ -37,6 +38,7 @@ describe('injectQueries', () => {
3738
</div>
3839
</div>
3940
`,
41+
changeDetection: ChangeDetectionStrategy.OnPush,
4042
})
4143
class Page {
4244
toString(val: any) {

0 commit comments

Comments
 (0)