Unit test cases for event

Viewed 37

I'm trying to write unit test cases for the the below function:

selected (event:any) {
  let selectedValue = event.target.value.substring(0,3);
  this.seletedBatch = selectedValue;
  this.enableSubmitButton = true
}

The test cases which I have written:

let selectedValue= fixture.debugElement.nativeElement.querySelector('selectedValue')
fixture.detectChanges();
expect(component.selectedBatch).toEqual(selectedValue)
expect (component.enableSubmitButton).toBe(true)

But I'm getting the following errors and not sure if I'm writing it correctly for this event:

1. Expected undefined to equal null. 
2. Expected false to be true

P.S - I'm new in writing test cases.

Thanks in advance. Any help is appreciated

1 Answers

Usually testing goes like this:

// 1. Arrange
// here goes everything you already did to set the test up
// fixture, dependencies, etc
let testValue = 'test';
let expectedValue = 'tes';

// 2. Act
component.selected({ target: { value: testValue } });
fixture.detectChanges();

// 3. Assert  
expect(component.selectedBatch).toEqual(expectedValue);
expect(component.enableSubmitButton).toBe(true);
Related