Skip to content

Commit 4ae65fa

Browse files
authored
test(angular-query-experimental/injectQueries): add test for 'combine' option (TanStack#10536)
1 parent cd6a274 commit 4ae65fa

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,66 @@ describe('injectQueries', () => {
8282
expect(results[2]).toMatchObject([{ data: 1 }, { data: 2 }])
8383
})
8484

85+
it('should return the combined result when combine is provided', async () => {
86+
const key1 = queryKey()
87+
const key2 = queryKey()
88+
const results: Array<Record<string, any>> = []
89+
90+
@Component({
91+
template: `
92+
<div>data: {{ combined().data.join(',') }}</div>
93+
<div>isPending: {{ combined().isPending }}</div>
94+
`,
95+
})
96+
class Page {
97+
combined = injectQueries(() => ({
98+
queries: [
99+
{
100+
queryKey: key1,
101+
queryFn: () => sleep(10).then(() => 1),
102+
},
103+
{
104+
queryKey: key2,
105+
queryFn: () => sleep(10).then(() => 2),
106+
},
107+
],
108+
combine: (queries) => ({
109+
data: queries.map((q) => q.data),
110+
isPending: queries.some((q) => q.isPending),
111+
}),
112+
}))
113+
114+
_ = effect(() => {
115+
results.push({ ...this.combined() })
116+
})
117+
}
118+
119+
const rendered = await render(Page, {
120+
providers: [
121+
provideZonelessChangeDetection(),
122+
provideTanStackQuery(queryClient),
123+
],
124+
})
125+
126+
expect(rendered.getByText('data: ,')).toBeInTheDocument()
127+
expect(rendered.getByText('isPending: true')).toBeInTheDocument()
128+
expect(results[0]).toMatchObject({
129+
data: [undefined, undefined],
130+
isPending: true,
131+
})
132+
133+
await vi.advanceTimersByTimeAsync(11)
134+
rendered.fixture.detectChanges()
135+
136+
expect(rendered.getByText('data: 1,2')).toBeInTheDocument()
137+
expect(rendered.getByText('isPending: false')).toBeInTheDocument()
138+
expect(results[results.length - 1]).toMatchObject({
139+
data: [1, 2],
140+
isPending: false,
141+
})
142+
expect(results.length).toBeGreaterThanOrEqual(2)
143+
})
144+
85145
describe('isRestoring', () => {
86146
it('should not fetch for the duration of the restoring period when isRestoring is true', async () => {
87147
const key1 = queryKey()

0 commit comments

Comments
 (0)