-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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 (post mounting) #24198
Comments
This was fixed in this PR #23596 and should be available in the next release. |
This comment was marked as off-topic.
This comment was marked as off-topic.
As far as I can see this is not the same issue (and still fails with Cypress 10.10.0) 🙂. If I modify the issue you're referring to, to this: import { Component, Input, OnChanges } from '@angular/core';
@Component({
selector: 'app-simple',
template: `<div data-cy="simple">ngOnChanges fired: {{ ngOnChangesFired }}</div>`,
})
export class SimpleComponent implements OnChanges {
@Input() name: string | undefined;
ngOnChangesFired = 0;
ngOnChanges(
): void {
this.ngOnChangesFired++;
}
} simple-component.component.cy.ts import { mount } from 'cypress/angular';
import { SimpleComponent } from './simple-component.component';
describe(`${SimpleComponent.name}`, () => {
// ❌ Fails
it('should fire ngOnChanges when using class syntax', () => {
mount(
SimpleComponent, { componentProperties: { name: 'Henk' } })
.then((instance) => {
const component = instance.fixture.componentInstance;
const fixture = instance.fixture;
fixture.detectChanges();
cy
.get('[data-cy=simple]')
.should('have.text', 'ngOnChanges fired: 1')
.then(() => {
component.name = 'Another Name';
fixture.detectChanges();
})
// 👇 Here
.get('[data-cy=simple]')
.should('have.text', 'ngOnChanges fired: 2')
;
});
});
}) Request to re-open @jordanpowell88 (or one of the others involved @ZachJW34 @mike-plummer)🙂? I'm still open to the possibility that it's just me missing something obvious (but the issue was just closed, and it sounds like the issue was misunderstood) 😊.. |
@rujorgensen Yes, this one is going to be reopened. |
Update: I migrated my NX project, and Cypress 11.1.0 still fails this test. |
Current behavior
ngOnChanges
does not seem to be called when the inputs of a component is changed post mounting.Desired behavior
ngOnChanges
should run whenever the inputs of a component changes.Test code to reproduce
classic-component-with-input.component.ts
classic-component-with-input.component.cy.ts
Cypress Version
10.9.011.1.0Node version
16.17.0
Operating System
Windows 10
Other
The code can be found in this NX repo too https://github.com/rujorgensen/nx-workspace, run
npm run test:components
to start component tests.I don't have a lot of experience with testing, so it might just be me missing something obvious 😬.
Thanks!
The text was updated successfully, but these errors were encountered: