Lightning Web Component (LWC) not evaluating if:true from testLightning Web Component: A required metadata folder named “lwc” does not exist in this workspaceWhy Lightning Web ComponentNot able to render dynamic Lightning Web ComponentLightning Web Components Easy Spaces sample application LWC component codeSalesforce Lightning Web ComponentLightning Web component is not getting deployed from Source to OrgLightning Web Component: pass parameter from html iteration to javascriptLightning Web Component - Not able to reference content asset file in LWC jsGet Lightning Web Component (LWC) definition on a describeLightning Web Components LWC Open Source Site CSS File

How to search for Android apps without ads?

Can an open source licence be revoked if it violates employer's IP?

What does the "titan" monster tag mean?

Fastest way from 10 to 1 with everyone in between

Commencez à vous connecter -- I don't understand the phrasing of this

Velocity of rotation of a sphere

Idiom for 'person who gets violent when drunk"

Why do the “Shtei HaLechem” not play a prominent part in the davenning for Shavuos?

Was the Lonely Mountain, where Smaug lived, a volcano?

Am I being scammed by a sugar daddy?

Is this equation correct? And if so, is this famous?

Why does this Apple //e drops into system monitor when booting?

Is all-caps blackletter no longer taboo?

How can this shape perfectly cover a cube?

Print the phrase "And she said, 'But that's his.'" using only the alphabet

Any gotchas in buying second-hand sanitary ware?

Is it ethical to cite a reviewer's papers even if they are rather irrelevant?

Purpose of cylindrical attachments on Power Transmission towers

Interview was just a one hour panel. Got an offer the next day; do I accept or is this a red flag?

Is it true that "only photographers care about noise"?

Someone who is granted access to information but not expected to read it

ISP is not hashing the password I log in with online. Should I take any action?

Is there a term for when fiction refers to fiction

Is there a term for someone whose preferred policies are a mix of Left and Right?



Lightning Web Component (LWC) not evaluating if:true from test


Lightning Web Component: A required metadata folder named “lwc” does not exist in this workspaceWhy Lightning Web ComponentNot able to render dynamic Lightning Web ComponentLightning Web Components Easy Spaces sample application LWC component codeSalesforce Lightning Web ComponentLightning Web component is not getting deployed from Source to OrgLightning Web Component: pass parameter from html iteration to javascriptLightning Web Component - Not able to reference content asset file in LWC jsGet Lightning Web Component (LWC) definition on a describeLightning Web Components LWC Open Source Site CSS File






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








5















I am trying to write some Jest test for my LWC and is encountering issue evaluating the if:true attribute.
Below is a sample of my code:



import LightningElement, track from 'lwc';

export default class EinsteinModelMetrics extends LightningElement

@track metrics;

constructor()
super();

let self = this;
window.addEventListener('message', (event, data) =>
if (event.data.name === 'metrics')
self.metrics = event.data.payload;

);




HTML template



<template>
<lightning-card if:true=metrics>
<div class="app slds-p-around_x-small">
<h1 class="slds-text-heading_medium">
Model Id metrics.id
</h1>
</lightning-card>
</template>


Test script



import createElement from 'lwc';
import EinsteinModelMetrics from 'c/einsteinModelMetrics';

describe('c-einstein-model-metrics', () =>

afterEach(() =>
// The jsdom instance is shared across test cases in a
// single file so reset the DOM
while (document.body.firstChild)
document.body.removeChild(document.body.firstChild);

);

it('displays Model id', () =>
const element = createElement('c-einstein-model-metrics',
is: EinsteinModelMetrics,
);
document.body.appendChild(element);

element.metrics =
"id": "LIUVBSMXR6OJKHYUDCERBKKKKK",
"object": "metrics",
"createdAt": "2019-05-14T13:34:52.000+0000",
;


return Promise.resolve().then(()=>
const h1Tag = element.shadowRoot.querySelector('h1');
expect(h1Tag.textContent).toBe('Model Id LIUVBSMXR6OJKHYUDCERBKKKKK');
);
);

);


If I had a h1 tag with a text without any condition, the test work.



Any help around this issue?



Thanks










