0

Here is a Mocha test in my Javascript program:

 it('displays all available currencies in drop down list', () => {
    return invoiceEditPage.details.currencyDropDown.dropDown.waitForEnabled()
    .then(() => invoiceEditPage.details.currencyDropDown.click())
    .then(() => getEnabledCurrencies(tenantUsers.admin.authUser))
    .then((listOfCurrencies) => console.log(listOfCurrencies["name"].split(",")))
    //.then((listOfCurrencies) => this.getCurrencyFromJSON(listOfCurrencies))
    //.then(() => console.log(invoiceEditPage.details.currencyDropDown.dropDownContents))
    //.then((listOfCurrencies) => assert.strictEqual(listOfCurrencies, invoiceEditPage.details.currencyDropDown.dropDownContents))
    .then(() => invoiceEditPage.details.currencyDropDown.dropDownMask.click());
});

If I just use the line

.then((listOfCurrencies) => console.log(listOfCurrencies))

then I can see the JSON string being printed out as something like this:

[ { displayText: 'USD$',
name: 'US Dollar',
symbol: 'USD$' },

etc.

I would like to get a string array containing the names of all the JSON objects, ie. ["US Dollar", "Canadian Dollar", "Australian Dollar"].

However, using the line I have above, it claims:

"undefined: Cannot read property 'split' of undefined".

If I try JSON.parse(), I get an Unexpected token o, so I know that "listOfCurrencies" it is already a string. What is happening?

Thanks

2
  • 1
    listOfCurrencies is an array so you can't do listOfCurrencies["name"]. You can do listOfCurrencies[index]["name"]. You need to iterate over the array or use the map function. Commented Aug 11, 2016 at 14:58
  • I believe when I tried earlier, it complained if I tried to index into listOfCurrencies because it thought it wasn't an array. Commented Aug 11, 2016 at 16:15

1 Answer 1

3

You could use the map function to only extract the name property from the currency

.then((listOfCurrencies) =>      
    console.log(listOfCurrencies.map( currency => currency.name ));
);
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.