import { render, screen } from "@testing-library/react"; import Portfolios from ".";…
- User
import { render, screen } from "@testing-library/react"; import Portfolios from "."; test("find link in document", () => { render(); const netlifyLinkSecondProject = screen.getByRole("link"); expect(netlifyLinkSecondProject).toHaveAttribute( "href", "https://www.test.com/" ); }); what wrong?
- Assistant
This test is not specific enough. It is finding any link in the rendered
Portfolioscomponent and checking if it has a certainhrefattribute value. This means that if there are multiple links, it may not be checking the correct one. Also, it may fail if thePortfolioscomponent is changed and no longer has a link with thathrefattribute value.To make the test more specific, you could add more information to the selector to find the correct link. For example, you could add a class or data-testid to the link element and use that to select it in the test. Also, you could add more expectations to check that the link text is correct or that the link opens in a new tab.
Here's an example of how the test could be improved:
import { render, screen } from "@testing-library/react"; import Portfolios from "."; test("find link to second project on Netlify", () => { render(<Portfolios />); const netlifyLinkSecondProject = screen.getByTestId("netlify-link-2"); expect(netlifyLinkSecondProject).toHaveAttribute( "href", "https://www.test.com/" ); expect(netlifyLinkSecondProject).toHaveTextContent("Second Project"); expect(netlifyLinkSecondProject).toHaveAttribute("target", "_blank"); expect(netlifyLinkSecondProject).toHaveAttribute("rel", "noopener noreferrer"); });