react-testing-library queryByTestId returning NULLLoop inside React JSXProgrammatically navigate using react routerInvariant Violation: Objects are not valid as a React childMissing React Component Props with Jest testReact child component state not updating when new props are passedReact-redux tutorial : Where does children come fromHow to test if a prop is rendered correctly in a Component using Jest and Enzyme in Reacttest cases when accessing function using refsReact Testing Library: Test if children are passed / rendered correctlyIsn't react-testing-library redundant with using a full render?

Open a doc from terminal, but not by its name

Redundant comparison & "if" before assignment

Hero deduces identity of a killer

Fear of getting stuck on one programming language / technology that is not used in my country

Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?

When were female captains banned from Starfleet?

Mixing PEX brands

Extract more than nine arguments that occur periodically in a sentence to use in macros in order to typset

Can a stoichiometric mixture of oxygen and methane exist as a liquid at standard pressure and some (low) temperature?

PTIJ: Haman's bad computer

What is Cash Advance APR?

Why would a new[] expression ever invoke a destructor?

Why does the Sun have different day lengths, but not the gas giants?

It grows, but water kills it

How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?

How to cover method return statement in Apex Class?

A social experiment. What is the worst that can happen?

Store Credit Card Information in Password Manager?

How does a computer interpret real numbers?

Does the Linux kernel need a file system to run?

What is the evidence for the "tyranny of the majority problem" in a direct democracy context?

How can I write humor as character trait?

Why is so much work done on numerical verification of the Riemann Hypothesis?

Angel of Condemnation - Exile creature with second ability



react-testing-library queryByTestId returning NULL


Loop inside React JSXProgrammatically navigate using react routerInvariant Violation: Objects are not valid as a React childMissing React Component Props with Jest testReact child component state not updating when new props are passedReact-redux tutorial : Where does children come fromHow to test if a prop is rendered correctly in a Component using Jest and Enzyme in Reacttest cases when accessing function using refsReact Testing Library: Test if children are passed / rendered correctlyIsn't react-testing-library redundant with using a full render?













0















I'm making the switch from using enzyme to react-testing-library for testing my components.



I have a simple component CustomModal which acts much like a wrapper around the Modal from reactstrap. I am trying to test that my CustomModal includes the child elements that it ought to.



Taking my cues from this article and this article, I am adding data-testid attributes to my children, and then I am using getByTestId and queryByTestId. But, for some reason, my queries are not finding the child nodes that, as far as I can tell, are there.



Is there something I am doing wrong in my test setup, or am I misunderstanding how react-testing-library should be used?



The basic code, along with the test (which is failing), can be found in this CodeSandbox:
Edit react-testing-library queryByTestId



My basic CustomModal component looks like this:



/*
* src/components/CustomModal/index.js
*/

import React from "react";
import Button, Modal, ModalHeader, ModalBody, ModalFooter from "reactstrap";

const getSanitizedModalProps = props =>
let modalProps = ...props ;
delete modalProps.onConfirm;
delete modalProps.onCancel;
delete modalProps.headerText;
delete modalProps.children;
modalProps.isOpen = modalProps.isOpen === true;
return modalProps;
;

export default props =>
return (
<Modal data-testid="modal" ...getSanitizedModalProps(props)>
<ModalHeader data-testid="modal-header">props.headerText</ModalHeader>
<ModalBody data-testid="modal-body">props.children</ModalBody>
<ModalFooter data-testid="modal-footer">
<Button data-testid="confirm-button" onClick=props.onConfirm>
Confirm
</Button>
<Button data-testid="cancel-button" onClick=props.onCancel>
Cancel
</Button>
</ModalFooter>
</Modal>
);
;


My test file looks like this:



/*
* src/components/CustomModal/CustomModal.test.js
*/

import React from "react";
import render from "react-testing-library";
import CustomModal from "./index";

const TEST_IDS =
modal: "modal",
header: "modal-header",
body: "modal-body",
footer: "modal-footer",
cancel: "cancel-button",
confirm: "confirm-button"
;

describe("<Modal />", () =>
const headerText = "hello world";
it("renders all of the children", () =>
const queryByTestId = render(<CustomModal headerText=headerText />);

// The following assertions all fail
expect(queryByTestId(TEST_IDS.modal)).toBeTruthy();
expect(queryByTestId(TEST_IDS.header)).toBeTruthy();
expect(queryByTestId(TEST_IDS.body)).toBeTruthy();
expect(queryByTestId(TEST_IDS.footer)).toBeTruthy();
expect(queryByTestId(TEST_IDS.cancel)).toBeTruthy();
expect(queryByTestId(TEST_IDS.confirm)).toBeTruthy();
);
);









