1

Im having a lot of trouble trying to read the options coming from a combobox when i select it. Ive tried

  • to move the options to a @track variable
  • making the call for the options just simply return the array and not call a method from another component.

When component_name.options is called i get [].

The stub for ligtning-combobox shows that there is a api variable called options but when i call it, it is []. Any ideas?

whoAreYouOptions is an array coming from the parent. and is tagged as api. Also, getClaimantTypes is a export method in a component i use for utils.

Jest test

test('get selected who are you dropdown', async () => {
    const contactDetails = document.querySelector('c-contact-details');
    contactDetails.whoAreYouOptions = ['Person'];
    await Promise.resolve()
    const personType = contactDetails.shadowRoot.querySelector('lightning-combobox[data-name="personType"]')
    console.log(personType.options)
    expect(contactDetails.options.length).toBe(3)
})

Html

<lightning-layout-item size="12" small-device-size="12" medium-device-size="12" large-device-size="12"
                padding="around-small">
                <lightning-combobox name="personType" label="Who are you?" options={personTypes}
                    onchange={debounceChangeEvent} value={formData.personType} class="input" data-name="personType"></lightning-combobox>
            </lightning-layout-item>

Javascript

get personTypes()
    {
        let types = getClaimantTypes()
        return types.filter(each => !this.whoAreYouOptions.includes(each.label))
    }

test output

console.log
    []

1 Answer 1

0

From what I can tell, these are the culprits:

console.log(personType.options)
expect(contactDetails.options.length).toBe(3)

As such, console.log won't output anything. I generally suggest not to work with console in the context of Jest and use breakpoints instead. One should however get something not empty-arrayish, if one does

console.log(JSON.stringify(personType.options))

Just in case you insist.

As for the expect, - don't you want to retrieve personType.options?!

3
  • LOL, thanks for pointing out i was using the wrong variable. I switched to using the debugger and can see its populated correctly, I also changed the variable and all is good now. Thank you!
    – mrgagne
    Commented Jun 24 at 13:36
  • I wish there were more people using (and asking questions regarding) Jest - always fun :-) Commented Jun 24 at 14:32
  • me too, it helps so much. Its way better than going into the browser every time to test something.
    – mrgagne
    Commented Jul 11 at 18:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .