How to create new event for unit testing in Angular 2?
Asked Answered
S

3

5

Hi I am writing unit test case for my angular code. I am trying to update textbox in gridview. Below is my gridview code.

<input *ngIf="editing[rowIndex + '-scopevalue']" class="inline-editor" autofocus (blur)="updateValue($event, 'scopevalue', value, rowIndex)" type="text" [value]="value" />

Below function performs update.

 updateValue(event, cell, cellValue, rowIndex) {
        this.editing[rowIndex + '-' + cell] = false;
        this.rows[rowIndex][cell] = event.target.value;
        this.rowsCache[rowIndex][cell] = event.target.value;
        this.scopeEdit = this.rows[rowIndex];
        this.updateScope();
    }

Below unit test case I am writing to check above code.

 it('update scope name value', () => {
        var row = component.rows[0];
        let cell = 'scopevalue';
        let cellValue = row.scopevalue;
        let rowIndex = 0;
        component.updateValue('/bmw', cell, cellValue, rowIndex);
    });

In the above method, first parameter supposed to be event. Can someone help me how to create event? Any help would be appreciated. Thank you

Secondly answered 22/5, 2018 at 8:43 Comment(3)
I don't know if that's you want, but you can make an harcoded value of event.target.value and verify in your updateValue function if rowsCache[rowIndex][cell]will have that value. You can mock Event with a simple object, like this : const event = { target: { value: 42 }}; component.updateValue(event, cell, cellValue, rowIndex);Discount
yes this is what i was looking for. ThanksSecondly
Nice ! I wrote the solution :)Discount
D
9

You can make an hardcoded value of event.target.value and verify in your updateValue function if rowsCache[rowIndex][cell] will have that value.

You can mock an event with a simple object, like this :

const event = { target: { value: 42 }};
component.updateValue(event, cell, cellValue, rowIndex);
Discount answered 22/5, 2018 at 9:33 Comment(4)
Note: This will not work if you have your argument typed as an Event object. There are several properties missing if you want to use that type.Fever
@Fever do you know any solution for that problem? we can't mock completely the Event object, it is a waste of time, typecast didn't work eitherGoodkin
@RaphaëlVO for me this works for me: https://mcmap.net/q/1922587/-angular2-jasmine-event-object , basically create an object const mockEvent : Event = <Event><any>{ //insert your target value here };Ines
if I remember correctly, you can't use directly this mock as Event, you need to typecast it to prevent compile error, like this: mock as unknown as EventGoodkin
I
5

In case your method's argument is typed as an Event object instead:

const mockEvent: Event = <Event><any>{
  target: {
      value: 42      
  }
};
component.updateValue(mockEvent, cell, cellValue, rowIndex);
Ines answered 16/11, 2021 at 7:51 Comment(0)
S
0

You can cast it first to unknown and then to an event like this:

// arrange
const file = {
  name: 'image.png',
  size: 50000,
  type: 'image/png'
} as File;


const mockEvent = {
  target: {
    files: [file]
  }
} as unknown as Event;

// act
component.onInputChange(mockEvent);

// assert
expect(component.files).toEqual([file]);
Scripture answered 28/4, 2022 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.