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?
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:
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
add a comment |
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:
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
add a comment |
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:
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
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:
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
reactjs jestjs react-testing-library
asked Mar 8 at 1:52
Alvin LeeAlvin Lee
2,5331425
2,5331425
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Your modal is closed, you need to pass isOpen
to it:
render(<CustomModal headerText=headerText isOpen />);
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Your modal is closed, you need to pass isOpen
to it:
render(<CustomModal headerText=headerText isOpen />);
add a comment |
Your modal is closed, you need to pass isOpen
to it:
render(<CustomModal headerText=headerText isOpen />);
add a comment |
Your modal is closed, you need to pass isOpen
to it:
render(<CustomModal headerText=headerText isOpen />);
Your modal is closed, you need to pass isOpen
to it:
render(<CustomModal headerText=headerText isOpen />);
answered Mar 8 at 8:40
GpxGpx
2,07121626
2,07121626
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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