<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: React/Jest - Properly testing component including external component in Developer Q&amp;A Forum</title>
    <link>https://community.dynatrace.com/t5/Developer-Q-A-Forum/React-Jest-Properly-testing-component-including-external/m-p/207727#M241</link>
    <description>&lt;P&gt;Thank you! That did the trick &lt;img class="lia-deferred-image lia-image-emoji" src="https://community.dynatrace.com/html/@C05EE4F42B21E33B69BD3C641CD041DB/images/emoticons/dancing_blob.gif" alt=":dancing_blob:" title=":dancing_blob:" /&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 23 Mar 2023 07:23:14 GMT</pubDate>
    <dc:creator>animel</dc:creator>
    <dc:date>2023-03-23T07:23:14Z</dc:date>
    <item>
      <title>React/Jest - Properly testing component including external component</title>
      <link>https://community.dynatrace.com/t5/Developer-Q-A-Forum/React-Jest-Properly-testing-component-including-external/m-p/207506#M234</link>
      <description>&lt;P&gt;Hi all,&lt;BR /&gt;I'm currently using the&amp;nbsp;ExpandableSection&amp;nbsp;in my app and I want to test with Jest the following behavior:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;if section is expanded show&amp;nbsp;ComponentA&amp;nbsp;and not&amp;nbsp;ComponentB&lt;/LI&gt;
&lt;LI&gt;if section is closed show&amp;nbsp;ComponentB&amp;nbsp;and not&amp;nbsp;ComponentA&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;I'm simulating an&amp;nbsp;fireEvent.click()&amp;nbsp;an check afterwards if the components are defined/throwing. Unfortunately, I'm failing to write the proper test for case 2.&lt;/P&gt;
&lt;DIV class=""&gt;My component looks like follows&lt;/DIV&gt;
&lt;DIV class=""&gt;&lt;LI-CODE lang="javascript"&gt;import { ExpandableSection } from '@dynatrace/strato-components-labs';

export const MyComponent = () =&amp;gt; {
  const [isClosed, setIsClosed] = useState&amp;lt;boolean&amp;gt;(false);

  const onChangeExpandable = () =&amp;gt; {
    setIsClosed((prev) =&amp;gt; !prev);
  };

  return (
    &amp;lt;&amp;gt;
      &amp;lt;ExpandableSection
        variant={'heading-3'}
        defaultOpen={true}
        label={'Global'}
        onOpenChange={() =&amp;gt; onChangeExpandable()}
        data-testid='expandable'
      &amp;gt;
        &amp;lt;AComponent data-testid='a-component'/&amp;gt;
      &amp;lt;/ExpandableSection&amp;gt;

      {/* Show component if expandable section is closed */}
      {isClosed ? &amp;lt;BComponent data-testid='b-component' /&amp;gt; : null}
    &amp;lt;/&amp;gt;
  );
}; &lt;/LI-CODE&gt;&lt;/DIV&gt;
&lt;DIV class=""&gt;
&lt;P&gt;&amp;nbsp;My test for case 2 looks as follows&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;it('Should show BComponent and should not show AComponent if expandable is closed', async () =&amp;gt; {
  // given
  render(&amp;lt;MyComponent /&amp;gt;);

  // when
  const expandable = await screen.findByTestId('expandable');
  fireEvent.click(expandable.querySelector('button') as Element);
  const bComponent = await screen.findByTestId('b-component');

  // then
  await expect(screen.findByTestId('a-component')).rejects.toThrow();
  expect(bComponent).toBeDefined();
});&lt;/LI-CODE&gt;
&lt;P&gt;The test is failing because the promise is resolved, and not rejected. But why?&lt;/P&gt;
&lt;P&gt;Some further general questions:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;is it actually useful doing such a test where an external component (ExpandableSection) is involved?&lt;/LI&gt;
&lt;LI&gt;does it make sense doing here an intergration test with TestCafe (as an external component is involved)?&lt;/LI&gt;
&lt;LI&gt;should I rather check the state, i.e.
&lt;UL&gt;
&lt;LI&gt;if&amp;nbsp;isClosed&amp;nbsp;is&amp;nbsp;true&amp;nbsp;the&amp;nbsp;&amp;lt;BComponent /&amp;gt;&amp;nbsp;should be defined&lt;/LI&gt;
&lt;LI&gt;if&amp;nbsp;isClosed&amp;nbsp;is&amp;nbsp;false&amp;nbsp;the&amp;nbsp;&amp;lt;AComponent /&amp;gt;&amp;nbsp;should be shown&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;/DIV&gt;
&lt;P&gt;&lt;CODE class="" data-stringify-type="code"&gt;&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Apr 2023 07:35:34 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Developer-Q-A-Forum/React-Jest-Properly-testing-component-including-external/m-p/207506#M234</guid>
      <dc:creator>animel</dc:creator>
      <dc:date>2023-04-20T07:35:34Z</dc:date>
    </item>
    <item>
      <title>Re: React/Jest - Properly testing component including external component</title>
      <link>https://community.dynatrace.com/t5/Developer-Q-A-Forum/React-Jest-Properly-testing-component-including-external/m-p/207514#M235</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;did you check if component A is still rendered even if they the section isn't expanded? If so, maybe&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;expect(screen.findByTestId('a-component')).not.toBeVisible();&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;would be the better choice.&lt;/P&gt;
