After running the karma coverage test, I achieved a coverage of 99.3%. To reach 100%, I require assistance in testing the else part of the function below:
createCurrencyUnits(): void {
var keys = Object.keys(ObjectsDomainConstants.CURRENCY_UNITS);
for (var i = 0; i < keys.length; i++) {
if (ObjectsDomainConstants.CURRENCY_UNITS[keys[i]].base_unit === null) {
// Base unit (like: rupees, dollar etc.).
createUnit(ObjectsDomainConstants.CURRENCY_UNITS[keys[i]].name, {
aliases: ObjectsDomainConstants.CURRENCY_UNITS[keys[i]].aliases});
} else {
// Sub unit (like: paise, cents etc.).
createUnit(ObjectsDomainConstants.CURRENCY_UNITS[keys[i]].name, { //<--red line at here
definition: ObjectsDomainConstants.CURRENCY_UNITS[keys[i]].base_unit,
aliases: ObjectsDomainConstants.CURRENCY_UNITS[keys[i]].aliases});
}
}
A suggestion is to mock/override the CURRENCY_UNITS in the test. You may utilize the function/variable provided below:
fromRawInputString(units: any): Units {
try {
this.createCurrencyUnits(); //<-- you can see this function call createCurrencyUnits()
} catch (parsingError) {}
var compatibleUnits = this.toMathjsCompatibleString(units);
if (compatibleUnits !== '') {
try {
unit(compatibleUnits);
} catch (err) {
throw new Error(err);
}
}
return new Units(this.fromStringToList(units));}
Here are the CURRENCY_UNITS:
public static CURRENCY_UNITS = {
dollar: {
name: 'dollar',
aliases: ['$', 'dollars', 'Dollars', 'Dollar', 'USD'],
front_units: ['$'],
base_unit: null
},
rupee: {
name: 'rupee',
aliases: ['Rs', 'rupees', '₹', 'Rupees', 'Rupee'],
front_units: ['Rs ', '₹'],
base_unit: null
},
cent: {
name: 'cent',
aliases: ['cents', 'Cents', 'Cent'],
front_units: [],
base_unit: '0.01 dollar'
},
paise: {
name: 'paise',
aliases: ['paisa', 'Paise', 'Paisa'],
front_units: [],
base_unit: '0.01 rupee'
}};
Below is the code I attempted to test, but encountered issues:
it('should test the CURRENCY_UNITS', () => {
ObjectsDomainConstants.CURRENCY_UNITS.cent.base_unit = '0.03 dollar';
ObjectsDomainConstants.CURRENCY_UNITS.paise.base_unit = '0.02 rupee';
expect(units.fromRawInputString('cent').toDict()).toEqual(
new Units([{exponent: 1, unit: 'cent'}]).toDict());
expect(units.fromRawInputString('paise').toDict()).toEqual(
new Units([{exponent: 1, unit: 'paise'}]).toDict());
ObjectsDomainConstants.CURRENCY_UNITS.cent.base_unit = '0.01 dollar';
ObjectsDomainConstants.CURRENCY_UNITS.paise.base_unit = '0.01 rupee';
});