Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autoDetectChanges not automatically detect changes as a "real" running component would do #492

Open
vladtheimpala opened this issue Oct 7, 2024 · 0 comments · May be fixed by #495
Open

Comments

@vladtheimpala
Copy link

vladtheimpala commented Oct 7, 2024

Issue
Reading the documentation of render method, autoDetectChanges option is supposed to take care of the change detection for us.

automatically detect changes as a "real" running component would do

But it seems it is not the case with Observable and async pipe at least we have to call detectChanges manually.

Example
Simple user component displaying username resolved from a UserService acting as a store
Testing the username change coming from the service is correctly rendered by the component

component under test :

@Component({
  selector: 'app-user',
  standalone: true,
  template: `<h1>{{ username$ | async }}</h1>`,
})
export class UserComponent {
  readonly username$: Observable<string> = inject(UserService).getName();
}

test :

describe('UserComponent', () => {
  it('should display username', async () => {

    // stubbed user service using a Subject
    const user = new BehaviorSubject({ name: 'username 1' });
    const userServiceStub: Partial<UserService> = {
      getName: () => user.asObservable().pipe(map(user => user.name))
    };

    // render the component with injection of the stubbed service
    const { detectChanges } = await render(UserComponent, {
      componentProviders: [
        {
          provide: UserService,
          useValue: userServiceStub
        }
      ]
    });

    // assert first username emitted is rendered
    expect(await screen.findByRole('heading')).toHaveTextContent('username 1');

    // emitting a second username
    user.next({ name: 'username 2' });

    detectChanges(); // => is required to make my test pass, would not be required in a "real" running component <= 

    // assert the second username is rendered
    expect(await screen.findByRole('heading')).toHaveTextContent('username 2');
  });
});
Screenshot 2024-10-07 at 19 06 22

Expected behavior
As written in the documentation, autoDetectChanges should handle changes detection as a real component would do

  • Ideally we want to avoid manually calling detectChanges() method when change detection would naturally occurs in a real browser context
    • ChangeDetectionStrategy of the component should be taken in account
  • Else document when autoDetectChanges really run change detection for us
timdeschryver added a commit that referenced this issue Oct 13, 2024
@timdeschryver timdeschryver linked a pull request Oct 13, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant