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?

Replacing matching entries in one column of a file by another column from a different file

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

Are the number of citations and number of published articles the most important criteria for a tenure promotion?

What's the output of a record needle playing an out-of-speed record

Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?

How can bays and straits be determined in a procedurally generated map?

Revoked SSL certificate

Is it possible to run Internet Explorer on OS X El Capitan?

Cross compiling for RPi - error while loading shared libraries

RSA: Danger of using p to create q

Why are electrically insulating heatsinks so rare? Is it just cost?

What is the word for reserving something for yourself before others do?

Does an object always see its latest internal state irrespective of thread?

What's that red-plus icon near a text?

Is it unprofessional to ask if a job posting on GlassDoor is real?

What does "Puller Prush Person" mean?

Do infinite dimensional systems make sense?

Approximately how much travel time was saved by the opening of the Suez Canal in 1869?

Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?

How do I deal with an unproductive colleague in a small company?

High voltage LED indicator 40-1000 VDC without additional power supply

How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?

Add text to same line using sed

Languages that we cannot (dis)prove to be Context-Free



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?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








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,5531425




      2,5531425






















          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,21621626




                2,21621626





























                    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

                    AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

                    Алба-Юлія

                    Захаров Федір Захарович