share|improve this question


























    0















    I'm making the switch from using enzyme to react-testing-library for testing my components.



    I have a simple component CustomModal which acts much like a wrapper around the Modal from reactstrap. I am trying to test that my CustomModal includes the child elements that it ought to.



    Taking my cues from this article and this article, I am adding data-testid attributes to my children, and then I am using getByTestId and queryByTestId. But, for some reason, my queries are not finding the child nodes that, as far as I can tell, are there.



    Is there something I am doing wrong in my test setup, or am I misunderstanding how react-testing-library should be used?



    The basic code, along with the test (which is failing), can be found in this CodeSandbox:
    Edit react-testing-library queryByTestId



    My basic CustomModal component looks like this:



    /*
    * src/components/CustomModal/index.js
    */

    import React from "react";
    import Button, Modal, ModalHeader, ModalBody, ModalFooter from "reactstrap";

    const getSanitizedModalProps = props =>
    let modalProps = ...props ;
    delete modalProps.onConfirm;
    delete modalProps.onCancel;
    delete modalProps.headerText;
    delete modalProps.children;
    modalProps.isOpen = modalProps.isOpen === true;
    return modalProps;
    ;

    export default props =>
    return (
    <Modal data-testid="modal" ...getSanitizedModalProps(props)>
    <ModalHeader data-testid="modal-header">props.headerText</ModalHeader>
    <ModalBody data-testid="modal-body">props.children</ModalBody>
    <ModalFooter data-testid="modal-footer">
    <Button data-testid="confirm-button" onClick=props.onConfirm>
    Confirm
    </Button>
    <Button data-testid="cancel-button" onClick=props.onCancel>
    Cancel
    </Button>
    </ModalFooter>
    </Modal>
    );
    ;


    My test file looks like this:



    /*
    * src/components/CustomModal/CustomModal.test.js
    */

    import React from "react";
    import render from "react-testing-library";
    import CustomModal from "./index";

    const TEST_IDS =
    modal: "modal",
    header: "modal-header",
    body: "modal-body",
    footer: "modal-footer",
    cancel: "cancel-button",
    confirm: "confirm-button"
    ;

    describe("<Modal />", () =>
    const headerText = "hello world";
    it("renders all of the children", () =>
    const queryByTestId = render(<CustomModal headerText=headerText />);

    // The following assertions all fail
    expect(queryByTestId(TEST_IDS.modal)).toBeTruthy();
    expect(queryByTestId(TEST_IDS.header)).toBeTruthy();
    expect(queryByTestId(TEST_IDS.body)).toBeTruthy();
    expect(queryByTestId(TEST_IDS.footer)).toBeTruthy();
    expect(queryByTestId(TEST_IDS.cancel)).toBeTruthy();
    expect(queryByTestId(TEST_IDS.confirm)).toBeTruthy();
    );
    );









    share|improve this question
























      0












      0








      0








      I'm making the switch from using enzyme to react-testing-library for testing my components.



      I have a simple component CustomModal which acts much like a wrapper around the Modal from reactstrap. I am trying to test that my CustomModal includes the child elements that it ought to.



      Taking my cues from this article and this article, I am adding data-testid attributes to my children, and then I am using getByTestId and queryByTestId. But, for some reason, my queries are not finding the child nodes that, as far as I can tell, are there.



      Is there something I am doing wrong in my test setup, or am I misunderstanding how react-testing-library should be used?



      The basic code, along with the test (which is failing), can be found in this CodeSandbox:
      Edit react-testing-library queryByTestId



      My basic CustomModal component looks like this:



      /*
      * src/components/CustomModal/index.js
      */

      import React from "react";
      import Button, Modal, ModalHeader, ModalBody, ModalFooter from "reactstrap";

      const getSanitizedModalProps = props =>
      let modalProps = ...props ;
      delete modalProps.onConfirm;
      delete modalProps.onCancel;
      delete modalProps.headerText;
      delete modalProps.children;
      modalProps.isOpen = modalProps.isOpen === true;
      return modalProps;
      ;

      export default props =>
      return (
      <Modal data-testid="modal" ...getSanitizedModalProps(props)>
      <ModalHeader data-testid="modal-header">props.headerText</ModalHeader>
      <ModalBody data-testid="modal-body">props.children</ModalBody>
      <ModalFooter data-testid="modal-footer">
      <Button data-testid="confirm-button" onClick=props.onConfirm>
      Confirm
      </Button>
      <Button data-testid="cancel-button" onClick=props.onCancel>
      Cancel
      </Button>
      </ModalFooter>
      </Modal>
      );
      ;


      My test file looks like this:



      /*
      * src/components/CustomModal/CustomModal.test.js
      */

      import React from "react";
      import render from "react-testing-library";
      import CustomModal from "./index";

      const TEST_IDS =
      modal: "modal",
      header: "modal-header",
      body: "modal-body",
      footer: "modal-footer",
      cancel: "cancel-button",
      confirm: "confirm-button"
      ;

      describe("<Modal />", () =>
      const headerText = "hello world";
      it("renders all of the children", () =>
      const queryByTestId = render(<CustomModal headerText=headerText />);

      // The following assertions all fail
      expect(queryByTestId(TEST_IDS.modal)).toBeTruthy();
      expect(queryByTestId(TEST_IDS.header)).toBeTruthy();
      expect(queryByTestId(TEST_IDS.body)).toBeTruthy();
      expect(queryByTestId(TEST_IDS.footer)).toBeTruthy();
      expect(queryByTestId(TEST_IDS.cancel)).toBeTruthy();
      expect(queryByTestId(TEST_IDS.confirm)).toBeTruthy();
      );
      );









      share|improve this question














      I'm making the switch from using enzyme to react-testing-library for testing my components.



      I have a simple component CustomModal which acts much like a wrapper around the Modal from reactstrap. I am trying to test that my CustomModal includes the child elements that it ought to.



      Taking my cues from this article and this article, I am adding data-testid attributes to my children, and then I am using getByTestId and queryByTestId. But, for some reason, my queries are not finding the child nodes that, as far as I can tell, are there.



      Is there something I am doing wrong in my test setup, or am I misunderstanding how react-testing-library should be used?



      The basic code, along with the test (which is failing), can be found in this CodeSandbox:
      Edit react-testing-library queryByTestId



      My basic CustomModal component looks like this:



      /*
      * src/components/CustomModal/index.js
      */

      import React from "react";
      import Button, Modal, ModalHeader, ModalBody, ModalFooter from "reactstrap";

      const getSanitizedModalProps = props =>
      let modalProps = ...props ;
      delete modalProps.onConfirm;
      delete modalProps.onCancel;
      delete modalProps.headerText;
      delete modalProps.children;
      modalProps.isOpen = modalProps.isOpen === true;
      return modalProps;
      ;

      export default props =>
      return (
      <Modal data-testid="modal" ...getSanitizedModalProps(props)>
      <ModalHeader data-testid="modal-header">props.headerText</ModalHeader>
      <ModalBody data-testid="modal-body">props.children</ModalBody>
      <ModalFooter data-testid="modal-footer">
      <Button data-testid="confirm-button" onClick=props.onConfirm>
      Confirm
      </Button>
      <Button data-testid="cancel-button" onClick=props.onCancel>
      Cancel
      </Button>
      </ModalFooter>
      </Modal>
      );
      ;


      My test file looks like this:



      /*
      * src/components/CustomModal/CustomModal.test.js
      */

      import React from "react";
      import render from "react-testing-library";
      import CustomModal from "./index";

      const TEST_IDS =
      modal: "modal",
      header: "modal-header",
      body: "modal-body",
      footer: "modal-footer",
      cancel: "cancel-button",
      confirm: "confirm-button"
      ;

      describe("<Modal />", () =>
      const headerText = "hello world";
      it("renders all of the children", () =>
      const queryByTestId = render(<CustomModal headerText=headerText />);

      // The following assertions all fail
      expect(queryByTestId(TEST_IDS.modal)).toBeTruthy();
      expect(queryByTestId(TEST_IDS.header)).toBeTruthy();
      expect(queryByTestId(TEST_IDS.body)).toBeTruthy();
      expect(queryByTestId(TEST_IDS.footer)).toBeTruthy();
      expect(queryByTestId(TEST_IDS.cancel)).toBeTruthy();
      expect(queryByTestId(TEST_IDS.confirm)).toBeTruthy();
      );
      );






      reactjs jestjs react-testing-library






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 1:52









      Alvin LeeAlvin Lee

      2,5331425




      2,5331425






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Your modal is closed, you need to pass isOpen to it:



          render(<CustomModal headerText=headerText isOpen />);





          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2fstackoverflow.com%2fquestions%2f55055619%2freact-testing-library-querybytestid-returning-null%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









            1














            Your modal is closed, you need to pass isOpen to it:



            render(<CustomModal headerText=headerText isOpen />);





            share|improve this answer



























              1














              Your modal is closed, you need to pass isOpen to it:



              render(<CustomModal headerText=headerText isOpen />);





              share|improve this answer

























                1












                1








                1







                Your modal is closed, you need to pass isOpen to it:



                render(<CustomModal headerText=headerText isOpen />);





                share|improve this answer













                Your modal is closed, you need to pass isOpen to it:



                render(<CustomModal headerText=headerText isOpen />);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 8 at 8:40









                GpxGpx

                2,07121626




                2,07121626





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • 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%2fstackoverflow.com%2fquestions%2f55055619%2freact-testing-library-querybytestid-returning-null%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







                    Popular posts from this blog

                    Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                    Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                    2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived