0

Trying to write an unit test for changeFunctionalityStatus(). facing the issue like fControl.get is not a function . Stuck with this Anyone pls help me why this issue coming. TIA

 changeFunctionalityStatus(fControl: FormControl, screen) {
            if (this.isConsult) return;
            const isFuncActivated = fControl.get('isFunctionalityActivatedController').value;
            if (fControl.value.functionalityCodeController == "CONSULT_PROJECT" || fControl.value.functionalityCodeController == "CONSULT_BUSINESS_OFFER" || fControl.value.functionalityCodeController == "CONSULT_PROJECT_CONTRACT" || fControl.value.functionalityCodeController == "CONSULT_BILLING_LIST" || fControl.value.functionalityCodeController == "CONSULT_PROJECT_INVOICES" || fControl.value.functionalityCodeController == "CONSULT_CONTRACT" || fControl.value.functionalityCodeController == "CONSULT_MILESTONE") {
                this.removeFunctionalitiesByScreen(screen);
            }
            if (isFuncActivated) {
                this.removeFunctionality(fControl);
            } else {
                this.addFunctionality(fControl);
            }
            this.checkAndSetScreenAccessController(screen);
        }

spec.ts

it('changeFunctionalityStatus', () => {
    
        spyOn(component, 'changeFunctionalityStatus').and.callThrough();
        component.changeFunctionalityStatus({} as FormControl,"mockscreen");
        expect(component.changeFunctionalityStatus).toHaveBeenCalled();
    
      });

1 Answer 1

0

You should return a mock object that mimics the properties and methods in fControl. I use beforeEach to reset the properties each time a new test case runs.

describe('changeFunctionalityStatus method', () => {
  let fControl: any = {};
  let returnVal = {
    value: true,
  };
  beforeEach(() => {
    fControl = {
      get: () => returnVal,
      value: {
        functionalityCodeController: 'CONSULT_MILESTONE',
      },
    };
    spyOn(component, 'removeFunctionalitiesByScreen');
    spyOn(component, 'removeFunctionality');
    spyOn(component, 'addFunctionality');
    spyOn(component, 'checkAndSetScreenAccessController');
  });

  it('should call removeFunctionality method', () => {
    component.changeFunctionalityStatus(fControl, 'hello');
    expect(component.removeFunctionality).toHaveBeenCalledWith(fControl);
  });

  it('should call addFunctionality method', () => {
    returnVal.value = false;
    component.changeFunctionalityStatus(fControl, 'hello');
    expect(component.addFunctionality).toHaveBeenCalledWith(fControl);
  });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.