import Images from "../../assets/exportFiles"; import { useInView } from…
- User
import Images from "../../assets/exportFiles"; import { useInView } from "react-intersection-observer";
const Tools = () => { const { ref, inView } = useInView({ threshold: 0, triggerOnce: true }); const myTools = [ { pict: Images.javaScript, text: "Javascript" }, { pict: Images.reactIcon, text: "React" }, { pict: Images.typeScriptIcon, text: "Typescript" }, { pict: Images.reduxIcon, text: "Redux" }, { pict: Images.sassIcon, text: "Sass" }, { pict: Images.bootstrap, text: "Bootstrap" }, { pict: Images.bitBucket, text: "Bitbucket" }, { pict: Images.sourceTree, text: "Sourcetree" }, ]; return ( // Tools My essentials {myTools.map((el, index) => { return ( <div key={index} className={ inView ? "tollsIcon__img" : "tollsIcon__img tollsIcon__img--translate" } > {el.text} ); })} ); }; export default Tools; write test in jest
- Assistant
As an AI language model, I am not able to write tests in Jest. However, below is an example of how a test for Tools component could look like using Jest:
import React from "react"; import { render } from "@testing-library/react"; import Tools from "./Tools";
test("renders all tool icons", () => { const { getAllByAltText } = render(); const toolIcons = getAllByAltText(/tool icon/i); expect(toolIcons).toHaveLength(8); });
This test checks if the Tools component renders all eight tool icons. It does so by searching for all img tags with an alt attribute containing the phrase "tool icon". If the test finds all eight images, it passes.
- User
but there no tool icon the alt are in Tools array
- Assistant
My apologies for the confusion. You are correct that the
altattributes for the images in theToolscomponent are not set to "tool icon". Instead, they contain the name of the tool.Here is an updated test that matches on the correct
altattributes:import React from "react"; import { render } from "@testing-library/react"; import Tools from "./Tools"; test("renders all tool icons", () => { const { getAllByAltText } = render(<Tools />); const toolIcons = getAllByAltText(/Javascript|React|Typescript|Redux|Sass|Bootstrap|Bitbucket|Sourcetree/i); expect(toolIcons).toHaveLength(8); });This test searches for
imgtags with analtattribute that matches any of the tool names included in themyToolsarray. If all 8 images are found, the test passes.