# React/Jest - Properly testing component including external component

**URL:** <https://community.dynatrace.com/t/react-jest-properly-testing-component-including-external-component/38919>\
**Category:** Developer Q&A Forum\
**Tags:** coding-assistance\
**Created:** [March 21, 2023, 7:29am UTC](https://community.dynatrace.com/t/react-jest-properly-testing-component-including-external-component/38919 "2023-03-21T07:29:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![animel](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/animel/32/11483_2.png) [@animel](https://community.dynatrace.com/u/animel)\
**Post date:** [March 21, 2023, 7:29am UTC](https://community.dynatrace.com/t/react-jest-properly-testing-component-including-external-component/38919/1 "2023-03-21T07:29:46Z")

</div>

Hi all,  
I’m currently using the&nbsp;ExpandableSection&nbsp;in my app and I want to test with Jest the following behavior:

- if section is expanded show&nbsp;ComponentA&nbsp;and not&nbsp;ComponentB
- if section is closed show&nbsp;ComponentB&nbsp;and not&nbsp;ComponentA

I’m simulating an&nbsp;fireEvent.click()&nbsp;an check afterwards if the components are defined/throwing. Unfortunately, I’m failing to write the proper test for case 2.

My component looks like follows

```javascript
import { ExpandableSection } from '@dynatrace/strato-components-labs';

export const MyComponent = () => {
  const [isClosed, setIsClosed] = useState<boolean>(false);

  const onChangeExpandable = () => {
    setIsClosed((prev) => !prev);
  };

  return (
    <>
      <ExpandableSection
        variant={'heading-3'}
        defaultOpen={true}
        label={'Global'}
        onOpenChange={() => onChangeExpandable()}
        data-testid='expandable'
      >
        <AComponent data-testid='a-component'/>
      </ExpandableSection>

      {/* Show component if expandable section is closed */}
      {isClosed ? <BComponent data-testid='b-component' /> : null}
    </>
  );
};

```

My test for case 2 looks as follows

```javascript
it('Should show BComponent and should not show AComponent if expandable is closed', async () => {
  // given
  render(<MyComponent />);

  // 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();
});

```

The test is failing because the promise is resolved, and not rejected. But why?

Some further general questions:

- is it actually useful doing such a test where an external component (ExpandableSection) is involved?
- does it make sense doing here an intergration test with TestCafe (as an external component is involved)?
- should I rather check the state, i.e.
  - if&nbsp;isClosed&nbsp;is&nbsp;true&nbsp;the&nbsp;\<BComponent /\>&nbsp;should be defined
  - if&nbsp;isClosed&nbsp;is&nbsp;false&nbsp;the&nbsp;\<AComponent /\>&nbsp;should be shown

---

<div class="post-metadata">

**Author:** ![JohannesHaas](https://avatars.discourse-cdn.com/v4/letter/j/c57346/32.png) [@JohannesHaas](https://community.dynatrace.com/u/JohannesHaas)\
**Post date:** [March 21, 2023, 8:47am UTC](https://community.dynatrace.com/t/react-jest-properly-testing-component-including-external-component/38919/2 "2023-03-21T08:47:42Z")

</div>

Hi,

did you check if component A is still rendered even if they the section isn’t expanded? If so, maybe

```javascript
expect(screen.findByTestId('a-component')).not.toBeVisible();

```

would be the better choice.

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.

---

<div class="post-metadata">

**Author:** ![educampver](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/educampver/32/29917_2.png) [@educampver](https://community.dynatrace.com/u/educampver)\
**Post date:** [March 21, 2023, 11:03am UTC](https://community.dynatrace.com/t/react-jest-properly-testing-component-including-external-component/38919/3 "2023-03-21T11:03:46Z")

</div>

Hi Melanie, it may be because the ExpandableSection only removes the component once the animation is finished. Using waitFor should work:

```javascript
import { waitFor } from "@dynatrace/strato-components-preview/testing";

await waitFor(async () => {
  expect(await screen.findByTestId('a-component')).not.toBeVisible();
});

```

You can read more about the waitFor options [here.](https://testing-library.com/docs/dom-testing-library/api-async/#waitfor)

---

<div class="post-metadata">

**Author:** ![animel](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/animel/32/11483_2.png) [@animel](https://community.dynatrace.com/u/animel)\
**Post date:** [March 23, 2023, 7:22am UTC](https://community.dynatrace.com/t/react-jest-properly-testing-component-including-external-component/38919/4 "2023-03-23T07:22:15Z")

</div>

Thanks for your answer! The expect(…).not.toBeVisible() is the better choice!

---

<div class="post-metadata">

**Author:** ![animel](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/animel/32/11483_2.png) [@animel](https://community.dynatrace.com/u/animel)\
**Post date:** [March 23, 2023, 7:23am UTC](https://community.dynatrace.com/t/react-jest-properly-testing-component-including-external-component/38919/5 "2023-03-23T07:23:14Z")

</div>

Thank you! That did the trick :dancing_blob:
