Undefined is not a constructor during React JS unit testing

843 views Asked by At

I am unit testing and will briefly layout the structure of the code. 1. currency.js which has currencies in an object which looks like this:

 module.exports = {
    'United States': 'USD',
    'India': 'Rupee',
    'Mexico': 'Peso',
   }
  1. CurrencyField which needs the currency.js to print it as an option list:

    const React = require('react'); const currency = require('somepath/currency');

    const CurrencyField = ({onCurrencyNameSet, currencyName}) => {
        const currencyOptions = ['Select Currency'].concat(Object.values(currency)).map((thiscurrency) =>
            <option key={thiscurrency}>{thiscurrency}</option>
            );
        return (
            <fieldset className="currency-form-group">
                <label className="currency-form-group-label">Country</label>
                <select onChange={onCurrencyNameSet} className="currency-form" value={currencyName}>
                    {currencyOptions}
                </select>
            </fieldset>
        );
    };
    
  2. I then have a PaymentForm.js which uses the currency as one of its fields.

class PaymentForm extends React.Component {

// some events. render() {

  return (
    // many things
    <CurrencyField onCurrencyNameSet={this.onFieldSet('currency')} currencyName={this.state.currencyName} />
    // many other things
  )

} }

  1. Then I have a unit test framework like this:

    describe('when rendering currency', function() { beforeEach(function() { this.wrapper = Enzyme.mount(React.createElement(PaymentForm, {some params})); });

  2. Trouble: undefined is not a constructor (evaluating 'Object.values(currency)')

Any ideas how to resolve this issue ?

1

There are 1 answers

1
Andy Ray On

This syntax is invalid:

['Select Currency'].concat(currency).Object.values(currency)

Object.values() is a standalone function call. You can't combine it in arrays with dot notation like this.

Did you mean?

['Select Currency'].concat( Object.values(currency) )