web_in
web_in

Reputation: 101

fControl.get is not a function in angular unit test

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();
    
      });

Upvotes: 0

Views: 21

Answers (1)

Naren Murali
Naren Murali

Reputation: 56052

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);
  });
});

Upvotes: 0

Related Questions