I am trying to write unit test for my component but I am unable to change my input value inside afterNextRender
// In component code
constructor() {
afterNextRender({
write: () => {
if (this.disabled()) {
this.myStream.next('hello');
}
}
});
}
// in unit test
it('should call the function when disabled is true', async () => {
fixture.componentRef.setInput('disabled', true);
await firstValueFrom(component.myStream);
expect(component.disabled()).toEqual(true);
});
The test waits forever then fails. it seems that afterNextRender
is executed in the constructor as soon as the component is created in the unit test, the input is false at that time, I need to be able to set the input first then call it to execute, similar to how we do with ngOnInit
for instance. how can I do this test with afterNextRender
?
You have to mock the method as true, through component inheritance.
The constructor is called, on the line fixture = TestBed.createComponent(TestingComponent);
, so the spyOn, is too late, which might be the issue.
You can simply convert the subject
to a BehaviourSubject
, through inheritance and just for testing this one particular scenario.
After doing this test case, you can continue using AppComponent
, Since subject is not possible to subscribe when the code is in the constructor.
Also one thing to note, is that the afterNextRender
will execute immediately when working on a non SSR context. Hence you are facing so many difficulties.
Spec TS:
import {
TestBed,
ComponentFixture,
waitForAsync,
fakeAsync,
flush,
} from '@angular/core/testing';
import { AppComponent } from './app.component';
import { AppModule } from './app.module';
import { BehaviorSubject, firstValueFrom } from 'rxjs';
import { Component, input } from '@angular/core';
@Component({
// ... // <- values similar to your component
selector: 'my-app',
template: '',
})
export class AppComponentTesting extends AppComponent {
isDisabled: any = input(true);
myStream = new BehaviorSubject(null);
constructor() {
// ... // <- same values as YourComponent.
super(); // <- add args if needed. But your properties must be protected and not private.
}
}
describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
let output;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [AppModule],
declarations: [AppComponentTesting],
}).compileComponents();
});
// in unit test
it('should call the function when disabled is true', fakeAsync(() => {
const fixture: ComponentFixture<AppComponentTesting> =
TestBed.createComponent(AppComponentTesting);
const component: AppComponentTesting = fixture.componentInstance;
fixture.componentRef.setInput('disabled', true);
spyOn(component.myStream, 'next');
fixture.detectChanges();
flush();
expect(component.myStream.value).toEqual('hello');
}));
});
TS:
import { Component, afterNextRender, input } from '@angular/core';
import { Subject } from 'rxjs';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
})
export class AppComponent {
disabled = input(true);
myStream = new Subject();
constructor() {
afterNextRender({
write: () => {
console.log('qwer', this.disabled());
if (this.disabled()) {
console.log('qwer1', this.disabled());
this.myStream.next('hello');
}
},
});
}
}
Stackblitz Demo
4