Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Angular unit test cases for subscribe and hostlistners

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();
                          })
                     })
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!