&lt;P&gt;Since it is a component from the design system I would expect that you don't have to test it's functionality since it's already tested internally.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2023 08:47:42 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Developer-Q-A-Forum/React-Jest-Properly-testing-component-including-external/m-p/207514#M235</guid>
      <dc:creator>JohannesHaas</dc:creator>
      <dc:date>2023-03-21T08:47:42Z</dc:date>
    </item>
    <item>
      <title>Re: React/Jest - Properly testing component including external component</title>
      <link>https://community.dynatrace.com/t5/Developer-Q-A-Forum/React-Jest-Properly-testing-component-including-external/m-p/207532#M236</link>
      <description>&lt;P&gt;Hi Melanie, it may be because the ExpandableSection only removes the component once the animation is finished. Using waitFor should work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;import { waitFor } from "@dynatrace/strato-components-preview/testing";

await waitFor(async () =&amp;gt; {
  expect(await screen.findByTestId('a-component')).not.toBeVisible();
});
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;You can read more about the waitFor options &lt;A href="https://testing-library.com/docs/dom-testing-library/api-async/#waitfor" target="_blank" rel="noopener"&gt;here.&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2023 11:45:42 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Developer-Q-A-Forum/React-Jest-Properly-testing-component-including-external/m-p/207532#M236</guid>
      <dc:creator>educampver</dc:creator>
      <dc:date>2023-03-21T11:45:42Z</dc:date>
    </item>
    <item>
      <title>Re: React/Jest - Properly testing component including external component</title>
      <link>https://community.dynatrace.com/t5/Developer-Q-A-Forum/React-Jest-Properly-testing-component-including-external/m-p/207726#M240</link>
      <description>&lt;P&gt;Thanks for your answer! The expect(...).not.toBeVisible() is the better choice!&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2023 07:22:15 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Developer-Q-A-Forum/React-Jest-Properly-testing-component-including-external/m-p/207726#M240</guid>
      <dc:creator>animel</dc:creator>
      <dc:date>2023-03-23T07:22:15Z</dc:date>
    </item>
    <item>
      <title>Re: React/Jest - Properly testing component including external component</title>
      <link>https://community.dynatrace.com/t5/Developer-Q-A-Forum/React-Jest-Properly-testing-component-including-external/m-p/207727#M241</link>
      <description>&lt;P&gt;Thank you! That did the trick &lt;img class="lia-deferred-image lia-image-emoji" src="https://community.dynatrace.com/html/@C05EE4F42B21E33B69BD3C641CD041DB/images/emoticons/dancing_blob.gif" alt=":dancing_blob:" title=":dancing_blob:" /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2023 07:23:14 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Developer-Q-A-Forum/React-Jest-Properly-testing-component-including-external/m-p/207727#M241</guid>
      <dc:creator>animel</dc:creator>
      <dc:date>2023-03-23T07:23:14Z</dc:date>
    </item>
  </channel>
</rss>

