angularunit-testingjasminekarma-jasminejasmine-node

How can I handle events of ts file inside spec file while doing unit test of angular


I wanted to test a function which have arguments type event. Inside the function it was checking whether the event object passed (lets say eventObj) eventObj.target.checked is true or not. How to handle this in the spec file?

If I'm planning to call the function in spec file, Ho can I create a dummy object having dummyObj.target.checked as true, so that I can get inside the if condition.


Solution

  • Something like this maybe:

    it('should do ___ when checked', () => {
      const mockObject = {
        target: {
          checked: true,
       }
      };
      component.yourFunctionThatTakesThatObject(mockObject as WhateverTheEventIs); // Typescript might complain that it is a mismatch, if so then you can do mockObject as any
    // Then the rest of your assertions can go here.
    });