0
votes
Flag(a) {
           let element=this.selected.filter(item => item.a=== a)
           return element.length > 1 ? true : false;
        }

Not sure how to write a unit test case for the above function. Can you please help

2

2 Answers

0
votes

A test case consists always of 3 steps:

  • Preparation
  • Execution
  • Verification

In your case this means:

  • Preparation: set the value of this.selected
  • Execution: call Flag with a defined a
  • Verification: check the function return false
0
votes

This should be pretty easy:

it('should run #Flag(a) method', () => {
  component.selected = [{ someKey1: 'someValue1' }, { someKey2: 'someValue2' }];
  const val = 'someValue1';
  spyOn(component, 'Flag').and.callThrough();
  component.Flag(val);
  expect(component.Flag).toHaveBeenCalled();
})