I am facing an issue when testing this code:
constructor(
private valuePairService: ValuePairService,
private sanitizer: DomSanitizer,
private service: Service,
private router: Router,
private route: ActivatedRoute
) {}
ngOnInit(): void {
this.route.data.subscribe(({ result }) => {
this.video =
result.value === undefined
? '//player.vimeo.com/video/65498532'
: result.value;
});
}
In my test I have this provider added:
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [RouterTestingModule, HttpClientTestingModule],
providers: [
{
provide: ActivatedRoute,
useValue: { data: of({ value: 'test'}) }
},
],
}).compileComponents();
router = TestBed.inject(Router);
});
Any idea about how to test it? I am having this error when executing the test:
error properties: Object({ longStack: 'TypeError: Cannot read property 'value' of undefined
UPDATE:
The problem was that I was testing the observable in the wrong way. I was using "value" instead of "result". The solution in the unit test is as follows:
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [RouterTestingModule, HttpClientTestingModule],
providers: [
{
provide: ActivatedRoute,
useValue: { data: of({ result: 'test'}) }
},
],
}).compileComponents();
router = TestBed.inject(Router);
});