Tanzeel
Tanzeel

Reputation: 5008

How to unit test a method that takes event as parameter [Angular/Jasmine]

I'm a beginner in Unit testing in Angular using Jasmine and Karma. My template is:

<p-overlayPanel #op>
  <div>
    ...  
  </div>
</p-overlayPanel>

<button (click)="closeTimeselector($event, op)">
  Close
</button>

Note: p-overlayPanel is primeng's component.

Here's my logic:

closeTimeselector(event, element) {
    element.hide(event);
}

I've to check when Close button is pressed from the template, the above method should be called and it should work as expected.

Here's my spec file:

import { TestBed, async, ComponentFixture } from '@angular/core/testing';
import { ...} from './timeselector.component';
...

fdescribe('Timeselector', () => {
   ...
  let fixture: ComponentFixture<TimeselectorComponent>;
  let component: TimeselectorComponent;
  let mockTranslateService;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
        imports: [
            ...
        ],
        declarations: [TimeselectorComponent],
        providers: [

        ],
        schemas: [CUSTOM_ELEMENTS_SCHEMA]
    }).compileComponents();
  }));


  beforeEach(() => {
    ...
    fixture=TestBed.createComponent(TimeselectorComponent);
    component=fixture.componentInstance;
    timeselector=new TimeselectorComponent(mockTranslateService);
  })

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('...', () => {
    ...
  })
})

Can you please help me with the approach. What should I test. Is it even testable ? Please help me.

Upvotes: 1

Views: 9999

Answers (1)

Brandon Taylor
Brandon Taylor

Reputation: 34593

You can always make a new event:

const event = new Event('click');

or whatever type of event you need: https://developer.mozilla.org/en-US/docs/Web/Events

to pass into your method

Upvotes: 4

Related Questions