Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if iframe exists with React testing library?

In my React app I have a component which embeds an Instagram post.

It renders the following HTML structure:

<div data-testid="instagram"><iframe class="instagram-media instagram-media-rendered" id="instagram-embed-3" //etc....

I am using React testing library.

How do I write a test that checks if the iframe element exists and e.g with the CSS class instagram-media?

like image 208
meez Avatar asked Mar 22 '26 14:03

meez


1 Answers

For accessibility purposes, it is recommended to always use the title attribute on an <iframe> to label/describe its content.

<div data-testid="instagram">
    <iframe title="Instagram embed" className="instagram-media ..." id="instagram-
embed-3"></iframe>
</div>

You can then test if the <iframe> element exists with the getByTitle query.

expect(screen.getByTitle('Instagram embed')).toBeInTheDocument();
like image 53
juliomalves Avatar answered Mar 24 '26 02:03

juliomalves



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!