You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
test('render error message if products fail to load',async()=>{axios.get.mockReturnValue(newPromise((resolve,reject)=>reject('some error')));const{ queryByText, getByText, debug }=renderComponent();expect(queryByText('Loading…')).toBeInTheDOM();expect(queryByText('Failed to load products')).not.toBeInTheDOM();awaitwaitForElement(()=>getByText('Failed to load products'));expect(queryByText('Failed to load products')).toBeInTheDOM();});
React component segment
render(){const{
loading,
error,
products,
favourites,
sortedBy
}=this.props;if(loading)return<divdata-test-id="loading-products">Loading…</div>if(error)return<divdata-test-id="products-error">Failed to load product</div>
What you did:
Testing a component error state
What happened:
When test is executed the component remains in loading state, even when error flips to true and loading false (confirmed by console logging).
However when I wrap the text "Failed to load product" inside of an additional element such as a <span /> the test passes.
It appears to me that waitForElement is expecting a change in the markup shape rather than just text in this instance…
react-testing-library
version: 4.1.2react
version: 16.1.0node
version: 8.11.3npm
(oryarn
) version: 5.6.0Relevant code or config:
Test
React component segment
What you did:
Testing a component error state
What happened:
When test is executed the component remains in loading state, even when error flips to
true
and loadingfalse
(confirmed by console logging).However when I wrap the text "Failed to load product" inside of an additional element such as a
<span />
the test passes.It appears to me that
waitForElement
is expecting a change in the markup shape rather than just text in this instance…Reproduction:
Repository
run
yarn test:reactTestingLibrary
Watch all the tests pass.
Then remove the
<span />
from Products.js L39The test should fail.
Problem description:
I'm having to change the shape of the returned JSX in order to satisfy tests.
The text was updated successfully, but these errors were encountered: