Angular Component Testing: ngOnChanges does not fire (post mounting)
See original GitHub issueCurrent 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
import { Component, Input, OnChanges } from '@angular/core';
@Component({
selector: 'app-classic-component-with-input',
template: `<p cy-data-id="el-number-list">{{ numberList }}</p><p cy-data-id="el-change-count">{{ __changesDetected }}</p>`
})
export class ClassicComponentWithInputComponent implements OnChanges {
@Input()
public numberList: number[] = [];
public __changesDetected: number = 0;
public ngOnChanges(
): void {
this.__changesDetected = this.__changesDetected + 1;
}
}
classic-component-with-input.component.cy.ts
import { ComponentFixture } from '@angular/core/testing';
import { mount } from 'cypress/angular';
import { ClassicComponentWithInputComponent } from './classic-component-with-input.component';
describe(ClassicComponentWithInputComponent.name, () => {
const config = {
declarations: [],
imports: [],
providers: []
};
let component: ClassicComponentWithInputComponent;
let fixture: ComponentFixture<ClassicComponentWithInputComponent>;
beforeEach((done) => {
mount(ClassicComponentWithInputComponent, {
...config,
})
.then((instance) => {
component = instance.fixture.componentInstance;
fixture = instance.fixture;
fixture.detectChanges();
done();
})
;
});
// ✅ Works!
it('detects input changes', () => {
// * Act #1
component.numberList = [1, 2, 3, 4];
fixture.detectChanges();
cy
// * Assertion #1
.get('[cy-data-id="el-number-list"]')
.should('have.text', '1,2,3,4')
.then(() => {
// * Act #2
component.numberList = [1, 2, 3];
fixture.detectChanges();
})
// * Assertion #2
.get('[cy-data-id="el-number-list"]')
.should('have.text', '1,2,3')
;
})
// ❌ Does not work
it('counts input changes', () => {
// Should + 1 to `__changesDetected`
component.numberList = [1, 2, 3, 4];
fixture.detectChanges();
cy
.get('[cy-data-id="el-change-count"]')
.should('have.text', '2')
;
})
})
Cypress Version
~10.9.0~ 11.1.0
Node 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!
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Angular Component Testing: ngOnChanges does not fire ...
Fast, easy and reliable testing for anything that runs in a browser. - Angular Component Testing: ngOnChanges does not fire (post mounting) ...
Read more >ngOnChanges not firing for nested object - Stack Overflow
Assign a new array to rawLapsData whenever you make any changes to the array contents. Then ngOnChanges() will be called because the array...
Read more >Testing your implementation of ngOnChanges in Angular ...
When you write a custom component in Angular (≥ 2.x) that updates its content whenever input changes you can add all necessary computations ......
Read more >Lifecycle hooks - Angular
Demonstrates how Angular calls the ngOnChanges() hook every time one of the component input properties changes, and shows how to interpret the changes...
Read more >Angular Testing In Depth: Components - Auth0
Learn how to test Components in Angular. We will start with writing isolated tests for a component and finish with integration tests for...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@rujorgensen Yes, this one is going to be reopened.
Update: I migrated my NX project, and Cypress 11.1.0 still fails this test.