Skip to content

Commit 67d1705

Browse files
authored
fix(angular-query): improve support for required signals (#8409)
1 parent 7f9f28e commit 67d1705

File tree

11 files changed

+238
-479
lines changed

11 files changed

+238
-479
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('injectMutationState', () => {
3030
})
3131

3232
describe('injectMutationState', () => {
33-
test('should return variables after calling mutate 1', async () => {
33+
test('should return variables after calling mutate 1', () => {
3434
const mutationKey = ['mutation']
3535
const variables = 'foo123'
3636

@@ -53,7 +53,7 @@ describe('injectMutationState', () => {
5353
expect(mutationState()).toEqual([variables])
5454
})
5555

56-
test('reactive options should update injectMutationState', async () => {
56+
test('reactive options should update injectMutationState', () => {
5757
const mutationKey1 = ['mutation1']
5858
const mutationKey2 = ['mutation2']
5959
const variables1 = 'foo123'
@@ -87,11 +87,10 @@ describe('injectMutationState', () => {
8787
expect(mutationState()).toEqual([variables1])
8888

8989
filterKey.set(mutationKey2)
90-
TestBed.flushEffects()
9190
expect(mutationState()).toEqual([variables2])
9291
})
9392

94-
test('should return variables after calling mutate 2', async () => {
93+
test('should return variables after calling mutate 2', () => {
9594
queryClient.clear()
9695
const mutationKey = ['mutation']
9796
const variables = 'bar234'
@@ -156,8 +155,6 @@ describe('injectMutationState', () => {
156155
const { debugElement } = fixture
157156
setFixtureSignalInputs(fixture, { name: fakeName })
158157

159-
fixture.detectChanges()
160-
161158
let spans = debugElement
162159
.queryAll(By.css('span'))
163160
.map((span) => span.nativeNode.textContent)

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('injectMutation', () => {
5151
})
5252
})
5353

54-
test('should change state after invoking mutate', async () => {
54+
test('should change state after invoking mutate', () => {
5555
const result = 'Mock data'
5656

5757
const mutation = TestBed.runInInjectionContext(() => {
@@ -60,6 +60,8 @@ describe('injectMutation', () => {
6060
}))
6161
})
6262

63+
TestBed.flushEffects()
64+
6365
mutation.mutate(result)
6466
vi.advanceTimersByTime(1)
6567

@@ -79,6 +81,7 @@ describe('injectMutation', () => {
7981
mutationFn: errorMutator,
8082
}))
8183
})
84+
8285
mutation.mutate({})
8386

8487
await resolveMutations()
@@ -129,8 +132,6 @@ describe('injectMutation', () => {
129132

130133
mutationKey.set(['2'])
131134

132-
TestBed.flushEffects()
133-
134135
mutation.mutate('xyz')
135136

136137
const mutations = mutationCache.find({ mutationKey: ['2'] })
@@ -405,6 +406,8 @@ describe('injectMutation', () => {
405406
}))
406407
})
407408

409+
TestBed.flushEffects()
410+
408411
mutate()
409412

410413
await resolveMutations()

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

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe('injectQuery', () => {
7878
const withResultInfer = TestBed.runInInjectionContext(() =>
7979
injectQuery(() => ({
8080
queryKey: key,
81-
queryFn: async () => true,
81+
queryFn: () => true,
8282
})),
8383
)
8484
expectTypeOf(withResultInfer.data()).toEqualTypeOf<boolean | undefined>()
@@ -263,8 +263,6 @@ describe('injectQuery', () => {
263263
expect(query.isFetching()).toBe(true)
264264
expect(query.isStale()).toBe(true)
265265
expect(query.isFetched()).toBe(false)
266-
267-
flush()
268266
}))
269267

270268
test('should resolve to success and update signal: injectQuery()', fakeAsync(() => {
@@ -275,7 +273,7 @@ describe('injectQuery', () => {
275273
}))
276274
})
277275

278-
flush()
276+
tick()
279277

280278
expect(query.status()).toBe('success')
281279
expect(query.data()).toBe('result2')
@@ -294,7 +292,7 @@ describe('injectQuery', () => {
294292
}))
295293
})
296294

297-
flush()
295+
tick()
298296

299297
expect(query.status()).toBe('error')
300298
expect(query.data()).toBe(undefined)
@@ -316,7 +314,7 @@ describe('injectQuery', () => {
316314
queryFn: spy,
317315
}))
318316
})
319-
flush()
317+
tick()
320318
expect(spy).toHaveBeenCalledTimes(1)
321319

322320
expect(query.status()).toBe('success')
@@ -331,7 +329,6 @@ describe('injectQuery', () => {
331329
queryKey: ['key8'],
332330
signal: expect.anything(),
333331
})
334-
flush()
335332
}))
336333

337334
test('should only run query once enabled signal is set to true', fakeAsync(() => {
@@ -350,8 +347,7 @@ describe('injectQuery', () => {
350347
expect(query.status()).toBe('pending')
351348

352349
enabled.set(true)
353-
TestBed.flushEffects()
354-
flush()
350+
tick()
355351
expect(spy).toHaveBeenCalledTimes(1)
356352
expect(query.status()).toBe('success')
357353
}))
@@ -381,7 +377,6 @@ describe('injectQuery', () => {
381377
expect(dependentQueryFn).not.toHaveBeenCalled()
382378

383379
tick()
384-
TestBed.flushEffects()
385380

386381
expect(query1.data()).toStrictEqual('Some data')
387382
expect(query2.fetchStatus()).toStrictEqual('fetching')
@@ -419,7 +414,7 @@ describe('injectQuery', () => {
419414
)
420415
})
421416

422-
flush()
417+
tick()
423418

424419
keySignal.set('key12')
425420

@@ -433,8 +428,6 @@ describe('injectQuery', () => {
433428
}),
434429
)
435430
})
436-
437-
flush()
438431
}))
439432

440433
describe('throwOnError', () => {
@@ -471,7 +464,6 @@ describe('injectQuery', () => {
471464
expect(() => {
472465
flush()
473466
}).toThrowError('Some error')
474-
flush()
475467
}))
476468

477469
test('should throw when throwOnError function returns true', fakeAsync(() => {
@@ -486,7 +478,6 @@ describe('injectQuery', () => {
486478
expect(() => {
487479
flush()
488480
}).toThrowError('Some error')
489-
flush()
490481
}))
491482
})
492483

@@ -501,12 +492,12 @@ describe('injectQuery', () => {
501492

502493
expect(query.status()).toBe('pending')
503494

504-
flush()
495+
tick()
505496

506497
expect(query.status()).toBe('error')
507498
}))
508499

509-
test('should render with required signal inputs', fakeAsync(async () => {
500+
test('should render with required signal inputs', fakeAsync(() => {
510501
@Component({
511502
selector: 'app-fake',
512503
template: `{{ query.data() }}`,
@@ -517,7 +508,7 @@ describe('injectQuery', () => {
517508

518509
query = injectQuery(() => ({
519510
queryKey: ['fake', this.name()],
520-
queryFn: () => Promise.resolve(this.name()),
511+
queryFn: () => this.name(),
521512
}))
522513
}
523514

@@ -526,10 +517,10 @@ describe('injectQuery', () => {
526517
name: 'signal-input-required-test',
527518
})
528519

529-
flush()
530520
fixture.detectChanges()
521+
tick()
531522

532-
expect(fixture.debugElement.nativeElement.textContent).toEqual(
523+
expect(fixture.componentInstance.query.data()).toEqual(
533524
'signal-input-required-test',
534525
)
535526
}))
@@ -565,13 +556,13 @@ describe('injectQuery', () => {
565556

566557
const fixture = TestBed.createComponent(FakeComponent)
567558
fixture.detectChanges()
568-
flush()
559+
tick()
569560

570561
expect(fixture.componentInstance.query.data()).toEqual('test name')
571562

572563
fixture.componentInstance.name.set('test name 2')
573564
fixture.detectChanges()
574-
flush()
565+
tick()
575566

576567
expect(fixture.componentInstance.query.data()).toEqual('test name 2')
577568
}))
@@ -608,13 +599,13 @@ describe('injectQuery', () => {
608599

609600
const fixture = TestBed.createComponent(FakeComponent)
610601
fixture.detectChanges()
611-
flush()
602+
tick()
612603

613604
expect(fixture.componentInstance.query.data()).toEqual('test name')
614605

615606
fixture.componentInstance.name.set('test name 2')
616607
fixture.detectChanges()
617-
flush()
608+
tick()
618609

619610
expect(fixture.componentInstance.query.data()).toEqual('test name 2')
620611
}))

packages/angular-query-experimental/src/__tests__/util/lazy-init/lazy-init.test.ts

Lines changed: 0 additions & 122 deletions
This file was deleted.

0 commit comments

Comments
 (0)