test(core): replace act with actAsync

Replaces the custom `act` helper function with the standardized `actAsync` utility

(cherry picked from commit 063eda2db2)
This commit is contained in:
SkyZeroZx
2026-09-01 12:18:06 -05:00
committed by Alex Rickabaugh
parent cc05deab72
commit ef48630a14
@@ -6,7 +6,8 @@
* found in the LICENSE file at https://angular.dev/license
*/
import {ApplicationRef, Injector, resource, ResourceParamsStatus, signal} from '../../src/core';
import {actAsync} from '@angular/private/testing';
import {Injector, resource, ResourceParamsStatus, signal} from '../../src/core';
import {TestBed} from '../../testing';
function throwStatusAndErrors<T>(source: () => T | ResourceParamsStatus | Error): () => T {
@@ -21,7 +22,7 @@ function throwStatusAndErrors<T>(source: () => T | ResourceParamsStatus | Error)
describe('resource with ResourceParamsStatus', () => {
it('should transition to idle when params throws ResourceParamsStatus.IDLE', async () => {
const s = signal<string | ResourceParamsStatus>('foo');
const res = await act(() =>
const res = await actAsync(() =>
resource({
params: throwStatusAndErrors(s),
loader: async ({params}) => {
@@ -34,7 +35,7 @@ describe('resource with ResourceParamsStatus', () => {
expect(res.status()).toBe('resolved');
expect(res.value()).toBe('foo');
await act(() => s.set(ResourceParamsStatus.IDLE));
await actAsync(() => s.set(ResourceParamsStatus.IDLE));
expect(res.status()).toBe('idle');
expect(res.value()).toBe(undefined);
@@ -43,7 +44,7 @@ describe('resource with ResourceParamsStatus', () => {
it('should transition to loading when params throws ResourceParamsStatus.LOADING', async () => {
const s = signal<string | ResourceParamsStatus>('foo');
let loadCount = 0;
const res = await act(() =>
const res = await actAsync(() =>
resource({
params: throwStatusAndErrors(s),
loader: async ({params}) => {
@@ -58,7 +59,7 @@ describe('resource with ResourceParamsStatus', () => {
expect(res.value()).toBe('foo');
expect(loadCount).toBe(1);
await act(() => s.set(ResourceParamsStatus.LOADING));
await actAsync(() => s.set(ResourceParamsStatus.LOADING));
expect(res.status()).toBe('loading');
expect(res.value()).toBe(undefined);
@@ -67,7 +68,7 @@ describe('resource with ResourceParamsStatus', () => {
it('should transition to error when params throws an Error', async () => {
const s = signal<string | Error>('foo');
const res = await act(() =>
const res = await actAsync(() =>
resource({
params: throwStatusAndErrors(s),
loader: async ({params}) => params as string,
@@ -78,7 +79,7 @@ describe('resource with ResourceParamsStatus', () => {
expect(res.status()).toBe('resolved');
const err = new Error('params error');
await act(() => s.set(err));
await actAsync(() => s.set(err));
expect(res.status()).toBe('error');
expect(res.error()).toEqual(err);
@@ -88,7 +89,7 @@ describe('resource with ResourceParamsStatus', () => {
it('should recover from special statuses', async () => {
const s = signal<string | ResourceParamsStatus | Error>(ResourceParamsStatus.IDLE);
let loadCount = 0;
const res = await act(() =>
const res = await actAsync(() =>
resource({
params: throwStatusAndErrors(s),
loader: async ({params}) => {
@@ -101,26 +102,20 @@ describe('resource with ResourceParamsStatus', () => {
expect(res.status()).toBe('idle');
await act(() => s.set(ResourceParamsStatus.LOADING));
await actAsync(() => s.set(ResourceParamsStatus.LOADING));
expect(res.status()).toBe('loading');
expect(loadCount).toBe(0);
await act(() => s.set(new Error('fail')));
await actAsync(() => s.set(new Error('fail')));
expect(res.status()).toBe('error');
expect(loadCount).toBe(0);
await act(() => s.set('foo'));
await actAsync(() => s.set('foo'));
expect(res.status()).toBe('resolved');
expect(res.value()).toBe('foo');
expect(loadCount).toBe(1);
});
});
async function act<T>(fn: () => T): Promise<T> {
const result = fn();
await TestBed.inject(ApplicationRef).whenStable();
return result;
}