0

I have started writing unit test cases for my new angular project. Can anyone please help me to write test cases for service & Hostlisteners. I have written but both specs are failing.

1st spec error: Exected spy on subscribe to equal spy on userData.

2nd spec error: Cannot read properties of undefined(reading 'triggerEventHandler')

mycomponent.ts

                     export class myComponent imlements OnInt{
                        constructor(){
                           this.user();
                        }
                        user(){
                              setTimeOut(()=>{
                                 if(this.router.url != '/contact' && this.router.url !== '/about'){
                                    thhis.usersubscription = this.service.userData().subscribe((data)=>{
                                        if(data){
                                           this.router.navigate(['/welcomepage'])
                                        }
                                     })
                                 }
                              },300)
                        }

                        @HostListner('document:keydown',['$event'])
                        @HostListner('window:touchmove',['$event'])
                        getUser(){
                           this.user();
                        }
                     }

myComponent.spec.ts

                     let mock = {
                        name: 'abc',
                        id: 1234
                     };
                     let inputElement:DebugElement;
                     let fixture;
                     describe('myComponent',()=>{
                        beforeEach(waitForAsync(()=>{
                             TestBed.configureTestingModule({
                                  declaration:[myComponent],
                                  providers: [Service]
                              }).compileComponents());
                        }));

                        beforeEach(()=>{
                               fixture= Test.createComponent(myComponent);
                         })

                         it('should subscribe userdata',fakeAsync(()=>{
                              let service = TestBed.get(Service);
                              let userSpy = spyOn(service,'userData').and.returnValue(of(mock));
                              let subspy = spyOn(service.userData(), 'subscribe');
                              tick();
                              expect(subspy).toEqual(userSpy);
                         }))

                          it('call user method when keydown',()=>{
                             const component = fixture.componentInstance;
                              fixture.detectChange();
                              inputElement.triggerEventHandler('keydown',null);
                              expect(component.user()).toHaveBeenCalled();
                          })
                     })
Yashwanth
  • 181
  • 1
  • 3
  • 17
  • Why do you expect two different spies to be equal? That doesn't make sense. You probably want to compare their arguments, or check if both have been called (depending on what you want to check). And the second one is pretty obvious, `inputElement` is not initialized and therefore `undefined`. – JSON Derulo Jan 19 '22 at 11:10
  • Hi @Json Derulo, Thanks for your help. I have assign value inputElement=fixture.debugElement; but still test case is failing. user method is not calling. – Yashwanth Jan 20 '22 at 01:55
  • Does this answer your question? [Angular How to test @HostListener](https://stackoverflow.com/questions/48931164/angular-how-to-test-hostlistener) – Akxe Jan 20 '22 at 12:38
  • Hi @Akxe, it didn't work for me. – Yashwanth Jan 20 '22 at 12:51

0 Answers0