0
extractValue(exemptionLevel: string, exemptionValue: any): string {
switch(exemptionLevel) {
  case "C": return exemptionValue.isoCountryCode.toString();
  case "R": return exemptionValue.districtNumber.toString();
  case "D": return exemptionValue.divisionCode.toString();
  case "T": return exemptionValue.terminalNumber.toString();
}

}

i tried below

it('expected value extractValue() c', () => {
component.extractValue('C','isoCountry');
expect('C').toEqual('isoCountry')

// expect(component.extractValue(expectedLevel[0], exemptionValue.isoCountryCode)).toHaveBeenCalled();

});

it showing error TypeError: Cannot read property 'toString' of undefined

1 Answer 1

1

You are sending a string parameter to the function and you are expecting to access its isoCountryCode property, which will always be undefined. You have to provide the tested function with exemptionValue mock.

it('expected value extractValue() c', () => {
  const isoCountryMockResult = 'someResult';
  const exemptionValue = { isoCountryCode: isoCountryMockResult } // define your mock
  component.extractValue('C', exemptionValue);
  expect('C').toEqual(isoCountryMockResult)
}

This should work, but you have to adjust it to your needs

Sign up to request clarification or add additional context in comments.

4 Comments

working fine . thankyou dallows
You should accept the answer then
i accepted.. Thanks for the feedback! You need at least 15 reputation to cast a vote, but your feedback has been recorded.
How to write testcase subscribe inside constructor ` constructor(private exemptionService:ExemptionService){ this.exemptionService.getGlobalExemptionReasons().subscribe(reason => this.reason = reason); }`

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.