Skip to content

Commit 063eda2

Browse files
SkyZeroZxalxhub
authored andcommitted
test(core): replace act with actAsync
Replaces the custom `act` helper function with the standardized `actAsync` utility
1 parent 318fefa commit 063eda2

1 file changed

Lines changed: 12 additions & 17 deletions

File tree

‎packages/core/test/resource/params_status_spec.ts‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {ApplicationRef, Injector, resource, ResourceParamsStatus, signal} from '../../src/core';
9+
import {actAsync} from '@angular/private/testing';
10+
import {Injector, resource, ResourceParamsStatus, signal} from '../../src/core';
1011
import {TestBed} from '../../testing';
1112

1213
function throwStatusAndErrors<T>(source: () => T | ResourceParamsStatus | Error): () => T {
@@ -21,7 +22,7 @@ function throwStatusAndErrors<T>(source: () => T | ResourceParamsStatus | Error)
2122
describe('resource with ResourceParamsStatus', () => {
2223
it('should transition to idle when params throws ResourceParamsStatus.IDLE', async () => {
2324
const s = signal<string | ResourceParamsStatus>('foo');
24-
const res = await act(() =>
25+
const res = await actAsync(() =>
2526
resource({
2627
params: throwStatusAndErrors(s),
2728
loader: async ({params}) => {
@@ -34,7 +35,7 @@ describe('resource with ResourceParamsStatus', () => {
3435
expect(res.status()).toBe('resolved');
3536
expect(res.value()).toBe('foo');
3637

37-
await act(() => s.set(ResourceParamsStatus.IDLE));
38+
await actAsync(() => s.set(ResourceParamsStatus.IDLE));
3839

3940
expect(res.status()).toBe('idle');
4041
expect(res.value()).toBe(undefined);
@@ -43,7 +44,7 @@ describe('resource with ResourceParamsStatus', () => {
4344
it('should transition to loading when params throws ResourceParamsStatus.LOADING', async () => {
4445
const s = signal<string | ResourceParamsStatus>('foo');
4546
let loadCount = 0;
46-
const res = await act(() =>
47+
const res = await actAsync(() =>
4748
resource({
4849
params: throwStatusAndErrors(s),
4950
loader: async ({params}) => {
@@ -58,7 +59,7 @@ describe('resource with ResourceParamsStatus', () => {
5859
expect(res.value()).toBe('foo');
5960
expect(loadCount).toBe(1);
6061

61-
await act(() => s.set(ResourceParamsStatus.LOADING));
62+
await actAsync(() => s.set(ResourceParamsStatus.LOADING));
6263

6364
expect(res.status()).toBe('loading');
6465
expect(res.value()).toBe(undefined);
@@ -67,7 +68,7 @@ describe('resource with ResourceParamsStatus', () => {
6768

6869
it('should transition to error when params throws an Error', async () => {
6970
const s = signal<string | Error>('foo');
70-
const res = await act(() =>
71+
const res = await actAsync(() =>
7172
resource({
7273
params: throwStatusAndErrors(s),
7374
loader: async ({params}) => params as string,
@@ -78,7 +79,7 @@ describe('resource with ResourceParamsStatus', () => {
7879
expect(res.status()).toBe('resolved');
7980

8081
const err = new Error('params error');
81-
await act(() => s.set(err));
82+
await actAsync(() => s.set(err));
8283

8384
expect(res.status()).toBe('error');
8485
expect(res.error()).toEqual(err);
@@ -88,7 +89,7 @@ describe('resource with ResourceParamsStatus', () => {
8889
it('should recover from special statuses', async () => {
8990
const s = signal<string | ResourceParamsStatus | Error>(ResourceParamsStatus.IDLE);
9091
let loadCount = 0;
91-
const res = await act(() =>
92+
const res = await actAsync(() =>
9293
resource({
9394
params: throwStatusAndErrors(s),
9495
loader: async ({params}) => {
@@ -101,26 +102,20 @@ describe('resource with ResourceParamsStatus', () => {
101102

102103
expect(res.status()).toBe('idle');
103104

104-
await act(() => s.set(ResourceParamsStatus.LOADING));
105+
await actAsync(() => s.set(ResourceParamsStatus.LOADING));
105106

106107
expect(res.status()).toBe('loading');
107108
expect(loadCount).toBe(0);
108109

109-
await act(() => s.set(new Error('fail')));
110+
await actAsync(() => s.set(new Error('fail')));
110111

111112
expect(res.status()).toBe('error');
112113
expect(loadCount).toBe(0);
113114

114-
await act(() => s.set('foo'));
115+
await actAsync(() => s.set('foo'));
115116

116117
expect(res.status()).toBe('resolved');
117118
expect(res.value()).toBe('foo');
118119
expect(loadCount).toBe(1);
119120
});
120121
});
121-
122-
async function act<T>(fn: () => T): Promise<T> {
123-
const result = fn();
124-
await TestBed.inject(ApplicationRef).whenStable();
125-
return result;
126-
}

0 commit comments

Comments
 (0)