React-testing-library: changes due to input
I'm trying to test that a component updates as it should due to changes in an input element. I use the fireEvent.change()
-function, and if I then check the value of the node I found using getByPlaceholderText
it has updated as it should. However I cannot see the changes in the react component itself.
This might be because the changes don't happen until a rerender; how would I test this? react-testing-library's rerender
appears to start the component "from scratch" (i.e. without the new input value), and waitForElement
never finds what it's waiting for.
Here's the component TestForm.js:
import React from 'react';
import { withState } from 'recompose';
const initialInputValue = 'initialInputValue';
const TestForm = ({ inputValue, setInputValue }) => (
<>
{console.log('inputValue', inputValue)}
<input value={inputValue} onChange={(e) => setInputValue(e.target.value)} placeholder="placeholder" />
{inputValue !== initialInputValue && <div>Input has changed</div>}
</>
);
export default withState('inputValue', 'setInputValue', initialInputValue)(TestForm);
And here's the test, run using npx jest test.js
:
import React from 'react';
import { cleanup, fireEvent, render, waitForElement } from 'react-testing-library';
import TestForm from './TestForm';
afterEach(cleanup);
describe('TestForm', () => {
it('Change input', async () => {
const { getByPlaceholderText, getByText } = render(<TestForm />);
const inputNode = getByPlaceholderText('placeholder');
fireEvent.change(inputNode, { target: { value: 'new value' } });
console.log('inputNode.value', inputNode.value);
await waitForElement(() => getByText('Input has changed'));
});
});
javascript reactjs unit-testing react-testing-library
add a comment |
I'm trying to test that a component updates as it should due to changes in an input element. I use the fireEvent.change()
-function, and if I then check the value of the node I found using getByPlaceholderText
it has updated as it should. However I cannot see the changes in the react component itself.
This might be because the changes don't happen until a rerender; how would I test this? react-testing-library's rerender
appears to start the component "from scratch" (i.e. without the new input value), and waitForElement
never finds what it's waiting for.
Here's the component TestForm.js:
import React from 'react';
import { withState } from 'recompose';
const initialInputValue = 'initialInputValue';
const TestForm = ({ inputValue, setInputValue }) => (
<>
{console.log('inputValue', inputValue)}
<input value={inputValue} onChange={(e) => setInputValue(e.target.value)} placeholder="placeholder" />
{inputValue !== initialInputValue && <div>Input has changed</div>}
</>
);
export default withState('inputValue', 'setInputValue', initialInputValue)(TestForm);
And here's the test, run using npx jest test.js
:
import React from 'react';
import { cleanup, fireEvent, render, waitForElement } from 'react-testing-library';
import TestForm from './TestForm';
afterEach(cleanup);
describe('TestForm', () => {
it('Change input', async () => {
const { getByPlaceholderText, getByText } = render(<TestForm />);
const inputNode = getByPlaceholderText('placeholder');
fireEvent.change(inputNode, { target: { value: 'new value' } });
console.log('inputNode.value', inputNode.value);
await waitForElement(() => getByText('Input has changed'));
});
});
javascript reactjs unit-testing react-testing-library
add a comment |
I'm trying to test that a component updates as it should due to changes in an input element. I use the fireEvent.change()
-function, and if I then check the value of the node I found using getByPlaceholderText
it has updated as it should. However I cannot see the changes in the react component itself.
This might be because the changes don't happen until a rerender; how would I test this? react-testing-library's rerender
appears to start the component "from scratch" (i.e. without the new input value), and waitForElement
never finds what it's waiting for.
Here's the component TestForm.js:
import React from 'react';
import { withState } from 'recompose';
const initialInputValue = 'initialInputValue';
const TestForm = ({ inputValue, setInputValue }) => (
<>
{console.log('inputValue', inputValue)}
<input value={inputValue} onChange={(e) => setInputValue(e.target.value)} placeholder="placeholder" />
{inputValue !== initialInputValue && <div>Input has changed</div>}
</>
);
export default withState('inputValue', 'setInputValue', initialInputValue)(TestForm);
And here's the test, run using npx jest test.js
:
import React from 'react';
import { cleanup, fireEvent, render, waitForElement } from 'react-testing-library';
import TestForm from './TestForm';
afterEach(cleanup);
describe('TestForm', () => {
it('Change input', async () => {
const { getByPlaceholderText, getByText } = render(<TestForm />);
const inputNode = getByPlaceholderText('placeholder');
fireEvent.change(inputNode, { target: { value: 'new value' } });
console.log('inputNode.value', inputNode.value);
await waitForElement(() => getByText('Input has changed'));
});
});
javascript reactjs unit-testing react-testing-library
I'm trying to test that a component updates as it should due to changes in an input element. I use the fireEvent.change()
-function, and if I then check the value of the node I found using getByPlaceholderText
it has updated as it should. However I cannot see the changes in the react component itself.
This might be because the changes don't happen until a rerender; how would I test this? react-testing-library's rerender
appears to start the component "from scratch" (i.e. without the new input value), and waitForElement
never finds what it's waiting for.
Here's the component TestForm.js:
import React from 'react';
import { withState } from 'recompose';
const initialInputValue = 'initialInputValue';
const TestForm = ({ inputValue, setInputValue }) => (
<>
{console.log('inputValue', inputValue)}
<input value={inputValue} onChange={(e) => setInputValue(e.target.value)} placeholder="placeholder" />
{inputValue !== initialInputValue && <div>Input has changed</div>}
</>
);
export default withState('inputValue', 'setInputValue', initialInputValue)(TestForm);
And here's the test, run using npx jest test.js
:
import React from 'react';
import { cleanup, fireEvent, render, waitForElement } from 'react-testing-library';
import TestForm from './TestForm';
afterEach(cleanup);
describe('TestForm', () => {
it('Change input', async () => {
const { getByPlaceholderText, getByText } = render(<TestForm />);
const inputNode = getByPlaceholderText('placeholder');
fireEvent.change(inputNode, { target: { value: 'new value' } });
console.log('inputNode.value', inputNode.value);
await waitForElement(() => getByText('Input has changed'));
});
});
javascript reactjs unit-testing react-testing-library
javascript reactjs unit-testing react-testing-library
asked Nov 22 at 18:15
jorgen
1,11321225
1,11321225
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This code works for me:
import React from "react";
const initialInputValue = "initialInputValue";
class TestForm extends React.Component {
constructor(props) {
super(props);
this.state = { inputValue: initialInputValue };
}
render() {
const { inputValue } = this.state;
return (
<div>
{console.log("inputValue", inputValue)}
<input
value={inputValue}
onChange={e => this.setState({ inputValue: e.target.value })}
placeholder="placeholder"
/>
{inputValue !== initialInputValue && <div>Input has changed</div>}
</div>
);
}
}
import { render, cleanup, fireEvent } from "react-testing-library";
import "jest-dom/extend-expect";
afterEach(cleanup);
test("form", () => {
const { getByPlaceholderText, getByText } = render(<TestForm />);
fireEvent.change(getByPlaceholderText("placeholder"), {
target: { value: "new value" }
});
expect(getByText("Input has changed")).toBeInTheDocument();
});
It does not work in codesandbox though, I guess they have some issues with keeping the browser and the test environment separated.
Oh, but unless I misunderstand you I'm seeing the opposite: the inputNode.value does change, but the getByText never triggers (the TestForm component never seems to update). Apologies if the question was unclear
– jorgen
Nov 23 at 8:47
1
Have you tried usingwait
instead ofwaitForElement
?
– Gpx
Nov 23 at 9:08
No I wasn't aware of that one! However it seems to give the same result. I tried like this:await wait(() => getByText('Input has changed'));
– jorgen
Nov 23 at 9:14
Please put the code in codesandbox so I can take a look at it while it's running
– Gpx
Nov 23 at 11:14
Here you go: codesandbox.io/s/nkoo75mmxp
– jorgen
Nov 23 at 12:54
|
show 2 more comments
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%2f53436344%2freact-testing-library-changes-due-to-input%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
This code works for me:
import React from "react";
const initialInputValue = "initialInputValue";
class TestForm extends React.Component {
constructor(props) {
super(props);
this.state = { inputValue: initialInputValue };
}
render() {
const { inputValue } = this.state;
return (
<div>
{console.log("inputValue", inputValue)}
<input
value={inputValue}
onChange={e => this.setState({ inputValue: e.target.value })}
placeholder="placeholder"
/>
{inputValue !== initialInputValue && <div>Input has changed</div>}
</div>
);
}
}
import { render, cleanup, fireEvent } from "react-testing-library";
import "jest-dom/extend-expect";
afterEach(cleanup);
test("form", () => {
const { getByPlaceholderText, getByText } = render(<TestForm />);
fireEvent.change(getByPlaceholderText("placeholder"), {
target: { value: "new value" }
});
expect(getByText("Input has changed")).toBeInTheDocument();
});
It does not work in codesandbox though, I guess they have some issues with keeping the browser and the test environment separated.
Oh, but unless I misunderstand you I'm seeing the opposite: the inputNode.value does change, but the getByText never triggers (the TestForm component never seems to update). Apologies if the question was unclear
– jorgen
Nov 23 at 8:47
1
Have you tried usingwait
instead ofwaitForElement
?
– Gpx
Nov 23 at 9:08
No I wasn't aware of that one! However it seems to give the same result. I tried like this:await wait(() => getByText('Input has changed'));
– jorgen
Nov 23 at 9:14
Please put the code in codesandbox so I can take a look at it while it's running
– Gpx
Nov 23 at 11:14
Here you go: codesandbox.io/s/nkoo75mmxp
– jorgen
Nov 23 at 12:54
|
show 2 more comments
This code works for me:
import React from "react";
const initialInputValue = "initialInputValue";
class TestForm extends React.Component {
constructor(props) {
super(props);
this.state = { inputValue: initialInputValue };
}
render() {
const { inputValue } = this.state;
return (
<div>
{console.log("inputValue", inputValue)}
<input
value={inputValue}
onChange={e => this.setState({ inputValue: e.target.value })}
placeholder="placeholder"
/>
{inputValue !== initialInputValue && <div>Input has changed</div>}
</div>
);
}
}
import { render, cleanup, fireEvent } from "react-testing-library";
import "jest-dom/extend-expect";
afterEach(cleanup);
test("form", () => {
const { getByPlaceholderText, getByText } = render(<TestForm />);
fireEvent.change(getByPlaceholderText("placeholder"), {
target: { value: "new value" }
});
expect(getByText("Input has changed")).toBeInTheDocument();
});
It does not work in codesandbox though, I guess they have some issues with keeping the browser and the test environment separated.
Oh, but unless I misunderstand you I'm seeing the opposite: the inputNode.value does change, but the getByText never triggers (the TestForm component never seems to update). Apologies if the question was unclear
– jorgen
Nov 23 at 8:47
1
Have you tried usingwait
instead ofwaitForElement
?
– Gpx
Nov 23 at 9:08
No I wasn't aware of that one! However it seems to give the same result. I tried like this:await wait(() => getByText('Input has changed'));
– jorgen
Nov 23 at 9:14
Please put the code in codesandbox so I can take a look at it while it's running
– Gpx
Nov 23 at 11:14
Here you go: codesandbox.io/s/nkoo75mmxp
– jorgen
Nov 23 at 12:54
|
show 2 more comments
This code works for me:
import React from "react";
const initialInputValue = "initialInputValue";
class TestForm extends React.Component {
constructor(props) {
super(props);
this.state = { inputValue: initialInputValue };
}
render() {
const { inputValue } = this.state;
return (
<div>
{console.log("inputValue", inputValue)}
<input
value={inputValue}
onChange={e => this.setState({ inputValue: e.target.value })}
placeholder="placeholder"
/>
{inputValue !== initialInputValue && <div>Input has changed</div>}
</div>
);
}
}
import { render, cleanup, fireEvent } from "react-testing-library";
import "jest-dom/extend-expect";
afterEach(cleanup);
test("form", () => {
const { getByPlaceholderText, getByText } = render(<TestForm />);
fireEvent.change(getByPlaceholderText("placeholder"), {
target: { value: "new value" }
});
expect(getByText("Input has changed")).toBeInTheDocument();
});
It does not work in codesandbox though, I guess they have some issues with keeping the browser and the test environment separated.
This code works for me:
import React from "react";
const initialInputValue = "initialInputValue";
class TestForm extends React.Component {
constructor(props) {
super(props);
this.state = { inputValue: initialInputValue };
}
render() {
const { inputValue } = this.state;
return (
<div>
{console.log("inputValue", inputValue)}
<input
value={inputValue}
onChange={e => this.setState({ inputValue: e.target.value })}
placeholder="placeholder"
/>
{inputValue !== initialInputValue && <div>Input has changed</div>}
</div>
);
}
}
import { render, cleanup, fireEvent } from "react-testing-library";
import "jest-dom/extend-expect";
afterEach(cleanup);
test("form", () => {
const { getByPlaceholderText, getByText } = render(<TestForm />);
fireEvent.change(getByPlaceholderText("placeholder"), {
target: { value: "new value" }
});
expect(getByText("Input has changed")).toBeInTheDocument();
});
It does not work in codesandbox though, I guess they have some issues with keeping the browser and the test environment separated.
edited Nov 23 at 17:03
answered Nov 22 at 19:14
Gpx
1,54821424
1,54821424
Oh, but unless I misunderstand you I'm seeing the opposite: the inputNode.value does change, but the getByText never triggers (the TestForm component never seems to update). Apologies if the question was unclear
– jorgen
Nov 23 at 8:47
1
Have you tried usingwait
instead ofwaitForElement
?
– Gpx
Nov 23 at 9:08
No I wasn't aware of that one! However it seems to give the same result. I tried like this:await wait(() => getByText('Input has changed'));
– jorgen
Nov 23 at 9:14
Please put the code in codesandbox so I can take a look at it while it's running
– Gpx
Nov 23 at 11:14
Here you go: codesandbox.io/s/nkoo75mmxp
– jorgen
Nov 23 at 12:54
|
show 2 more comments
Oh, but unless I misunderstand you I'm seeing the opposite: the inputNode.value does change, but the getByText never triggers (the TestForm component never seems to update). Apologies if the question was unclear
– jorgen
Nov 23 at 8:47
1
Have you tried usingwait
instead ofwaitForElement
?
– Gpx
Nov 23 at 9:08
No I wasn't aware of that one! However it seems to give the same result. I tried like this:await wait(() => getByText('Input has changed'));
– jorgen
Nov 23 at 9:14
Please put the code in codesandbox so I can take a look at it while it's running
– Gpx
Nov 23 at 11:14
Here you go: codesandbox.io/s/nkoo75mmxp
– jorgen
Nov 23 at 12:54
Oh, but unless I misunderstand you I'm seeing the opposite: the inputNode.value does change, but the getByText never triggers (the TestForm component never seems to update). Apologies if the question was unclear
– jorgen
Nov 23 at 8:47
Oh, but unless I misunderstand you I'm seeing the opposite: the inputNode.value does change, but the getByText never triggers (the TestForm component never seems to update). Apologies if the question was unclear
– jorgen
Nov 23 at 8:47
1
1
Have you tried using
wait
instead of waitForElement
?– Gpx
Nov 23 at 9:08
Have you tried using
wait
instead of waitForElement
?– Gpx
Nov 23 at 9:08
No I wasn't aware of that one! However it seems to give the same result. I tried like this:
await wait(() => getByText('Input has changed'));
– jorgen
Nov 23 at 9:14
No I wasn't aware of that one! However it seems to give the same result. I tried like this:
await wait(() => getByText('Input has changed'));
– jorgen
Nov 23 at 9:14
Please put the code in codesandbox so I can take a look at it while it's running
– Gpx
Nov 23 at 11:14
Please put the code in codesandbox so I can take a look at it while it's running
– Gpx
Nov 23 at 11:14
Here you go: codesandbox.io/s/nkoo75mmxp
– jorgen
Nov 23 at 12:54
Here you go: codesandbox.io/s/nkoo75mmxp
– jorgen
Nov 23 at 12:54
|
show 2 more comments
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53436344%2freact-testing-library-changes-due-to-input%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