Skip to main content
Greg Coogan a posé une question dans #Lightning Experience
I've built a Lightning Web Component and need to write a Jest unit test class for it. A part of the component only renders when a tracked property, called it "showDesktopView" is true. The default value is false. For a Jest test case to pass, the section needs to render. Therefore, I need a method that will result in "showDesktopView" being set to true. It is set to true when the UI theme isn't mobile.

The property:

@track showDesktopView = false;

I've tried the below appproaches of setting the property, but they don't work.

const element = createElement('c-test', { is: testLWC });

element.showDesktopView = "true"; //Trial 1

element.showDesktopView = true; //Trial 2

document.body.setAttribute('showDesktopView','true'); //Trial 3

document.body.appendChild(element);

return Promise.resolve().then(() => {

// My promise code

});

They don't seem to work since my Promise fails. I know my code in the Promise works because if I set the property "showDesktopView" to a default value of true, then it passes.

Does anyone know how to correctly set this property from a Jest unit test?
4 réponses
  1. 23 oct. 2019, 14:08
    I do use this.template.querySelector in my code in certain places (not jest test) and it definitely functions correctly.

    In my jest test, the element.shadowroot.querySelector does work if I default showDesktopView to true. The test case passes, it finds the name value.

    I also found this in my jest test, if I use element.querySelector when default showDesktopView is true, the case fails.
0/9000