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

Angular Component Testing: ngOnChanges does not fire when using Class syntax #23591

Closed
Waterstraal opened this issue Aug 29, 2022 · 2 comments · Fixed by #23596
Closed

Angular Component Testing: ngOnChanges does not fire when using Class syntax #23591

Waterstraal opened this issue Aug 29, 2022 · 2 comments · Fixed by #23596
Assignees
Labels
CT Issue related to component testing

Comments

@Waterstraal
Copy link

Waterstraal commented Aug 29, 2022

Current behavior

When using the Class syntax in a Cypress Component test, ngOnChanges does not fire.,

Desired behavior

ngOnChanges should always fire when an input property is set.

Test code to reproduce

Create a component which implements OnChanges and make it set a flag which we display in the template:

import { Component, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core';

@Component({
  selector: 'app-simple',
  template: `<div data-cy="simple">Hi {{ name }}. ngOnChanges fired: {{ ngOnChangesFired }}</div>`,
})
export class SimpleComponent implements OnChanges {
  @Input() name: string | undefined;

  ngOnChangesFired = false;

  ngOnChanges(changes: SimpleChanges): void {
    this.ngOnChangesFired = true;
  }
}

Create a Cypress component test and assert the output:

import { SimpleComponent } from './simple.component';

describe(`${SimpleComponent.name}`, () => {

  // This succeeds
  it('should fire ngOnChanges when using template syntax', () => {
    cy.mount(`<app-simple [name]="name"></app-simple>`, {declarations: [SimpleComponent], componentProperties: {name: 'Henk'}});

    cy.get('[data-cy=simple]').should('have.text', 'Hi Henk. ngOnChanges fired: true');
  });

  // This fails
  it('should fire ngOnChanges when using class syntax', () => {
    cy.mount(SimpleComponent, {componentProperties: {name: 'Henk'}});

    cy.get('[data-cy=simple]').should('have.text', 'Hi Henk. ngOnChanges fired: true');
  });

})

The first test succeeds (template syntax), and the second test fails (class syntax).

Cypress Version

10.6.0

Node version

16.15.1

Operating System

Windows 10

Debug Logs

No response

Other

Angular 13.3.11

@mike-plummer mike-plummer self-assigned this Aug 29, 2022
@ZachJW34
Copy link
Contributor

ngOnChanges makes sense to fire immediately after mounting, coupled with a detectChanges call. Thanks for posting @Waterstraal.

A workaround (before we get a fix for this) is to handle it manually, so something like:

cy.mount(SimpleComponent, { componentProperties: { name: 'Henk' } }).then(
  ({ fixture }) => {
    fixture.componentInstance.ngOnChanges({});
    fixture.detectChanges();
  }
);

You could even make the custom mount in cypress/support/component.ts handle this for all components.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Oct 11, 2022

Released in 10.10.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v10.10.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Oct 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CT Issue related to component testing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants