Home > other >  How can i test the if branch inside this function
How can i test the if branch inside this function

Time:07-14

onSelect(event: any) {
    if (event.type === 'GROUP') {
      return null;
    }
  }

I´ve mocked event.

    function event(): any {
  return [
    {
      originalEvent: {
        isTrusted: true,
      },
      query: 'MeNu',
    },
    {
      originalEvent: {
        isTrusted: true,
      },
      query: 'MeNu',
      url: '/i2s-life',
      type: 'GROUP',
    },
    {
      originalEvent: {
        isTrusted: true,
      },
      query: 'MeNu',
      url: '/i2s-life',
      type: 'EMBED',
    },
  ];
}

And made following SPEC test:

it('should select event with type GROUP', () => {
    const ev = event()[1];
    component.onSelect(event());
    expect(ev.type).toEqual('GROUP');
  });

But the if branch does not get covered.

CodePudding user response:

You should send the argument ev and not event().

Like this:

it('should select event with type GROUP', () => {
    const ev = event()[1];
    const select = component.onSelect(ev);
    expect(select).toBeNull();
  });
  • Related