share|improve this question




























    5















    I am trying to write some Jest test for my LWC and is encountering issue evaluating the if:true attribute.
    Below is a sample of my code:



    import LightningElement, track from 'lwc';

    export default class EinsteinModelMetrics extends LightningElement

    @track metrics;

    constructor()
    super();

    let self = this;
    window.addEventListener('message', (event, data) =>
    if (event.data.name === 'metrics')
    self.metrics = event.data.payload;

    );




    HTML template



    <template>
    <lightning-card if:true=metrics>
    <div class="app slds-p-around_x-small">
    <h1 class="slds-text-heading_medium">
    Model Id metrics.id
    </h1>
    </lightning-card>
    </template>


    Test script



    import createElement from 'lwc';
    import EinsteinModelMetrics from 'c/einsteinModelMetrics';

    describe('c-einstein-model-metrics', () =>

    afterEach(() =>
    // The jsdom instance is shared across test cases in a
    // single file so reset the DOM
    while (document.body.firstChild)
    document.body.removeChild(document.body.firstChild);

    );

    it('displays Model id', () =>
    const element = createElement('c-einstein-model-metrics',
    is: EinsteinModelMetrics,
    );
    document.body.appendChild(element);

    element.metrics =
    "id": "LIUVBSMXR6OJKHYUDCERBKKKKK",
    "object": "metrics",
    "createdAt": "2019-05-14T13:34:52.000+0000",
    ;


    return Promise.resolve().then(()=>
    const h1Tag = element.shadowRoot.querySelector('h1');
    expect(h1Tag.textContent).toBe('Model Id LIUVBSMXR6OJKHYUDCERBKKKKK');
    );
    );

    );


    If I had a h1 tag with a text without any condition, the test work.



    Any help around this issue?



    Thanks










    share|improve this question
























      5












      5








      5


      1






      I am trying to write some Jest test for my LWC and is encountering issue evaluating the if:true attribute.
      Below is a sample of my code:



      import LightningElement, track from 'lwc';

      export default class EinsteinModelMetrics extends LightningElement

      @track metrics;

      constructor()
      super();

      let self = this;
      window.addEventListener('message', (event, data) =>
      if (event.data.name === 'metrics')
      self.metrics = event.data.payload;

      );




      HTML template



      <template>
      <lightning-card if:true=metrics>
      <div class="app slds-p-around_x-small">
      <h1 class="slds-text-heading_medium">
      Model Id metrics.id
      </h1>
      </lightning-card>
      </template>


      Test script



      import createElement from 'lwc';
      import EinsteinModelMetrics from 'c/einsteinModelMetrics';

      describe('c-einstein-model-metrics', () =>

      afterEach(() =>
      // The jsdom instance is shared across test cases in a
      // single file so reset the DOM
      while (document.body.firstChild)
      document.body.removeChild(document.body.firstChild);

      );

      it('displays Model id', () =>
      const element = createElement('c-einstein-model-metrics',
      is: EinsteinModelMetrics,
      );
      document.body.appendChild(element);

      element.metrics =
      "id": "LIUVBSMXR6OJKHYUDCERBKKKKK",
      "object": "metrics",
      "createdAt": "2019-05-14T13:34:52.000+0000",
      ;


      return Promise.resolve().then(()=>
      const h1Tag = element.shadowRoot.querySelector('h1');
      expect(h1Tag.textContent).toBe('Model Id LIUVBSMXR6OJKHYUDCERBKKKKK');
      );
      );

      );


      If I had a h1 tag with a text without any condition, the test work.



      Any help around this issue?



      Thanks










      share|improve this question














      I am trying to write some Jest test for my LWC and is encountering issue evaluating the if:true attribute.
      Below is a sample of my code:



      import LightningElement, track from 'lwc';

      export default class EinsteinModelMetrics extends LightningElement

      @track metrics;

      constructor()
      super();

      let self = this;
      window.addEventListener('message', (event, data) =>
      if (event.data.name === 'metrics')
      self.metrics = event.data.payload;

      );




      HTML template



      <template>
      <lightning-card if:true=metrics>
      <div class="app slds-p-around_x-small">
      <h1 class="slds-text-heading_medium">
      Model Id metrics.id
      </h1>
      </lightning-card>
      </template>


      Test script



      import createElement from 'lwc';
      import EinsteinModelMetrics from 'c/einsteinModelMetrics';

      describe('c-einstein-model-metrics', () =>

      afterEach(() =>
      // The jsdom instance is shared across test cases in a
      // single file so reset the DOM
      while (document.body.firstChild)
      document.body.removeChild(document.body.firstChild);

      );

      it('displays Model id', () =>
      const element = createElement('c-einstein-model-metrics',
      is: EinsteinModelMetrics,
      );
      document.body.appendChild(element);

      element.metrics =
      "id": "LIUVBSMXR6OJKHYUDCERBKKKKK",
      "object": "metrics",
      "createdAt": "2019-05-14T13:34:52.000+0000",
      ;


      return Promise.resolve().then(()=>
      const h1Tag = element.shadowRoot.querySelector('h1');
      expect(h1Tag.textContent).toBe('Model Id LIUVBSMXR6OJKHYUDCERBKKKKK');
      );
      );

      );


      If I had a h1 tag with a text without any condition, the test work.



      Any help around this issue?



      Thanks







      salesforcedx lightning-web-components






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 8 hours ago









      KevanKevan

      245312




      245312




















          1 Answer
          1






          active

          oldest

          votes


















          5














          Only properties decorated with @api are public and accessible from outside the component.
          Tracked variables are component internals and the test cannot set them - which is the reason why your rendering condition will never be fulfilled. You have also some other small issues in your shared code.



          1. you miss a closing tag from the inner div

          2. directives are only allowed on a template element

          First Option is to change the @track decorator to @api (keep in mind that you are then not able to mutate the property inside this component which means you have to consume it by a different component and bubble up the message event)



          Or alternatively you have to trigger the message event in order to mutate the data:
          Changes to your testcase:



           it('displays Model id', () => 
          const element = createElement('c-einstein-model-metrics',
          is: EinsteinModelMetrics,
          );
          document.body.appendChild(element);

          window.postMessage(
          name: 'metrics', payload:
          "id": "LIUVBSMXR6OJKHYUDCERBKKKKK",
          "object": "metrics",
          "createdAt": "2019-05-14T13:34:52.000+0000",

          , '*');

          return new Promise(resolve => setImmediate(resolve) ).then(() =>
          return new Promise(resolve => setImmediate(resolve) )
          ).then(() =>
          const h1Tag = element.shadowRoot.querySelector('h1');
          expect(h1Tag.textContent).toBe('Model Id LIUVBSMXR6OJKHYUDCERBKKKKK');
          )
          );





          share|improve this answer

























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "459"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f265670%2flightning-web-component-lwc-not-evaluating-iftrue-from-test%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            5














            Only properties decorated with @api are public and accessible from outside the component.
            Tracked variables are component internals and the test cannot set them - which is the reason why your rendering condition will never be fulfilled. You have also some other small issues in your shared code.



            1. you miss a closing tag from the inner div

            2. directives are only allowed on a template element

            First Option is to change the @track decorator to @api (keep in mind that you are then not able to mutate the property inside this component which means you have to consume it by a different component and bubble up the message event)



            Or alternatively you have to trigger the message event in order to mutate the data:
            Changes to your testcase:



             it('displays Model id', () => 
            const element = createElement('c-einstein-model-metrics',
            is: EinsteinModelMetrics,
            );
            document.body.appendChild(element);

            window.postMessage(
            name: 'metrics', payload:
            "id": "LIUVBSMXR6OJKHYUDCERBKKKKK",
            "object": "metrics",
            "createdAt": "2019-05-14T13:34:52.000+0000",

            , '*');

            return new Promise(resolve => setImmediate(resolve) ).then(() =>
            return new Promise(resolve => setImmediate(resolve) )
            ).then(() =>
            const h1Tag = element.shadowRoot.querySelector('h1');
            expect(h1Tag.textContent).toBe('Model Id LIUVBSMXR6OJKHYUDCERBKKKKK');
            )
            );





            share|improve this answer





























              5














              Only properties decorated with @api are public and accessible from outside the component.
              Tracked variables are component internals and the test cannot set them - which is the reason why your rendering condition will never be fulfilled. You have also some other small issues in your shared code.



              1. you miss a closing tag from the inner div

              2. directives are only allowed on a template element

              First Option is to change the @track decorator to @api (keep in mind that you are then not able to mutate the property inside this component which means you have to consume it by a different component and bubble up the message event)



              Or alternatively you have to trigger the message event in order to mutate the data:
              Changes to your testcase:



               it('displays Model id', () => 
              const element = createElement('c-einstein-model-metrics',
              is: EinsteinModelMetrics,
              );
              document.body.appendChild(element);

              window.postMessage(
              name: 'metrics', payload:
              "id": "LIUVBSMXR6OJKHYUDCERBKKKKK",
              "object": "metrics",
              "createdAt": "2019-05-14T13:34:52.000+0000",

              , '*');

              return new Promise(resolve => setImmediate(resolve) ).then(() =>
              return new Promise(resolve => setImmediate(resolve) )
              ).then(() =>
              const h1Tag = element.shadowRoot.querySelector('h1');
              expect(h1Tag.textContent).toBe('Model Id LIUVBSMXR6OJKHYUDCERBKKKKK');
              )
              );





              share|improve this answer



























                5












                5








                5







                Only properties decorated with @api are public and accessible from outside the component.
                Tracked variables are component internals and the test cannot set them - which is the reason why your rendering condition will never be fulfilled. You have also some other small issues in your shared code.



                1. you miss a closing tag from the inner div

                2. directives are only allowed on a template element

                First Option is to change the @track decorator to @api (keep in mind that you are then not able to mutate the property inside this component which means you have to consume it by a different component and bubble up the message event)



                Or alternatively you have to trigger the message event in order to mutate the data:
                Changes to your testcase:



                 it('displays Model id', () => 
                const element = createElement('c-einstein-model-metrics',
                is: EinsteinModelMetrics,
                );
                document.body.appendChild(element);

                window.postMessage(
                name: 'metrics', payload:
                "id": "LIUVBSMXR6OJKHYUDCERBKKKKK",
                "object": "metrics",
                "createdAt": "2019-05-14T13:34:52.000+0000",

                , '*');

                return new Promise(resolve => setImmediate(resolve) ).then(() =>
                return new Promise(resolve => setImmediate(resolve) )
                ).then(() =>
                const h1Tag = element.shadowRoot.querySelector('h1');
                expect(h1Tag.textContent).toBe('Model Id LIUVBSMXR6OJKHYUDCERBKKKKK');
                )
                );





                share|improve this answer















                Only properties decorated with @api are public and accessible from outside the component.
                Tracked variables are component internals and the test cannot set them - which is the reason why your rendering condition will never be fulfilled. You have also some other small issues in your shared code.



                1. you miss a closing tag from the inner div

                2. directives are only allowed on a template element

                First Option is to change the @track decorator to @api (keep in mind that you are then not able to mutate the property inside this component which means you have to consume it by a different component and bubble up the message event)



                Or alternatively you have to trigger the message event in order to mutate the data:
                Changes to your testcase:



                 it('displays Model id', () => 
                const element = createElement('c-einstein-model-metrics',
                is: EinsteinModelMetrics,
                );
                document.body.appendChild(element);

                window.postMessage(
                name: 'metrics', payload:
                "id": "LIUVBSMXR6OJKHYUDCERBKKKKK",
                "object": "metrics",
                "createdAt": "2019-05-14T13:34:52.000+0000",

                , '*');

                return new Promise(resolve => setImmediate(resolve) ).then(() =>
                return new Promise(resolve => setImmediate(resolve) )
                ).then(() =>
                const h1Tag = element.shadowRoot.querySelector('h1');
                expect(h1Tag.textContent).toBe('Model Id LIUVBSMXR6OJKHYUDCERBKKKKK');
                )
                );






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 7 hours ago

























                answered 7 hours ago









                Renji-xDRenji-xD

                38113




                38113



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Salesforce Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f265670%2flightning-web-component-lwc-not-evaluating-iftrue-from-test%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown