mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
test(core): replace act with actAsync
Replaces the custom `act` helper function with the standardized `actAsync` utility
This commit is contained in:
committed by
Alex Rickabaugh
parent
318fefad30
commit
063eda2db2
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user